首页 > 解决方案 > nslookup/dig/drill commands on a file that contains websites to add ip addresses

问题描述

UPDATE : Still open for solutions using nslookup without parallel, dig or drill

I need to write a script that scans a file containing web page addresses on each line, and adds to these lines the IP address corresponding to the name using nslookup command. The script looks like this at the moment :

#!/usr/bin/

while read ip
do

   nslookup "$ip" | 
   awk '/Name:/{val=$NF;flag=1;next} /Address:/ &&
        flag{print val,$NF;val=""}' | 
   sed -n 'p;n'

done < is8.input

The input file contains the following websites :

www.edu.ro
vega.unitbv.ro

www.wikipedia.org

The final output should look like :

www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35

www.wikipedia.org 91.198.174.192

The main problem i have with the current state of the script is that it takes the names from nslookup (which is good for www.edu.ro) instead of taking the aliases when those are available. My output looks like this:

www.edu.ro 193.169.21.181
etc.unitbv.ro 193.254.231.35

dyna.wikimedia.org 91.198.174.192

I was thinking about implementing a if-else for aliases but i don't know how to do one on the current command. Also the script can be changed if anyone has a better understanding of how to format nslookup to show it like the output given.

标签: shellawksednslookupdig

解决方案


极简主义解决方法准答案。这是使用GNU 的脚本的单行替换parallelhost(解析的工作量少于nslookup),并且sed

parallel "host {} 2> /dev/null | 
          sed -n '/ has address /{s/.* /'{}' /p;q}'" < is8.input

...或以增加GNU复杂性nslookup为代价使用。 sed

parallel "nslookup {} 2> /dev/null | 
          sed -n '/^A/{s/.* /'{}' /;T;p;q;}'"  < is8.input

...或使用xargs

xargs -I '{}' sh -c \
         "nslookup {}  2> /dev/null | 
          sed -n '/^A/{s/.* /'{}' /;T;p;q;}'"  < is8.input

任何这些的输出:

www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35
www.wikipedia.org 208.80.154.224

推荐阅读