Network Services and IPs

I have long had a GeekTool item on my desktop that tells me what network interfaces were active and what IPs I have on them, as well as my external IP address.  For a long time this relied on ifconfig and manually updating the interface numbers (en0, en1, etc…).  Now that I have a Retina MBP, without an ethernet port, I have a lot of possible interfaces with the various adapters.  Managing the script manually was getting annoying.  So I rewrote it to use networksetup and a while loop.  The script is below:

#! /bin/bash
connection=false
services=$(networksetup -listallnetworkservices)
while read service; do
 ip=$(networksetup -getinfo "$service" | grep "IP address" | grep -v "IPv6" | awk '{print $3}')
 if [ "$ip" != "" ]; then
 echo "$service : $ip"
 connection=true
 fi
done <<<"$services"

astatus=$(networksetup -getinfo "Wi-Fi" | grep "IP address" | grep -v "IPv6" | awk '{print $3}')
if [ "$astatus" != "" ]; then
 NetName=`system_profiler SPAirPortDataType | awk '/Current Network Information:/ { found=NR } found && NR==found+1' | awk '{$1=$1};1' | cut -d":" -f1`
 Channel=`system_profiler SPAirPortDataType | awk '/Current Network Information:/ { found=NR } found && NR==found+4' | awk '{$1=$1};1' | sed 's/Channel/Ch/g'`
 echo "$NetName / $Channel"
fi

if [ $connection = false ]; then
 echo "No Connected Services"
else
 extIP=$(curl -s http://checkip.dyndns.org/ | sed 's/[a-zA-Z<>/ :]//g')
 if [ "$extIP" != "" ]; then
 echo "External IP: $extIP"
 else
 echo "No External Connection"
 fi
fi

The first line gets a list of all network services as defined in the current location in the Network panel of System Preferences, as well as a few other options (bluetooth, usb attached MiFis, etc).  Then for each one of those, we try to look up an IP address.  If there isn’t an IP, nothing is displayed, but if there is, then the service name and IP are output.

If the Wi-Fi service has an IP address, then the network name and channel that the system is connected to is found and output.

Finally, we use a external website to lookup the public IP that our outside connection is from.

[Edit] I added some checking so that if there is no connection, it will display that.  Also it will now display if there is no external connection [/Edit]

Currently, this is all for IPv4 addresses, and I explicitly exclude any IPv6 addresses that might be assigned, but that could be modified by changing the grep portions of the script.

Then in GeekTool I setup a Shell Script item and point it to my script.  That looks like this: GeekTool config

The output then is positioned in the lower left hand corner of my screen and looks like: GeekTool output

The next step is to use this as an excuse to try out Übersicht.

Advertisements

One thought on “Network Services and IPs

  1. Pingback: Update to network GeekTool script | My Thoughts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s