获取网卡信息
获取网卡名:
#ifconfig | grep -o "^[^[:space:]]\{1,\}
”
获取eth0的ip地址:
#ifconfig eth0 | grep -o "inet addr:[0-9\.]\{1,\}" | cut -d: -f2
红帽7上的方法
#ifconfig eth0 | grep -o "inet [0-9\.]\{1,\}" | cut -d' ' -f2
不同的系统ifconfig得到的信息不同修改grep的内容来准确获取
获取指定IP的网卡名:
#ifconfig | grep -B 1 "192.168.0.99" | grep -o "^[^[:space:]]\{1,\}"
编写一个getinterface.sh脚本,可以接受选项{-i,-I,-a},完成以下功能:
1、使用以下形式:getinterface.sh [-i interface | -I IP | -a]
2、当用户使用-i时,显示其指定网卡的IP地址
3、当用户使用-I时,显示其指定IP地址的网络接口
4、当用户使用-a时,显示其所有的网络接口和其IP地址(除LO接口外)
#!/bin/bash
#
#Name:getinterface.sh
#Description:Get ethernet information
#Author:chen
#Version:0.0.1
#date time:2016-08-29 22:01:53
#Usage:getinterface.sh
SHOWIP (){
if ! ifconfig |grep -o "^[^[:space:]]\{1,\}"|grep $1 &> /dev/null;then
return 13
fi
echo -n "$1:"
ifconfig $1 | grep -o "inet [0-9\.]\{1,\}" | cut -d' ' -f2
}
SHOWETHER(){
if ! ifconfig | grep -o "inet [0-9\.]\{1,\}" | cut -d' ' -f2 | grep ^"$1"$ &> /dev/null;then
return 14
fi
echo -n "$1:"
ifconfig | grep -B 1 "$1" | grep -o "^[^[:space:]]\{1,\}"|cut -d: -f1-2
}
SHOWALL () {
ifconfig | grep -o "^[^[:space:]]\{1,\}"|grep -o "[a-z,A-Z,0-9].*[^:]" > ether.txt
while read LINE;do
if [ $LINE != "lo" ];then
SHOWIP $LINE
fi
done < ether.txt
rm -f ether.txt
}
USAGE () {
echo "getinterface.sh <-i interface | -I IP | -a >"
}
while getopts ":i:I:a" SWICH;do
case $SWICH in
i)
SHOWIP $OPTARG
[ $? -eq 13 ] && echo "Wrong ethercard"
;;
I)
SHOWETHER $OPTARG
[ $? -eq 14 ] && echo "Wrong IP"
;;
a)
SHOWALL
;;
*)
USAGE
;;
esac
done