DEFAULTNET=”$(ip route show |grep -o ‘default via [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.*’ |head -n1 |sed ‘s/proto.*\|onlink.*//g’ |awk ‘{print $NF}’)”
[ -n "$DEFAULTNET" ] && IPSUB=”$(ip addr |grep ”${DEFAULTNET}” |grep ‘global’ |grep ‘brd’ |head -n1 |grep -o ‘[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/[0-9]\{1,2\}’)”
IPv4=”$(echo -n “$IPSUB” |cut -d’/’ -f1)”
NETSUB=”$(echo -n “$IPSUB” |grep -o ‘/[0-9]\{1,2\}’)”
GATE=”$(ip route show |grep -o ‘default via [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}’ |head -n1 |grep -o ‘[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}’)”
[ -n "$NETSUB" ] && MASK=”$(echo -n ’128.0.0.0/1,192.0.0.0/2,224.0.0.0/3,240.0.0.0/4,248.0.0.0/5,252.0.0.0/6,254.0.0.0/7,255.0.0.0/8,255.128.0.0/9,255.192.0.0/10,255.224.0.0/11,255.240.0.0/12,255.248.0.0/13,255.252.0.0/14,255.254.0.0/15,255.255.0.0/16,255.255.128.0/17,255.255.192.0/18,255.255.224.0/19,255.255.240.0/20,255.255.248.0/21,255.255.252.0/22,255.255.254.0/23,255.255.255.0/24,255.255.255.128/25,255.255.255.192/26,255.255.255.224/27,255.255.255.240/28,255.255.255.248/29,255.255.255.252/30,255.255.255.254/31,255.255.255.255/32′ |grep -o ‘[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}’${NETSUB}” |cut -d’/’ -f1)”
[ -n "$GATE" ] && [ -n "$MASK" ] && [ -n "$IPv4" ] || {
ipNum() {
local IFS=’.’
read ip1 ip2 ip3 ip4 <<<”$1″
echo $((ip1*(1<<24)+ip2*(1<<16)+ip3*(1<<8)+ip4))
}
SelectMax(){
ii=0
for IPITEM in `route -n |awk -v OUT=$1 ‘{print $OUT}’ |grep ‘[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}’`
do
NumTMP=”$(ipNum $IPITEM)”
eval “arrayNum[$ii]=’$NumTMP,$IPITEM’”
ii=$[$ii+1]
done
echo ${arrayNum[@]} |sed ‘s/\s/\n/g’ |sort -n -k 1 -t ‘,’ |tail -n1 |cut -d’,’ -f2
}
[[ -z $IPv4 ]] && IPv4=”$(ifconfig |grep ‘Bcast’ |head -n1 |grep -o ‘[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}’ |head -n1)”
[[ -z $GATE ]] && GATE=”$(SelectMax 2)”
[[ -z $MASK ]] && MASK=”$(SelectMax 3)”
}
PubIPv4=”$(wget -qO- checkip.amazonaws.com)”
[ -n "$IPv4" ] && [ -n "$PubIPv4" ] && {
[ "$IPv4" != "$PubIPv4" ] && IPv4=”$(echo “$IPv4; $PubIPv4″)”
}
[ -n "$GATE" ] && [ -n "$MASK" ] && [ -n "$IPv4" ] && {
echo -e “IPv4:\t\033[33m$IPv4\033[0m\nMASK:\t\033[33m$MASK\033[0m\nGATE:\t\033[33m$GATE\033[0m”
} || {
echo “Error! Not found network config. “
}