Sample Header Ad - 728x90

Why does nslookup script give different results?

0 votes
2 answers
430 views
I try to use name resolution and get IP address of a web site. I used **nslookup** command and use 6'th line of its output. Because I noticed that output of nslookup contains IP address(in IPv4) in 6'th line. My command was :
website=
IP=$(nslookup "$website" | head -n 6 | tail -n 1 | cut -d" " -f2)
Also I tried sed command to reach the same goal and used :
website=
IP=$(nslookup "$website" | sed -n 6p | cut -d" " -f2)
result was the same, result is unreliable, sometimes worked, sometimes not. It works correctly but not always. Sometimes it reads the 7'th line, not the 6'th line and fails. Actually I solved my problem using another approach :
website=
newIP=$(nslookup "$website" | grep "Address: " | head -n 1 | cut -d" " -f2)
which gave the correct line and IP address everytime(although it can give more than one IP > nslookup can return more than one IP ) Why do the first two codes fail?
Asked by user458762
Feb 11, 2022, 05:40 PM
Last activity: Feb 11, 2022, 06:14 PM