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

Mac OS X – Get Network Information

Posted on February 27, 2017May 2, 2017 By David Kittell

Every once in a while you need to get some basic network information from your Mac or a Mac you are supporting, this script below will help get you some helpful information.

#!/bin/sh

clear

sExternalMACALService="http://dns.kittell.net/macaltext.php?address="

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

# 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) ))
}

# Get remote/public IP address
remoteip=$(dig +short myip.opendns.com @resolver1.opendns.com)

# Get computer name
computername=$(scutil --get ComputerName)

# Get serial number
sSerialNumber=$(system_profiler SPHardwareDataType |grep "Serial Number (system)" |awk '{print $4}'  | cut -d/ -f1)
#echo $sSerialNumber

# Get operating system name and version - Start
OSvers1=$( sw_vers -productVersion | cut -d. -f1 )
OSvers2=$( sw_vers -productVersion | cut -d. -f2 )
OSvers3=$( sw_vers -productVersion | cut -d. -f3 )
case $OSvers2 in
    8)
        OSName="Mountain Lion"
    ;;
    9)
        OSName="Mavericks"
    ;;
    10)
        OSName="Yosemite"
    ;;
    11)
        OSName="El Capitan"
    ;;
    12)
        OSName="Sierra"
    ;;
    default)
        OSName="Unknown"
    ;;
esac
# Get operating system name and version - Stop

echo "$computername"
echo "--------------"
echo "      Computer OS:  Mac OS X - $OSName $OSvers1.$OSvers2.$OSvers3"
echo "    Computer Name:  $computername"
echo "Current User Name:  $(whoami)"
echo "    Serial Number:  $sSerialNumber"

if [[ $remoteip ]]; then
    echo "Remote IP Address:  $remoteip\n"
else
    echo "Remote IP Address:  Unable To Determine\n"
fi

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

    if [ "$activated" == "active" ]; then
        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)"
                echo "--------------"

                # 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"
                fi

                echo "       IP Address:  $ipaddress"
                echo "      Subnet Mask:  $netmask"
                echo "           Router:  $router"
                echo "          IP CIDR:  $ipaddress/$(mask2cdr $netmask)"

                if [[ -z $dnsserver ]]; then
                    if [[ $DHCPActive ]]; then
                        echo "       DNS Server:  Set With DHCP"
                    else
                        echo "       DNS Server:  Unknown"
                    fi
                else
                    echo "       DNS Server:  $dnsserver"
                fi

                echo "      MAC-address:  $macaddress ($macal)"
                echo "    Network Speed:  $networkspeed"
                echo "     Link quality:  $quality"
                echo " "
            fi

            # Don't display the inactive ports.
        fi
    fi
done
Originally Posted on February 27, 2017
Last Updated on May 2, 2017
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

Code Mac OS X Shell Mac OSX UNIX UNIX Shell Scripts CIDRIP AddressNetworkNetwork Information

Post navigation

Previous post
Next post

Related Posts

FormatDate Function

Posted on June 10, 2014October 26, 2015

public string FormatDate(string sDate) { string formatteddate = ""; try { //DateTime d = Convert.ToDateTime(Date); DateTime d = DateTime.Parse(sDate); string format = "MM/dd/yyyy"; formatteddate = d.ToString(format); return formatteddate; } catch { return formatteddate; } } Originally Posted on June 10, 2014Last Updated on October 26, 2015 All information on this…

Read More

Mac OSX Terminal – Get Active Interface

Posted on June 23, 2016March 6, 2017

This will let you know which network interface you are actively using should you have Wi-Fi and Ethernet connected. route get default | grep interface | awk ‘{print $2}’ Get name of network interface ActiveNetwork=$(route get default | grep interface | awk ‘{print $2}’) ActiveNetworkName=$(networksetup -listallhardwareports | grep -B 1…

Read More

PowerShell – Battery Charge Status

Posted on November 9, 2015

Add-Type -AssemblyName System.Windows.Forms [Windows.Forms.PowerStatus].GetConstructor( [Reflection.BindingFlags]36, $null, [Type[]]@(), $null ).Invoke($null) Reference: http://poshcode.org/5687 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…

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
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes