【问题标题】:Searching by nslookup command my own ip address from many others ip通过 nslookup 命令从许多其他 ip 中搜索我自己的 ip 地址
【发布时间】:2017-08-31 14:57:33
【问题描述】:

我有很多具有静态 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"` ]]; then
dig +short $ns
fi
done

但是我的第二个循环出了点问题。你能帮助我吗 ? 我应该生成一个包含我的域的列表,例如 test1.com、test2.com ... 之后我应该得到IP地址。下一步将与我的系统 ip 进行比较,如果我有 ip 192.168.110.1,我应该得到我的域名,例如 test2.com。它不起作用,我打破了我的头,但我不知道如何做到这一点。如果可能,请提供帮助。

【问题讨论】:

    标签: bash shell for-loop sh


    【解决方案1】:

    直接的错误是[[ `dig +short $ns=="192.168.110.1"` ]] 只是检查dig 的输出是否为非空字符串(它不是,因为作为查询传入的字符串不是有效字符串)。表面上的修复是

    if [[ `dig +short "$ns"` == "192.168.110.1" ]]; then ...
    

    等号周围的空格很重要,当然,比较不应该作为参数传递给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。您可以将for 循环替换为可移植的seq 1 20 | while read i; do,然后脚本也将与sh 兼容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多