查看网卡及对应的IP、MAC

#!/bin/bash
# judge OS
OS_release=`cat /etc/redhat-release | awk '{print $(NF-1)}'|cut -c 1`
# To obtain current network card name,Do not include lo
NETWORK_NAME=`ifconfig|grep -vE "^ |^$|lo"|awk -F "[ :]+" '{print $1}'`
NETWORK_PATH="/sys/class/net"
NETWORK_NAME=`ls $NETWORK_PATH |grep -v "lo"`
network_mac() {
echo "current network card name/ip/mac have:"
for i in $NETWORK_NAME;do
NETWORK_MAC=`cat /sys/class/net/$i/address`
NETWORK_IP=`ip addr|grep $i|grep -w inet|awk -F "[ /]+" '{print $3}'`
if [ -z $NETWORK_IP ];then
NETWORK_IP="\033[25;31mThe network card not have ip\033[0m"
fi
echo -e "\033[25;32m$i --- $NETWORK_IP\033[0m \033[25;32m--- $NETWORK_MAC\033[0m"
done
}
case $OS_release in
6)
echo "This is 6"
network_mac
;;
7)
echo "This is 7"
network_mac
;;

*)
echo -e "\033[25;31mcurrent OS release is $OS_release, not in scope\033[0m"
esac

posted @ 2019-03-20 14:56  浩0x208哥  阅读(970)  评论(0编辑  收藏  举报