Script list-network-interfaces

Para listar cada una de las interfaces de red de un equipo, junto con su IP y MAC en una línea por interfaz, podemos usar el siguiente script de roaima:

#!/bin/bash
# ------------------------------------------------------------
# script:  /usr/local/sbin/list-network-interfaces
#
# Purpose: List every interface name, ip, and mac in one line

ip addr show |
    awk '
        # Output function to format results (if any)
        function outline() {
            if (link>"") {printf "%s %s %s\n", iface, inets, link}
        }

        # Interface section starts here
        $0 ~ /^[1-9]/ {
            outline();                              # Output anything we previously collected
            iface=substr($2, 1, index($2,":")-1);   # Capture the interface name
            inets="";                               # Reset the list of addresses
            link=""                                 # and MAC too
        }

        # Capture the MAC
        $1 == "link/ether" {
            link=$2
        }

        # Capture an IPv4 address. Concatenate to previous with comma
        $1 == "inet" {
            inet=substr($2, 1, index($2,"/")-1);    # Discard /nn subnet mask
            if (inets>"") inets=inets ",";          # Suffix existing list with comma
            inets=inets inet                        # Append this IPv4
        }

        # Input processing has finished
        END {
            outline()                               # Output remaining collection
        }
    '

Ejemplo de uso:

list-network-interfaces 
eth1  00:1b:21:2a:b5:ef
enp2s0  00:1b:21:2a:b5:ef
enp3s0  00:1b:21:2e:68:8c
eth0  00:1b:21:2e:68:8c
eth2 192.168.0.16 90:1b:0e:08:20:b4
bond0 172.19.144.14 00:1b:21:2e:68:8c
bond1 192.168.100.16 00:1b:21:2a:b5:ef
Be the first to comment

Por favor acceder para comentar.