首页 > 代码库 > 获取本机IP地址的小脚本
获取本机IP地址的小脚本
获取本机私网地址(1个)
1 #!/bin/bash 2 # Author : standby 3 # Date : 2017-05-18 4 # Description : Get private ip address of local machine. 5 6 arr=(`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk ‘{print $2}‘|tr -d "addr:"`) 7 #private_ip=${arr[0]} 8 private_ip=‘0.0.0.0‘ 9 10 for ip in ${arr[*]};11 do12 ip_tmp=`echo $ip | grep ‘^10\.‘`13 if [[ ! -z $ip_tmp ]];then14 private_ip=$ip_tmp15 break16 fi17 done18 [[ $private_ip == "0.0.0.0" ]] && echo "No private ip found!" || echo $private_ip
获取本机公网地址(1个或多个)
1 #!/bin/bash 2 # Author : standby 3 # Date : 2017-05-18 4 # Description : Get all public ip address of local machine. 5 6 arr=(`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk ‘{print $2}‘|tr -d "addr:"`) 7 #public_ip=${arr[0]} 8 public_ip=‘0.0.0.0‘ 9 10 for ip in ${arr[*]}11 do12 ip_tmp=`echo $ip | grep -v ‘^10\.‘`13 if [[ ! -z $ip_tmp ]];then14 public_ip=$public_ip$ip_tmp","15 #break16 fi17 done18 if [[ $public_ip == "0.0.0.0" ]]19 then20 echo "No public ip found!"21 else22 pub_arr=(`echo $public_ip |sed ‘s/0.0.0.0//g‘ |tr ‘,‘ ‘\n‘`)23 for ip in ${pub_arr[*]}24 do25 ip_tmp=`echo $ip | grep -vE ‘^192\.|^172\.‘`26 [[ ! -z $ip_tmp ]] && echo $ip_tmp27 done28 fi
获取本机IP地址的小脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。