获取本地网卡IP mac地址 掩码 dns 外网IP的shell脚本

发布时间:2020-07-29编辑:脚本学堂
一个可以获取到本地网卡IP、MAC地址、掩码、dsn、外网IP信息的shell脚本,供大家学习参考。

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