linuxjishu/14043.html target=_blank class=infotextkey>ifconfig命令的灵活应用,从中获取mac地址、IP、掩码Mask、外网IP址址,以及从resolv.conf中读取dns信息。
#/usr/bin/env bash
#-------
NIC=eth0
MAC=`LANG=C ifconfig $NIC | awk '/HWaddr/{ print $5 }' `
IP=`LANG=C ifconfig $NIC | awk '/inet addr:/{ print $2 }' | awk -F: '{print $2 }'`
MASK=`LANG=C ifconfig $NIC | awk -F: '/Mask/{print $4}'`
ext_ip=`curl ifconfig.me`
if [ -f /etc/resolv.conf ];
then
dns=`awk '/^nameserver/{print $2}' /etc/resolv.conf `
fi
#------
echo "Your network information is as below:"
echo $MAC
echo $IP
echo $dns
echo $ext_ip