Welcome Reprint! When reproduced, please indicate the source:http://blog.csdn.net/nfer_zhuang/article/details/42609733
Introduction
Usage scenarios:
I use the Linux server has more than one network card, sometimes need to use the script to specify the IP address of the network card or MAC address, then there are two approaches, one is to pre-read the network card IP address or MAC address, and then write to the script Another way is to get the current IP address or MAC address on the specified network card in real time through the shell command.
General Usage:
Read NIC information using ifconfig eth0
- Manually copy the IP address or MAC address into the corresponding variable in the script
Sentence script usage:
Ifconfig eth0 | grep "inet Addr:" | awk ' {print $} ' | Cut-c 6- (get the IP address of the ETH0 network card) or ifconfig eth0 | grep "HWaddr" | awk ' {print $} ' (get the MAC address of the eth0 NIC)
The above script decomposition steps are:
- Get information about the eth0 NIC
- Line that filters out IP addresses or MAC addresses
- Using the awk output to specify the field, the 5th field is the Mac for the MAC address, and for the IP address, the 2nd field will be truncated after the 6th character
Ifconfig Command Section Description
First look at the description in the Ifconfig Man Handbook:
Ifconfig is used to configure the Kernel-resident network interfaces.
If no arguments is 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 is down.
The main function of the ifconfig command is to configure the network adapter, but if there is no parameter given or only given the name of the NIC, the status information of the NIC will be displayed.
So, here we specify the output of the NIC by the parameter eth0
The grep command section explains
Let's take a look at the output format of the 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)
Where the red part contains the MAC address information, the blue part contains the address information of the IPV4. And if the NIC we specify does not exist, the output information is as follows:
[Email protected]:~$ ifconfig eth1
Eth1:error fetching interface Information:device not found
So in the above command, we use grep "inet addr:" and grep "HWaddr" to filter out the address of the IPV4 and the line where the MAC address is located, if the network card does not exist or is not active to filter the corresponding row.
The awk command and cut command sections explain
The MAC address and IPV4 address in the above grep command are filtered in the following format:
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
Therefore, if you are getting a MAC address, you only need to use awk to output the value of the 5th field: awk ' {print $} ';
If you want to get the address of the IPV4, you need to first output the value of the 2nd field: awk ' {print $} ', and then use the Cut command to remove the "addr:" 5 characters, that is, all characters from the 6th character to the end: Cut-c 6.
The following is a description of the-c parameter of the Cut command, followed by the word list characters to be displayed:
-C,--characters=list
Select only these characters
Use one, and only one of-b,-C or-f. Each LIST was made up of one range, or many ranges separated by commas.
Selected input was written in the same order that it was 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
We do this here by character, so we use the-c parameter, we need to show the 6th character to the end of the section, so we use N-mode to represent list.
Summarize
This one-sentence script uses the following knowledge:
- Ifconfig command
- grep command
- awk command
- The-c parameter of the Cut command
Word of the script series get the IP address (or MAC address) of the eth0 NIC