10-31-2021 09:53 AM - edited 11-01-2021 07:47 AM
Good day. I was reading another post regarding resolving hostnames to IPs and only using the first IP in the list.
I want to do the opposite and used the following script:
#!/bin/bash IPLIST="/Users/mymac/Desktop/list2.txt" applinked for IP in 'cat $IPLIST'; do domain=$(dig -x $IP +short | head -1) echo -e "$domain" >> results.csv done < domainlist.txt
I would like to give the script a list of 1000+ IP addresses collected from a firewall log, and resolve the list of destination IP's to domains. I only want one entry in the response file since I will be adding this to the CSV I exported from the firewall as another "column" in Excel. I could even use multiple responses as semi-colon separated on one line (or /,|,\,* etc). The list2.txt is a standard ascii file. I have tried EOF in Mac, Linux, Windows.
18.104.22.168 22.214.171.124 126.96.36.199
What I am getting now:
The domainlist.txt is getting an exact duplicate of list2.txt while the results has nothing. No error come up on the screen when I run the script either.
I am running Mac OS X with Macports.
11-01-2021 07:43 AM
Make sure you are using backticks around the cat $IPLIST and since you're not inputting from STDIN, I'd get rid of the < domainlist.txt after the done statement too.
ibrimac:test kellyb$ dir total 16 -rw-r--r--@ 1 kellyb staff 27B Nov 1 10:38 list2.txt -rwxr-xr-x@ 1 kellyb staff 161B Nov 1 10:39 test.sh* ibrimac:test kellyb$ cat test.sh #!/bin/bash -x IPLIST="list2.txt" for IP in `cat $IPLIST`; do domain=$(dig -x $IP +short | head -1) echo -e "$domain" >> results.csv done # < domainlist.txt ibrimac:test kellyb$ cat list2.txt 188.8.131.52 184.108.40.206 ibrimac:test kellyb$ ./test.sh + IPLIST=list2.txt ++ cat list2.txt + for IP in '`cat $IPLIST`' ++ dig -x 220.127.116.11 +short ++ head -1 + domain=time.cloudflare.com. + echo -e time.cloudflare.com. + for IP in '`cat $IPLIST`' ++ dig -x 18.104.22.168 +short ++ head -1 + domain=nu.binary.net. + echo -e nu.binary.net. ibrimac:test kellyb$ dir total 24 -rw-r--r--@ 1 kellyb staff 27B Nov 1 10:38 list2.txt -rw-r--r-- 1 kellyb staff 36B Nov 1 10:40 results.csv -rwxr-xr-x@ 1 kellyb staff 161B Nov 1 10:39 test.sh* ibrimac:test kellyb$ cat results.csv time.cloudflare.com. nu.binary.net. ibrimac:test kellyb$