I am a huge fan of Geek Tool. A description, from their website, reads:

GeekTool is a PrefPane (System Preferences module) for Panther or Tiger to show system logs, unix commands output, or images (i.e. from the internet) on your desktop (or even in front of all windows).

Geek Tool has many, many uses, and one of the things I use it for is displaying a list of my current IP addresses. We could use the ifconfig command, but the data it spits out can be a lot of useless information - especially when we want something simple for our desktop, that we can garnish the required information at a glance. Enter this little gem:

#!/bin/bash
function pad {
	string="$1"; num="$2"; count=$(echo -n "$string" | wc -c); ((count += 0))
	while (( count  num )); do string=" $string"; ((count += 1)); done
	echo -n "$string"
}
idx=0; mode="media"; info=$(ifconfig | fgrep -B 2 netmask)
for i in $info; do
	case "$mode" in
		media)
			case "$i" in
				en0*)	media[$idx]="Ethernet"; mode="ip";;
				en1*)	media[$idx]="WiFi"; mode="ip";;
				en*)	media[$idx]=""; mode="ip";;
			esac;;
		ip)
			if [[ "$i" == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
				ip[$idx]="$i"; ((idx += 1)); mode="media"
			fi;;
	esac
done
for ((i=0; i  ${#media[*]}; i++)); do
	if [[ $ip ]]; then
		ip=$(pad ${ip[$i]:-Unknown} 15)
		echo "Internal  $ip    (via ${media[$i]})"
	fi
done
ip=$(wanip)
echo "External    $ip"
 

This simple script outputs your current IP address(es) with their known media type next to it. For example, you might get a returned value of "Internal 192.168.1.102 (via WiFi)". Say, for example, you have multiple IP addresses because you're on your WiFi and plugged into the ethernet, you would get an additional line below the first, with that connection's IP address as well. All the information you need in one tidy script.

Bonus!
If you have lynx installed (a text based web browser), you can use the following command to display your external IP address:

lynx -dump http://checkip.dyndns.org | cut -d : -f 2
 

If you don't have lynx, you can install it via port or fink.

  1. 1 Trackback(s)

  2. Dec 11, 2008: ./hadak » Blog Archive » The weather in your Geektool arsenal

You must be logged in to post a comment.