获取网卡信息

获取网卡信息

获取网卡名:
#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值