我的研究表明,nslookup
从来没有真正打算用于脚本化使用:dig
而是使用它,它带有+short
选项,根据查询参数生成机器可读的输出。
dig +short myip.opendns.com @resolver1.opendns.com
话虽这么说,我的路由器不支持 dig 并支持nslookup
.我试图将返回的 IP 地址保存为一个变量,以便与实际 IP 地址进行比较:
ip=$(curl --沉默http://api.ipify.org/)
awk
在Ubuntu上成功:
nslookup yahoo.com | awk -F': ' 'NR==6 { print $2 } '
但在 DD-WRT 路由器上返回错误:
nslookup: can't resolve '(null)'
2001:4998:58:1836::10 media-router-fp1.prod1.media.vip.bf1.yahoo.com
最终目标
我有 CLI.sh命令更新 DDNS 服务:每当测量和存储的 DDNS IP 地址不同时,我想执行更新命令(CRON 作业每 5 分钟检查一次)。
问题
相当于上面所说的 dig 示例的最简单的 nslookup 是什么?
观察、反馈和测试区域
添加了第二个参数:
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#
键入 nslookup:
root@DD-WRT:~# type nslookup
nslookup is /usr/bin/nslookup
管道到 grep 然后到文件:
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 雅虎
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
格列普:
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
格列普:
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.
答案1
这有效:
nslookup "$D" 2>/dev/null | grep "Address " | sed 's/.*: //g;s/ .*//g'
但请注意
- 它可以返回多个地址,因此循环遍历它们
- 如果你不需要它们,则 grep 出 ipv6
- 许多域名每次查询都会解析不同的内容(例如亚马逊随机使用cloudfront、fastly或其他ip)