UNIX – Display Network Information

| | | | | | | | |

The following code will give you network specific information about your server/computer.

Red Hat/CentOS/Fedora along with Debian/Ubuntu and Mac OS X options below.

At some point if I have a need (more than one adapter) on a Red Hat or Debian server/computer I may build it out like the Mac OS X version.

declare OSVer=$(cat /etc/redhat-release)
declare netAdapter=$(nmcli device status | grep en | cut -d " " -f1)
if [ -z "$netAdapter" ]; then
netAdapter=$(nmcli device status | grep eth | cut -d " " -f1)
fi
declare netIP=$(/sbin/ip -o -4 addr list $netAdapter | awk '{print $4}' | cut -d/ -f1)
#declare netCIDR=$(/sbin/ip -o -4 addr list $netAdapter | cut -d ' ' -f7)
declare netMask=$(ipcalc -m $netIP | cut -d '=' -f2)
declare netCIDR=$(ipcalc -p $netIP $netMask | cut -d '=' -f2)
declare netWork=$(ipcalc -n $netIP $netMask | cut -d '=' -f2)
declare banner=$(cat <<EOF
$OSVer
       Hostname: $(hostname)
Network Information
        Adapter: $netAdapter
             IP: $netIP
        Netmask: $netMask
           CIDR: $netWork/$netCIDR


EOF
)
echo "$banner"
# Install IPCalc
sudo apt-get install ipcalc

# Install Network Manager (nmcli)
sudo apt-get install network-manager

#lsb_release -a
OS=$(lsb_release -i | cut -d ":" -f2 | tr -d '[:space:]')
OSCode=$(lsb_release -c | cut -d ":" -f2 | tr -d '[:space:]')
OSVer=$(lsb_release -r | cut -d ":" -f2 | tr -d '[:space:]')
#echo "$OS $OSCode $OSVer"

# Network Variables - Start
declare netAdapter=$(nmcli device status | grep en | cut -d " " -f1)
if [ -z "$netAdapter" ]; then
netAdapter=$(nmcli device status | grep eth | cut -d " " -f1)
fi
#declare netAdapter=$(ip -o link show | awk '{print $2,$9}' | grep UP | cut -d ":" -f1)
#echo $netAdapter

declare netIP=$(/sbin/ip -o -4 addr list $netAdapter | awk '{print $4}' | cut -d/ -f1)
#echo $netIP

#declare netMask=$(ipcalc -m $netIP | cut -d '=' -f2)
declare netMask=$(ifconfig "$netAdapter" | sed -rn '2s/ .*:(.*)$/\1/p')
declare netCIDR=$(ipcalc $netIP/$netMask | grep "Netmask:" | cut -d "=" -f2 | cut -d " " -f2 | tr -d '[:space:]')
declare netWork=$(ipcalc $netIP/$netMask | grep "Network:" | cut -d "/" -f1 | cut -d " " -f4 | tr -d '[:space:]')
declare banner=$(cat <<EOF
$OS $OSCode $OSVer
       Hostname: $(hostname)
Network Information
        Adapter: $netAdapter
             IP: $netIP
        Netmask: $netMask
           CIDR: $netWork/$netCIDR


EOF
)
echo "$banner"
# Get IP - Start
sExternalMACALService="http://dns.kittell.net/macaltext.php?address="

# Function to convert IP Subnet Mask to CIDR
mask2cdr ()
{
# Assumes there's no "255." after a non-255 byte in the mask
local x=${1##*255.}
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
x=${1%%$3*}
echo $(( $2 + (${#x}/4) ))
}

# List all Network ports
NetworkPorts=$(ifconfig -uv | grep '^[a-z0-9]' | awk -F : '{print $1}')
#echo $NetworkPorts

rm -rf ~/NetworkInfo.txt

for val in $(echo $NetworkPorts); do   # Get for all available hardware ports their status
    activated=$({ifconfig -uv "$(echo $val)" | grep 'status: ' | awk '{print $2}'})
    #echo $activated

    label=$(ifconfig -uv "$(echo $val)" | grep 'type' | awk '{print $2}')
    #echo $label

    #ActiveNetwork=$(route get default | grep interface | awk '{print $2}')
    ActiveNetworkName=$(networksetup -listallhardwareports | grep -B 1 "$label" | awk '/Hardware Port/{ print }'|cut -d " " -f3- | uniq)

    #echo $ActiveNetwork
    #echo $ActiveNetworkName

    state=$(ifconfig -uv "$val" | grep 'status: ' | awk '{print $2}')
    #echo $state

    ipaddress=$(ifconfig -uv "$val" | grep 'inet ' | awk '{print $2}')
    # echo $ipaddress

    if [[ -z $(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3, $4}' | sed 'N;s/\n/ up /' ) ]]; then
        networkspeed="$(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3}' ) up/down"
    else
        networkspeed="$(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3, $4}' | sed 'N;s/\n/ up /' ) down"
    fi
    #echo $networkspeed

    macaddress=$(ifconfig -uv "$val" | grep 'ether ' | awk '{print $2}')
    #echo $macaddress

    macal=$(curl -s "$sExternalMACALService$macaddress")
    #echo $macal

    quality=$(ifconfig -uv "$val" | grep 'link quality:' | awk '{print $3, $4}')
    #echo $quality

    netmask=$(ipconfig getpacket "$val" | grep 'subnet_mask (ip):' | awk '{print $3}' | tr -d '[:space:]')
    #echo $netmask

    router=$(ipconfig getpacket "$val" | grep 'router (ip_mult):' | sed 's/.*router (ip_mult): {\([^}]*\)}.*/\1/')
    #echo $router

    DHCPActive=$(networksetup -getinfo "Wi-Fi" | grep DHCP)
    #echo $DHCPActive

    dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | awk '{print $1, $2}' | sed 'N;s/\n//' )
    #echo $dnsserver

    if [[ ! -z "$netmask" ]]; then
        #echo "Network Port is Active"

        if [[ $ipaddress ]]; then
            echo "$ActiveNetworkName ($val)" >> ~/NetworkInfo.txt
            echo "--------------" >> ~/NetworkInfo.txt

            # Is this a WiFi associated port? If so, then we want the network name

            if [ "$label" = "Wi-Fi" ]; then
                WiFiName=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep '\sSSID:' | sed 's/.*: //')
                #echo $WiFiName
                echo "       Network Name: $WiFiName" >> ~/NetworkInfo.txt
            fi

            echo "         IP Address: $ipaddress" >> ~/NetworkInfo.txt
            echo "        Subnet Mask: $netmask" >> ~/NetworkInfo.txt
            echo "             Router: $router" >> ~/NetworkInfo.txt
            echo "            IP CIDR: $ipaddress/$(mask2cdr $netmask)" >> ~/NetworkInfo.txt

            if [[ -z $dnsserver ]]; then
                if [[ $DHCPActive ]]; then
                    echo "         DNS Server: Set With DHCP" >> ~/NetworkInfo.txt
                else
                    echo "         DNS Server: Unknown" >> ~/NetworkInfo.txt
                fi
            else
                echo "         DNS Server: $dnsserver" >> ~/NetworkInfo.txt
            fi

            echo "        MAC-address: $macaddress ($macal)" >> ~/NetworkInfo.txt
            #echo "      Network Speed: $networkspeed" >> ~/NetworkInfo.txt
            #echo "       Link quality: $quality" >> ~/NetworkInfo.txt
            echo " " >> ~/NetworkInfo.txt
        fi

        # Don't display the inactive ports.
    fi
done
cat ~/NetworkInfo.txt
# Get IP - Stop
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.