Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

UNIX – Display Network Information

Posted on March 6, 2017March 6, 2017 By David Kittell

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.

Related

CentOS Code Fedora Mac OS X Shell Mac OSX Raspberry Pi Red Hat Ubuntu UNIX UNIX Shell Scripts

Post navigation

Previous post
Next post

Related Posts

Powershell Download Script

Posted on September 11, 2015

# If you haven’t already done so you will need to run this to be able to execute PowerShell scripts # You will need to run PowerShell as administrator to run this script set-executionpolicy remotesigned Pass variables to the script <# .SYNOPSIS Downloads one file from a defined URL to…

Read More

Trim Function

Posted on August 8, 2013October 26, 2015

–FOR SQL SERVER 2000: CREATE FUNCTION dbo.TRIM(@string VARCHAR(8000)) RETURNS VARCHAR(8000) BEGIN RETURN LTRIM(RTRIM(@string)) END GO –FOR SQL SERVER 2005: CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX)) RETURNS VARCHAR(MAX) BEGIN RETURN LTRIM(RTRIM(@string)) END GO Source: http://blog.sqlauthority.com/2007/04/24/sql-server-trim-function-udf-trim/ Originally Posted on August 8, 2013Last Updated on October 26, 2015 All information on this site is shared…

Read More

NetSuite – SuiteScript 1.0 – Useful Functions

Posted on August 31, 2018

These first function snippets are from https://gist.github.com/W3BGUY // function to figure out the number of days in month. function getDaysInMonth(thisMonth,thisYear){ var monthArray=[31,28,31,30,31,30,31,31,30,31,30,31]; if(thisMonth!=2){return monthArray[thisMonth-1]}; if(thisYear%4!=0){return monthArray[1]}; if(thisYear%100==0 && thisYear%400!=0){return monthArray[1]}; return monthArray[1]+1; } // NetSuite usage example nlapiLogExecution(‘DEBUG’,getDaysInMonth(11,2016)) // function to add leading zeros on date parts. function zeroPad(num,len){ var…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes