Reply

bash script to perform dig -x

[ Edited ]
New Member
Posts: 1
3193     2

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.

216.58.219.78
206.190.36.45
173.252.120.6

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.

Re: bash script to perform dig -x

New Member
Posts: 1
3193     2

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.

 

BK

 

--

 

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 
162.159.200.1
216.229.0.50
ibrimac:test kellyb$ ./test.sh 
+ IPLIST=list2.txt
++ cat list2.txt
+ for IP in '`cat $IPLIST`'
++ dig -x 162.159.200.1 +short
++ head -1
+ domain=time.cloudflare.com.
+ echo -e time.cloudflare.com.
+ for IP in '`cat $IPLIST`'
++ dig -x 216.229.0.50 +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$ 

Re: bash script to perform dig -x

Superuser
Posts: 105
3193     2

hi,

 

you can do like this

 

while read -r line; do dig -x $line +short >> result.csv; done < IPLIST
Showing results for 
Search instead for 
Did you mean: 

Recommended for You