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
END GOAL
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).
QUESTION
What is the simplest nslookup equivalent to said dig example above?
OBSERVATIONS, FEEDBACK & TESTING AREA
second argument added:
root@DD-WRT:/opt# nslookup mydomain.asuscomm.com resolver1.opendns.com
Server: 208.67.222.222
Address 1: 208.67.222.222 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
root@DD-WRT:/opt#
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
nslookup
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: 72.30.35.9 media-router-fp1.prod1.media.vip.bf1.yahoo.com
Address 8: 98.137.246.7 media-router-fp1.prod1.media.vip.gq1.yahoo.com
Address 9: 98.138.219.231 media-router-fp1.prod1.media.vip.ne1.yahoo.com
Address 10: 98.138.219.232 media-router-fp2.prod1.media.vip.ne1.yahoo.com
Address 11: 72.30.35.10 media-router-fp2.prod1.media.vip.bf1.yahoo.com
Address 12: 98.137.246.8 media-router-fp2.prod1.media.vip.gq1.yahoo.com
grep:
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: 98.137.246.8 media-router-fp2.prod1.media.vip.gq1.yahoo.com
Address 8: 98.137.246.7 media-router-fp1.prod1.media.vip.gq1.yahoo.com
grep:
root@DD-WRT:/opt# nslookup yahoo.com | grep -o -E '([0-9][0-9]?[0-9]?\.?){4}$'
nslookup: can't resolve '(null)'
grep3:
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
7W.10X.7Y.6Z
W-10X-7Y-6Z.