1

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.

JonBjatBun
  • 39
  • 5

3 Answers3

2

Minimalist workaround quasi-answer. Here's a one-liner replacement for the script using GNU parallel, host (less work to parse than nslookup), and sed:

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

...or using nslookup at the cost of added GNU sed complexity.

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

...or using xargs:

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

Output of any of those:

www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35
www.wikipedia.org 208.80.154.224
agc
  • 7,973
  • 2
  • 29
  • 50
  • [*Potong*'s `sed` & `host` one-liner](https://stackoverflow.com/a/60913759/6136214) is a better and simpler version of the above. – agc Mar 30 '20 at 14:41
  • Really good command but is it possible to work it around without parallel ? @agc – JonBjatBun Apr 01 '20 at 20:35
1

Replace your complete nslookup line with:

echo "$IP $(dig +short "$IP" | grep -m 1 -E '^[0-9.]{7,15}$')"
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

This might work for you (GNU sed and host):

sed '/\S/{s#.*#host & | sed -n "/ has address/{s///p;q}"#e}' file

For all non-empty lines: invoke the host command on the supplied host name and pipe the results to another invocation of sed which strips out text and quits after the first result.

potong
  • 55,640
  • 6
  • 51
  • 83
  • This is better. Hadn't though of using ***`e`***_val_ instead of `parallel`. – agc Mar 29 '20 at 14:04
  • @agc I'm all for using GNU parallel but in this case it seems the OP wants to preserve empty lines and sed seemed a better fit in this circumstance. – potong Mar 30 '20 at 14:30