#!/bin/bash
ip addr show dev $1 2>/dev/null |grep 'NO-CARRIER' >/dev/null 2>&1
carr=$?
ip addr show dev $1 2>/dev/null |grep 'UP' >/dev/null 2>&1
updo=$?
MAC=`ip addr show dev $1 2>/dev/null |awk '/ether/{print $2}'`
#IPADDR=`ip addr show dev $1 2>/dev/null |awk -F'[ \/]+' '/inet /{print $3}'`
IPADDR=`ip addr show dev $1 2>/dev/null |sed -rn '/inet /s/inet ([^ ]*)\/.*/\1/p'|sed 'N;s/\n/ /g'`
NETMASK=`ip addr show dev $1 2>/dev/null |sed -rn '/inet /s/.*brd ([^ ]*) scope.*/\1/p'|sed 'N;s/\n/\t/g'`
if [ $# -lt 1 ]
then
echo "please input the device"
exit 1
fi
hang=`ip addr show dev $1 2>/dev/null|wc -l `
if [ "$hang" -eq 0 ]
then
echo " Device "$1" does not exist."
exit 3
fi
if [ $carr -eq 0 ]
then
if [ $updo -eq 0 ]; then
echo -e "$1 没接网线,启用状态\n mac地址为: $MAC \nip地址为: $IPADDR \n掩码地址为:$NETMASK"
else
echo -e "$1 没接网线,禁用状态\n mac地址为: $MAC\n ip地址为: $IPADDR\n掩码地址为:$NETMASK"
fi
else
if [ $updo -eq 0 ]; then
echo -e "$1 有接网线,启用状态 \nmac地址为: $MAC \nip地址为: $IPADDR \n掩码地址为:$NETMASK"
else
echo -e "$1 有接网络,禁用状态 \nmac地址为: $MAC \nip地址为: $IPADDR \n掩码地址为:$NETMASK"
fi
fi
root [ /home/kkg ]# ./kkg.sh eth0
eth0 有接网线,启用状态
mac地址为: 00:50:09:00:73:14
ip地址为: 192.168.129.254 192.168.140.254
掩码地址为:192.168.129.255 192.168.140.255
root [ /home/kkg ]# ./kkg.sh eth1
eth1 没接网线,启用状态
mac地址为: 00:50:09:00:73:15
ip地址为: 192.168.6.1 192.168.7.1
掩码地址为:192.168.6.255 192.168.7.255
root [ /home/kkg ]# ./kkg.sh eth2
eth2 有接网线,启用状态
mac地址为: 00:50:09:00:73:16
ip地址为: 59.57.251.51
掩码地址为:59.57.251.255
root [ /home/kkg ]# ./kkg.sh eth3
eth3 有接网络,禁用状态
mac地址为: 00:50:09:00:73:17
ip地址为:
掩码地址为:
root [ /home/kkg ]# ./kkg.sh eth4
Device eth4 does not exist.
root [ /home/kkg ]# ./kkg.sh
please input the device
阅读(1253) | 评论(0) | 转发(0) |