#!/bin/bash
#测试网络连通性,提取公网IP地址,如为私网则访问网站查询IP
function tesip(){
ping -c 3 www.baidu.com &> /dev/null
if [ ! $? == 0 ];then
echo "You Internet looks like down or baidu is down,Please check your configuration!"
exit 1
fi
}
function netip(){
# Get public IP address
IPL=`ip addr | grep 'scope global' | awk '{print $2}'| awk -F '/' '{print $1}'`
IP=$(ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1)
[ -z "$IP" ] && IP=$(wget -qO- -t1 -T2 ipv4.icanhazip.com)
echo "You public ipaddress is : ${IP}"
echo "You local ipaddress is :${IPL}"
}
tesip
netip
#---------------