gubbu
March 20, 2014, 1:46am
1
I have a list of hostnames in a txt file . I need to do nslookup or other command on linux and get the ip address and if you dont find an ip address then put 0.0.0.0 instead in the output text file along with the hostname.
So input
host1
host2
host2.dd.ddd.net
Output
host1, 10.136.21.2
host2,10.127.1.2
host2.dd.ddd.net,0.0.0.0
thanks
G
Scott
March 20, 2014, 4:53am
2
Using the host command:
$ cat myScript
while read HOST x; do
IPS=$(host -t A "$HOST" | awk '/has address/ { IP=IP","$NF } END { print IP?IP:",0.0.0.0" }')
echo "$HOST$IPS"
done < hosts.txt > hosts.new.txt
$ ./myScript
$ cat hosts.txt
bbc.co.uk
google.com
ns1.scottn.adm
vmc.scottn.vm
vmd
blah_blah
$ cat hosts.new.txt
bbc.co.uk,212.58.244.18,212.58.246.104,212.58.244.20,212.58.246.103
google.com,173.194.116.70,173.194.116.64,173.194.116.65,173.194.116.73,173.194.116.66,173.194.116.67,173.194.116.68,173.194.116.69,173.194.116.78,173.194.116.71,173.194.116.72
ns1.scottn.adm,10.10.10.100
vmc.scottn.vm,10.10.10.173
vmd,10.10.10.174
blah_blah,0.0.0.0
(Change 'IP=IP","$NF' to 'print ","$NF; exit' in the main action, and remove the END clause, to store only the first IP address)