怎么用Linux shell脚本获取外网IP地址
导读:本文共810字符,通常情况下阅读需要3分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 基于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#... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。基于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地址的详细内容,希望对您有所帮助,信息来源于网络。