Simple Linux Network interface configuration mapper
Tags: linux, awkprogramming, devops, network
Published: 2021-09-04 16:26:21

There are many questions to answer when it comes to a physical machine-related Network interface configuration that has multiple interfaces for individual use. 

. Which card is active?
. Is that part of bonding?
. What is its mac address?
. What is its permanent mac if part of Bonding?
. What's the speed of device and what is its state now?
. What all ip address its holding now ? 

While ifconfig provides you some info , new 'ip' command does it much more better yet don't have all the representations. Then you end up looking at some other config files/device files to further map information.

> Give this a try, Just copy and run. 

To keep it simple and portable as possible for admins I did it in my fav awk programming.

###### Sample output
```bash
#awk -f Netinfo.awk
Filter IPV4(-4) : No
______________________________________________________________________________________________________________
Device     Name        Bond        Mac                     Mac(perm)           State       IP/Net
______________________________________________________________________________________________________________  
/          lo                                                                  UNKNOWN()   127.0.0.1/8
02:01      eno1        bond0       92:6a:64:43:d5:c1      92:6a:64:43:d5:c1    UP(10G) 
02:02      eno2        bond0       92:6a:64:43:d5:c1      92:6a:64:43:d5:c5    UP(10G)
02:03      eno3                    92:6a:64:12:d3:12                           DOWN(10G)   192.168.0.11/24,fdb4:a9d0:15e6:a052::/64
           bond0                   92:6a:64:12:d3:13                                        192.168.0.12/24,192.168.0.20/24
_____________________________________________________________________________________________________________
```

 
###### Code QuickView 
```javascript
#!/usr/bin/awk -f 
#Code On Github: https://github.com/vkg23/Netinfo.awk
#VipinkG/Vkg23/awk/LinuxNetinfoMapper Netinfo.awk Version:5 
#About Compatiblities/Procedures and RoadMaps : Refer to Github Page 

function checkFile(filename) {
if( system( "test -f "filename) == 0 ) {return 0} else {return 1}
}


function foo() {
    if(intname!~/bond/) {
        devfile="/sys/class/net/"intname"/device"
        cmdsub="ls -l "devfile " 2> /dev/null"
        cmdsub|getline devlink
        close(cmdsub)
        split(devlink,tmp,"/");
        a1[intname,"pci"]="/"tmp[length(tmp)]
        speedfile="/sys/class/net/"intname"/speed"
        if ((getline speed < speedfile) > 0) {
            speed=speed/1000
            a1[intname,"speed"]=speed"G";
    }
    else {}
    }
}

BEGIN {
    id=0

    if (checkFile("/usr/sbin/ip") == 0) {ipcmd="/usr/sbin/ip" }
    else if (checkFile("/sbin/ip") == 0) {ipcmd="/sbin/ip"}
    else {print " Error! IP Command not found [/usr/sbin/ip or /sbin/ip], Check if \"iproute\" rpm exists?" ; exit}

    cmd=ipcmd" -o link show" ;
    while ((cmd|getline) > 0){
        intname=$2
        id++
        gsub(/:$/,"",intname)
        intArray[id]=intname;
        a1[intname,"name"]=intname;
        a1[intname,"ip"];
        foo()
        if(intname!~/bond/) {if ($0~/bond[0-9]*/) {
            #print "Part of Bond, So reading slave perm address."
				ethfile="/sys/class/net/"intname"/bonding_slave/perm_hwaddr"

            getline pmac < ethfile
            a1[intname,"pmac"]=pmac;
        }
        }

        for (i=3;i<=NF;i++){
            if($i~/link\/ether/){a1[intname,"mac"]=$(i+1)
            }
            if($i~/state/){a1[intname,"state"]=$(i+1);
            }
            if($i~/bond[0-9]*/){
            a1[intname,"bond"]=$(i);
            }
            }
    }    
    close(cmd)
    if (ARGV[1]=="-4") {
    print "Filter ipv4(-4):","Yes"
    cmd=ipcmd" -o -4 addr show"
    } else {
       print "Filter ipv4(-4):","No"
       cmd=ipcmd" -o addr show" ;
    }
    while ((cmd|getline) > 0){
        intname=$2
        gsub(/:$/,"",intname)
        if ($3~/inet/) {
            if (a1[intname,"ip"]==""){
                a1[intname,"ip"]=$(4)
            } else {
                a1[intname,"ip"]=a1[intname,"ip"]","$(4)
            }
        }
    }
    close(cmd)
    print "____________________________________________________________________________________________________________________"
    
	printf "%15s\t%6s\t%5s\t%20s\t%20s\t%10s\t%18s\n","Device","Name","Bond","Mac","Mac(perm)","State","Ip/Net"
    print "____________________________________________________________________________________________________________________"
    for (x=1;x<=length(intArray);x++) {
        i=intArray[x]
        state=a1[i,"state"]"("a1[i,"speed"]")"
        printf "%15s\t%6s\t%5s\t%20s\t%20s\t%10s\t%18s\n",a1[i,"pci"],a1[i,"name"],a1[i,"bond"],a1[i,"mac"],a1[i,"pmac"],state,a1[i,"ip"]
    
	}
    print "_____________________________________________________________________________________________________________________"
    print "Press Enter to Exit!"
    exit
	}
{print "Done!"; exit 0}
END{}
#End of Code

```

###### Code Dowload/Fork
Refer to my Github page for more details.
Link [Github/vkg23/Netinfo.awk](https://github.com/vkg23/Netinfo.awk)

> Tested for RHEL/CENTOS 6/7/8 Versions.

Hope it helps the Linux Community. 

*Cover image: unsplash/@__itsflores*