一句話腳本系列之獲取eth0網卡的IP地址(或MAC地址)

歡迎轉載!轉載時請註明出處:http://blog.csdn.net/nfer_zhuang/article/details/42609733

引言

使用場景:

我使用的Linux服務器上有多個網卡,有時候就需要在腳本中使用到指定網卡的IP地址或MAC地址,那麼這個時候就有兩種做法,一是預先讀取網卡的IP地址或MAC地址,然後寫入到腳本中;另外一種方法就是通過shell命令實時獲取指定網卡上的當前IP地址或MAC地址。

一般用法:
  1. 使用ifconfig eth0讀取網卡信息

  2. 手動將IP地址或MAC地址拷貝出來,並填入到腳本中的對應變量中

一句話腳本用法:

ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 6-  (獲取eth0網卡的IP地址)
或
ifconfig eth0 | grep "HWaddr" | awk '{print $5}'  (獲取eth0網卡的MAC地址)
上面的腳本分解步驟是:
  1. 獲取eth0網卡的信息
  2. 過濾出IP地址的行或MAC地址的行
  3. 使用awk輸出指定字段,對於MAC地址,第5個字段就是MAC;而對於IP地址,還需要對第2個字段截取第6個字符之後的內容

ifconfig命令部分說明

先看一下ifconfig的man手冊中的描述:

       Ifconfig  is  used  to  configure  the  kernel-resident network interfaces.  
       If  no  arguments  are  given,  ifconfig  displays the status of the currently active interfaces.  If a single
       interface argument is given, it displays the status of the given interface only; if a single  -a  argument  is
       given, it displays the status of all interfaces, even those that are down. 

ifconfig命令的主要作用是進行網卡配置,但是如果沒有給定參數或者只是給定了網卡名稱這一個參數,則就會顯示網卡的狀態信息。

因此,在這裏,我們通過參數eth0來指定輸出該網卡的信息

grep命令部分說明

我們先看一下ifconfig的輸出格式:

eth0      Link encap:Ethernet  HWaddr 08:00:27:f6:18:8e  
          inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fef6:188e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12575 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3429 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1931585 (1.9 MB)  TX bytes:465667 (465.6 KB)

其中紅色部分中包含了MAC地址信息,藍色部分包含了IPv4的地址信息。而如果我們指定的網卡不存在時,則輸出信息如下:

nfer@nfer:~$ ifconfig eth1
eth1: error fetching interface information: Device not found

因此在上面的命令中,我們分別使用grep "inet addr:"和grep "HWaddr"來過濾出IPv4的地址和MAC地址所在的行,如果是該網卡不存在或者不處於活動狀態就過濾不到對應的行。

awk命令和cut命令部分說明

在上面的grep命令過濾出來的MAC地址和IPv4地址所在行的格式如下:

eth0      Link encap:Ethernet  HWaddr 08:00:27:f6:18:8e  
          inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0

因此,如果是獲取MAC地址,只需要使用awk輸出第5個字段的值即可:awk '{print $5}';

而如果是要獲取IPv4的地址,則需要先輸出第2個字段的值:awk '{print $2}',然後再使用cut命令,將"addr:"這5個字符去除,即從第6個字符到結尾的所有字符:cut -c 6-。

其中cut命令的-c參數以及後面的需要顯示的字符列表的表述方式的描述如下:

       -c, --characters=LIST
              select only these characters


       Use one, and only one of -b, -c or -f.  Each LIST is made up of one range, or many ranges separated by commas.
       Selected  input  is written in the same order that it is read, and is written exactly once.  Each range is one of:
       N      N'th byte, character or field, counted from 1
       N-     from N'th byte, character or field, to end of line
       N-M    from N'th to M'th (included) byte, character or field
       -M     from first to M'th (included) byte, character or field

我們這裏是按照字符操作的,所以使用了-c參數;需要顯示的是從第6個字符到結尾的部分,所以使用了N-的模式表示LIST。

總結

本次一句話腳本使用到了以下知識:

  1. ifconfig命令
  2. grep命令
  3. awk命令
  4. cut命令的-c參數


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章