1.获取本机内网ip地址
#!/bin/bash # Author : # Date : # Description : Get private ip address of local machine. arr=(`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`) #private_ip=${arr[0]} private_ip='0.0.0.0' for ip in ${arr[*]}; do ip_tmp=`echo $ip | grep '^10\.'` if [[ ! -z $ip_tmp ]];then private_ip=$ip_tmp break fi done [[ $private_ip == "0.0.0.0" ]] && echo "No private ip found!" || echo $private_ip
2.获取本机外网ip地址(1个或多个)
#!/bin/bash # Author : # Date : # Description : Get all public ip address of local machine. arr=(`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`) #public_ip=${arr[0]} public_ip='0.0.0.0' for ip in ${arr[*]} do ip_tmp=`echo $ip | grep -v '^10\.'` if [[ ! -z $ip_tmp ]];then public_ip=$public_ip$ip_tmp"," #break fi done if [[ $public_ip == "0.0.0.0" ]] then echo "No public ip found!" else pub_arr=(`echo $public_ip |sed 's/0.0.0.0//g' |tr ',' '\n'`) for ip in ${pub_arr[*]} do ip_tmp=`echo $ip | grep -vE '^192\.|^172\.'` [[ ! -z $ip_tmp ]] && echo $ip_tmp done fi