Linux 国家/地区和外部IP Bash脚本
我根据互联网上的内容和一些bash教程创建了一个脚本,它将显示我的外部IP和它所在的国家 # 脚本如下所示:Linux 国家/地区和外部IP Bash脚本,linux,bash,shell,ip,wan,Linux,Bash,Shell,Ip,Wan,我根据互联网上的内容和一些bash教程创建了一个脚本,它将显示我的外部IP和它所在的国家 # 脚本如下所示: #!/bin/bash wanip=$(dig +short myip.opendns.com @resolver1.opendns.com); echo "$wanip" > /root/Documents/filewanip; iplist="/root/Documents/filewanip" while read IP;do whois "$IP" done <
#!/bin/bash
wanip=$(dig +short myip.opendns.com @resolver1.opendns.com);
echo "$wanip" > /root/Documents/filewanip;
iplist="/root/Documents/filewanip"
while read IP;do
whois "$IP"
done < "$iplist" | grep "country" >geoloc
cat geoloc filewanip
rm filewanip geoloc
#/bin/bash
wanip=$(dig+short myip.opendns.com@resolver1.opendns.com);
echo“$wanip”>/root/Documents/filewanip;
iplist=“/root/Documents/filewanip”
读IP时;做
谁是“$IP”
完成“$iplist”| grep”国家“>geoloc
cat geoloc filewanip
rm filewanip geoloc
#
输出如下所示:
国家:荷兰
183.64.132.80
#
问题是,我不想使用文件来实现这一点,因为文件结构在不同的系统之间发生了明显的变化。
如何以优雅的方式进行检查,从而将检查存储到变量中,然后直接显示到shell中
据我所知,您的目标是消除临时文件的使用。在这种情况下:
#!/bin/bash
wanip=$(dig +short myip.opendns.com @resolver1.opendns.com);
echo "$wanip" | while read ip; do
echo "$ip $(whois "$ip" | awk ' /[Cc]ountry/{print $2}')"
done
上面写的好像dig
为您的IP返回多个地址。如果不是这样,那么while
循环是多余的
如果您只需要一个IP,则:
#!/bin/bash
ip=$(dig +short myip.opendns.com @resolver1.opendns.com);
echo "$ip $(whois "$ip" | awk ' /[Cc]ountry/{print $2}')"
注:
- 我将IP转换为IP,因为最好对shell变量使用小写或混合大小写的名称
- My
返回一行国家,而不是国家。因此,我对country的搜索不区分大小写whois