My research indicated nslookup was never really intended for scripted use: use dig instead, which with the +short option produces machine-readable output according to the query parameters.

dig +short myip.opendns.com @resolver1.opendns.com

That being said, my router does not support dig and supports nslookup. I seek to save the returned IP address a variable for a comparison with the actual IP address:

ip=$(curl --silent http://api.ipify.org/)

awk was successful on Ubuntu:

nslookup yahoo.com  | awk -F': ' 'NR==6 { print $2 } '

but returned an error on the DD-WRT router:

nslookup: can't resolve '(null)'
2001:4998:58:1836::10 media-router-fp1.prod1.media.vip.bf1.yahoo.com


I have CLI .sh command to update the DDNS service: I would like to execute the update command whenever the measured and stored DDNS IP address are different (CRON job to check every 5 mintues).


What is the simplest nslookup equivalent to said dig example above?


second argument added:

root@DD-WRT:/opt# nslookup mydomain.asuscomm.com  resolver1.opendns.com
Address 1: resolver1.opendns.com

Name:      mydomain.asuscomm.com
Address 1: 7W.10X.7Y.6Z c-7W-10X-7Y-6Z.hsd1.fl.comcast.net

devnull: > /dev/null

root@DD-WRT:/opt# nslookup mydomain.asuscomm.com  > /dev/null
nslookup: can't resolve '(null)'

devnull: > /dev/null 2>&1

root@DD-WRT:/opt# nslookup mydomain.asuscomm.com  > /dev/null 2>&1

type nslookup:

root@DD-WRT:~# type nslookup
nslookup is /usr/bin/nslookup

pipe to grep then to file:

root@DD-WRT:/opt# nslookup myDomain.asuscomm.com | grep Address > test.txt
nslookup: can't resolve '(null)'
root@DD-WRT:/opt# cat test.txt
Address 1: 7W.10X.7Y.6Z c-7W-10X-7Y-6Z.hsd1.fl.comcast.net


root@DD-WRT:/opt/test# nslookup myFQDN.asuscomm.com

Name:      mydomain.asuscomm.com
Address 1: 7x.10x.7.x6x c-7x-10x-7x-6x.hsd1.fl.comcast.net

nslookup yahoo

root@DD-WRT:/opt# nslookup yahoo.com

nslookup: can't resolve '(null)'

Name:      yahoo.com
Address 1: 2001:4998:c:1023::5 media-router-fp2.prod1.media.vip.gq1.yahoo.com
Address 2: 2001:4998:58:1836::10 media-router-fp1.prod1.media.vip.bf1.yahoo.com
Address 3: 2001:4998:58:1836::11 media-router-fp2.prod1.media.vip.bf1.yahoo.com
Address 4: 2001:4998:c:1023::4 media-router-fp1.prod1.media.vip.gq1.yahoo.com
Address 5: 2001:4998:44:41d::3 media-router-fp1.prod1.media.vip.ne1.yahoo.com
Address 6: 2001:4998:44:41d::4 media-router-fp2.prod1.media.vip.ne1.yahoo.com
Address 7: media-router-fp1.prod1.media.vip.bf1.yahoo.com
Address 8: media-router-fp1.prod1.media.vip.gq1.yahoo.com
Address 9: media-router-fp1.prod1.media.vip.ne1.yahoo.com
Address 10: media-router-fp2.prod1.media.vip.ne1.yahoo.com
Address 11: media-router-fp2.prod1.media.vip.bf1.yahoo.com
Address 12: media-router-fp2.prod1.media.vip.gq1.yahoo.com


root@DD-WRT:/# nslookup yahoo.com | grep gq1

nslookup: can't resolve '(null)'
Address 1: 2001:4998:c:1023::4 media-router-fp1.prod1.media.vip.gq1.yahoo.com
Address 6: 2001:4998:c:1023::5 media-router-fp2.prod1.media.vip.gq1.yahoo.com
Address 7: media-router-fp2.prod1.media.vip.gq1.yahoo.com
Address 8: media-router-fp1.prod1.media.vip.gq1.yahoo.com


root@DD-WRT:/opt# nslookup yahoo.com | grep -o -E '([0-9][0-9]?[0-9]?\.?){4}$'

nslookup: can't resolve '(null)'


root@DD-WRT:/opt# nslookup mydomain.asuscomm.com | grep -o -E '([0-9][^:][0-9]?[0-9]?\.?){4}' > output.txt
nslookup: can't resolve '(null)'
root@DD-WRT:/opt# cat output.txt
  • Can you confirm that the command you used on your router was also nslookup yahoo.com | awk -F': ' 'NR==6 { print $2 } ' – Jeff H. Apr 20 at 12:58
  • @JeffH. Yes: the same command was copied for execution on both platforms – gatorback Apr 20 at 13:00
  • What's your goal? To update your OpenDNS IP address only if it has been changed? – Kusalananda Apr 20 at 13:07
  • @Kusalananda Good question. Updating DDNS through the ASUS service only if the measured (nslookup) IP address has changed. OP edited to reflect the goal. – gatorback Apr 20 at 13:16
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.