怎么用Linux shell脚本获取外网IP地址(IP,linux,shell,开发技术)

时间:2024-05-07 00:59:19 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :
怎么用Linux shell脚本获取外网IP地址

基于Linux系统的获取外网IP地址的shell脚本,脚本内容如下: getIp.sh

#!/bin/bash#检查IP的合法性check_ipaddr(){echo$1|grep"^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$">/dev/null;if[$?-ne0]then#echo"IP地址必须全部为数字"return1fiipaddr=$1a=`echo$ipaddr|awk-F.'{print$1}'`#以"."分隔,取出每个列的值b=`echo$ipaddr|awk-F.'{print$2}'`c=`echo$ipaddr|awk-F.'{print$3}'`d=`echo$ipaddr|awk-F.'{print$4}'`fornumin$a$b$c$ddoif[$num-gt255]||[$num-lt0]#每个数值必须在0-255之间then#echo$ipaddr"中,字段"$num"错误"return1fidone#echo$ipaddr"地址合法"return0}host=ns1.dnspod.netport=16666ip=`cat/tcp/$host/$port`check_ipaddr"$ip"if["$?"x="0"x];thenecho"外网IP地址:$ip"elseecho"获取IP地址失败!"fi

使用命令 chmod +x getIp.sh 给脚本授权后即可执行输出结果。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么用Linux shell脚本获取外网IP地址的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Shell怎么监控网站URL运行情况下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18