Created
April 10, 2017 02:37
-
-
Save jnikolak/6106c9ea2408510eb9d19a5427153bd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lets break the full command down | |
ip -o link show eth0 | awk '{ for (x=1; x<=NF;x++) if ($x=="link/ether") print $(x+1)}' | |
############################################################ | |
# If we just run the ip -o link command against regular, it just adds the ip link into one line. | |
This is useful if you want to awk after | |
ip -o link show eth0 | |
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000\ link/ether 00:1a:4a:16:01:86 brd ff:ff:ff:ff:ff:ff | |
ip link show eth0 | |
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 | |
link/ether 00:1a:4a:16:01:86 brd ff:ff:ff:ff:ff:ff | |
############################################################### | |
The next code is an awk, it will take all of these arguments. | |
awk '{ for (x=1; x<=NF;x++) if ($x=="link/ether") print $(x+1)}' | |
To understand this command, we first have to understand the awk NF variable | |
-->: http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/?ref=binfind.com/web | |
As can be seen from the solution, NF is counting the fields. | |
########################################### | |
So...lets break down for (x=1; x<=NF;x++) | |
for (x=1 means: | |
While this statement is true | |
Set x to the value of 1. | |
x=1;x<=NF | |
if x is less than the total, add one to x) | |
So until the value of x reaches NF, the statement will be true and be able to execute. | |
########################################### | |
Next part: | |
if ($x=="link/ether") | |
If the value of x is equal to link ether field. | |
then | |
print $(x+1)}' | |
Which translates to print the value of variable x + 1 | |
After which we get the Mac Address: | |
ip -o link show eth0 | awk '{ for (x=1; x<=NF;x++) if ($x=="link/ether") print $(x+1)}' | |
00:1a:4a:16:01:86 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment