Jawad Al Shaikh Jawad Al Shaikh - 4 months ago 21
Linux Question

Optimizing bash script that extract substring based on MAC address matching

Client requires 50 CentOS 7 Hostname to be named based on NIC MAC address:

currently I combined below bash script but seems a lot of optimization can be done, the script will be launched on Image first boot only, it must be stable and tight:

for i in `ip a |awk '/ether/ {print toupper($2) "_"}' | sed -r 's/:/-/g'`; do grep -i $i* MAC_HOST | awk -F _ '{print $2}'; done


MAC_HOST contents:

22-8A-07-4F-16-A8_BT001
22-8A-07-5C-0F-58_BT002
22-8A-07-5C-0F-5B_BT003
22-8A-07-5D-D1-3A_BT004
22-8A-07-5D-D1-3D_BT005
22-8A-07-5D-D1-4C_BT006
22-8A-07-5D-D1-52_BT007
22-8A-07-5D-D1-58_BT008
22-8A-07-5D-D1-5B_BT009
22-8A-07-5D-D1-5E_BT010
22-8A-07-5D-D1-61_BT011
22-8A-07-5D-D1-67_BT012
22-8A-07-5D-D1-7C_BT013
22-8A-07-5D-D1-82_BT014
22-8A-07-5D-D1-8E_BT015
22-8A-07-5D-D1-91_BT016
22-8A-07-5E-2F-4D_BT017
22-8A-07-5E-2F-5C_BT018
22-8A-07-5E-2F-65_BT019
22-8A-07-5E-2F-7D_BT020
22-8A-07-5F-36-65_BT021
22-8A-07-5F-36-9B_BT022
22-8A-07-5F-36-A1_BT023
22-8A-07-5F-36-A4_BT024
22-8A-07-5F-36-A7_BT025
22-8A-07-5F-36-AA_BT026
22-8A-07-5F-36-AD_BT027
22-8A-07-5F-36-B0_BT028
22-8A-07-5F-36-B3_BT029
22-8A-07-5F-36-B6_BT030
22-8A-07-5F-36-BC_BT031
22-8A-07-5F-D8-0B_BT032
22-8A-07-5F-D8-0E_BT033
22-8A-07-5F-D8-11_BT034
22-8A-07-5F-D8-14_BT035
22-8A-07-5F-D8-17_BT036
22-8A-07-5F-D8-1A_BT037
22-8A-07-5F-D8-1D_BT038
22-8A-07-5F-D8-20_BT039
22-8A-07-5F-D8-26_BT040
22-8A-07-5F-D8-29_BT041
22-8A-07-5F-D8-2F_BT042
22-8A-07-5F-D8-32_BT043
22-8A-07-5F-D8-35_BT044
22-8A-07-5F-D8-38_BT045
22-8A-07-5F-D8-3B_BT046
22-8A-07-5F-D8-3E_BT047
22-8A-07-5F-D8-41_BT048
22-8A-07-5F-D8-44_BT049
22-8A-07-5F-D8-47_BT050


note: append below "before ;done" to write the found Hostname. I remove it so no one overwrite his/her hostname by mistake.


| cat >> /etc/hostname

Answer

I think that this does what you want and it eliminates all shell looping and a lot of processes:

ip a | awk 'FNR==NR{a[$1]=$2;next} /ether/{gsub(/:/, "-", $2); print a[toupper($2)]}' FS=_ MAC_HOST FS=" "  -

How it works

  • FNR==NR{a[$1]=$2;next}

    This reads in the contents of the first file, MAC_HOST, and stores it in associative array a.

    Note that, regardless of how many interfaces are returned by ip a, the file MAC_HOST is read only once. Further, associative arrays have fast look-up.

  • /ether/{gsub(/:/, "-", $2); print a[toupper($2)]}

    This selects the lines containing ether and processes the second field

  • FS=_ MAC_HOST FS=" " -

    This sets the field separator to _ when reading MAC_HOST and then sets it to a space when reading stdin (which comes from ip a).

    Note that, in awk, FS=" " is a special case: it means that any sequence of one or more whitespace characters is treated as a field separator.

Multiline version

The same code, when spread over multiple lines, is:

ip a | awk '

  FNR==NR{
    a[$1]=$2
    next
  }

  /ether/{
    gsub(/:/, "-", $2)
    print a[toupper($2)]
  }
  ' FS=_ MAC_HOST FS=" "  -

Variation: ignoring non-matching MAC addresses

What do we do when ip a returns a MAC address that is not listed in MAC_HOST? The version above prints a blank line. The version below prints nothing when the MAC_HOST entry is missing:

ip a | awk 'FNR==NR{a[$1]=$2;next} {b=""} /ether/{gsub(/:/, "-", $2); b=a[toupper($2)]} b {print b}' FS=_ MAC_HOST FS=" "  -

As an example, the fake input here provides two MAC addresses, one in MAC_HOST, and one not:

$ printf 'ether 22:8a:07:5f:36:a4\n\nether 22:8a:07:5f:36:a5\n\n' | awk 'FNR==NR{a[$1]=$2;next} {b=""} /ether/{gsub(/:/, "-", $2); b=a[toupper($2)]} b {print b}' FS=_ MAC_HOST FS=" "  -
BT024

To use the above to overwrite /etc/hostname:

ip a | awk 'FNR==NR{a[$1]=$2;next} {b=""} /ether/{gsub(/:/, "-", $2); b=a[toupper($2)]} b {print b}' FS=_ MAC_HOST FS=" "  - >/etc/hostname
Comments