Reading Data from File into Array - C -
i'm doing project on kali linux run tool ettercap lists ip addresses on network , saves them .txt. need use these ip's on tool nmap, wanted write c code can save ips ip.txt array. far 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); } (i = 0; < 10; i++) { fscanf(iplist, "%d,", &iparray[i] ); } (i = 0; < 10; i++) { printf("nmap -a -t4 -f %d\n\n", iparray[i]); } fclose(iplist); return 0; }
the resulting output bunch of random numbers. ideas? matter i'm using kali? , iparray set 10; problem if don't have 10 ip addresses?
the ip addresses stored 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. 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 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 goal have code ignore "ip address :" , if possible output list.
given input format lines such as:
ip address : 10.0.0.1
then need read address string, or 4 separate small integers.
char *ip_address[16]; (i = 0; < 10; i++) { if (fscanf(fp, "ip address : %15s", ip_address) != 1) …report format error… …code convert dotted-decimal ipv4 address… }
or:
int ip[4]; (i = 0; < 10; i++) { if (fscanf(fp, "ip address : %d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]) != 4) …report format error… (j = 0; j < 4; j++) { if (ip[j] < 0 || ip[j] > 255) …report bogus value… } …convert dotted-decimal values ipv4 address… }
Comments
Post a Comment