I'm doing a project on Kali Linux where I run the tool Ettercap that lists IP addresses on a network and saves them to a .txt. I then need to use these IP's on another tool nmap, so I wanted to write a C code that can save the IPs from IP.txt into an array. So far this is the closest working code:
#include <stdio.h>
#include <stdlib.h>
main(){
FILE *ipList;
ipList = fopen("IP.txt", "r");
int ipArray[10];
int i;
if (ipList == NULL)
{
printf("Error\n");
exit (0);
}
for (i = 0; i < 10; i++)
{
fscanf(ipList, "%d,", &ipArray[i] );
}
for (i = 0; i < 10; i++)
{
printf("nmap -A -T4 -F %d\n\n", ipArray[i]);
}
fclose(ipList);
return 0;
}
The resulting output is just a bunch of random numbers. Any ideas? Does it matter that I'm using Kali? And my ipArray is set to 10; will it be a problem if I don't have 10 ip addresses?
The IP addresses are stored like this:
IP address : 10.0.0.1
IP address : 10.0.0.2
IP address : 10.0.0.3
IP address : 10.0.0.4
I've made progress. This is my current out put:
nmap -A -T4 -F IP|nnmap -A -T4 -F address|nnmap -A -T4 -F :|nnmap -A -T4 -F 10.0.2.2|nnmap -A -T4 -F IP|nnmap -A -T4 -F address|nnmap -A -T4 -F :|nnmap -A -T4 -F 10.0.2.3|nnmap -A -T4 -F IP|nnmap -A -T4 -F address|nnmap -A -T4 -F :|nnmap -A -T4 -F 10.0.2.4
Here is my current code:
#include <stdio.h>
#include <stdlib.h>
main() {
FILE *ipList;
ipList = fopen("IP.txt","r");
char ip_addr[256];
while (fscanf(ipList, "%255s", ip_addr) == 1){
printf("nmap -A -T4 -F %s|n", ip_addr);
}
if (ipList == NULL){
printf("error\n");
exit (1);
}
fclose(ipList);
return 0;
}
So now my goal is to have the code ignore "IP address :" and if possible output it as a list.
fscanf()so you've no idea whether it thinks it was able to read an integer from the input. You'll never know whether the comma was matched or not, which probably doesn't matter. Your error message should be printed on standard error (stderr), not standard output. Your error exit should probably be1orEXIT_FAILURErather than0which indicates success. The fact that you're using Kali Linux is almost certainly completely immaterial. Your code is completely standard C, except that you should have an explicit return type:int main().%d(andint) for some non-existent format specifier (and type) that represents IP addresses, and you should probably use%s(corresponding to an array ofchar, rather than a pointer toint) to handle this input as strings, instead...char ip_addr[256]; while (fscanf(ipList, "%255s", ip_addr) == 1) { printf("nmap -A -T4 -F %s\n", ip_addr); }"%255[^,],"."%255[^,],"introduces a different challenge with regards to failure modes.