Bash 通过nslookup命令从许多其他ip中搜索我自己的ip地址
我有很多具有静态ip地址和a记录的虚拟服务器。我应该使用关联阵列来安装我的CMS。但我有个主意。也许我应该用dig或者其他的dnsutil? 所以,我开始写:Bash 通过nslookup命令从许多其他ip中搜索我自己的ip地址,bash,shell,for-loop,sh,Bash,Shell,For Loop,Sh,我有很多具有静态ip地址和a记录的虚拟服务器。我应该使用关联阵列来安装我的CMS。但我有个主意。也许我应该用dig或者其他的dnsutil? 所以,我开始写: start=test dns=com for i in "${start}" {1..20}."$dns"; do echo $i >> "/tmp/temp" done for ns in `cat /tmp/temp`; do if [[ `dig +short $ns=="192.168.110.1"` ]]; the
start=test
dns=com
for i in "${start}" {1..20}."$dns"; do
echo $i >> "/tmp/temp"
done
for ns in `cat /tmp/temp`; do
if [[ `dig +short $ns=="192.168.110.1"` ]]; then
dig +short $ns
fi
done
但是我的第二个循环出了问题。你能帮助我吗?
我应该用我的域生成一个列表,比如test1.com、test2.com。。。
然后我应该得到ip地址。下一步将比较我的系统ip,如果我有ip 192.168.110.1,我应该得到我的域名,比如test2.com。它不工作,我打破了我的头,但我不知道,如何做到这一点。如果可能的话,请提供帮助。直接的错误是
[[`dig+short$ns==“192.168.110.1”`]
只是检查dig
的输出是否为非空字符串(不是空字符串,因为作为查询传入的字符串无效)。表面上的解决办法是
if [[ `dig +short "$ns"` == "192.168.110.1" ]]; then ...
如果equals运算符周围的空格很重要,当然,不应将比较作为参数传递给dig
;但我会对你的脚本进行更多的重构。现在还不完全清楚你到底想要脚本做什么,但是像这样的事情
#!/bin/bash
start=test
dns=com
for i in {1..20}; do
host="$start.$i.$dns"
ip=$(dig +short "$host")
if [[ "$ip" == "192.168.110.1" ]]; then
# I'm guessing you want to perform a reverse lookup on the IP address here?
dig +short "$ip"
fi
done
{1..20}
大括号扩展仅限于Bash。您可以在读取i时将for
循环替换为可移植的seq 1 20 |;执行
,脚本也将与sh
兼容。