#!/bin/bash

### Curby's GeekTool network diagnostic script
### by Michael 'Curby' Lee
### v1.00.00, 2007-02-17

###   Description
#
#  Outputs context-sensitive network information such as primary
#  interface, Wifi network name if applicable, and primary IP address
#  and reverse DNS name if available.  It also performs simple
#  reachability tests and DNS tests.  It was written to fail
#  gracefully and quickly even in the absence of network connectivity.
#

###   Suggested use
#
#  This script can be executed from the OS X commandline without
#  administrative priveleges, but is intended for use with GeekTool
#  with a refresh time of at least 10 seconds.  This script was
#  tested on OS X v10.4.8 with GeekTool v2.1.2(112).
#

###   License
#
#  Copyright (C) 2007  Michael Lee <kirbysdl@hotmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#



#       ========================================================================
#       Configuration
#       ========================================================================
ping_one='128.111.1.1'
ping_two='192.228.79.201'
dns_host='a.root-servers.net'



#       ========================================================================
#       Data Collection
#       ========================================================================
# Routing and interface determination
route=$(netstat -r -n -f inet | grep '^default')
if [[ $? -ne 0 ]]; then
  iface='n/a'
  my_addy='n/a'
  gateway='n/a'
else
  iface=$(echo $route | awk '{print $6}')
  my_addy=$(ifconfig $iface | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2)
  if [[ $iface == 'en1' ]]; then
    wifi='("'$(system_profiler SPAirPortDataType | grep -e "Current Wireless Network:" | awk '{print $4}')'" WIFI Network)'
  fi
  gateway=$(echo $route | awk '{print $2}')
  gate_reach='('$(scutil -r $gateway | sed 's/Directly Reachable Address/ Directly/')')'
fi

# ping tests
ping_test=$(ping -t 1 -c 1 $ping_one | grep 'bytes from' 2>/dev/null)
if [[ $? -ne 0 ]]; then
  ping_result='ping failed'
else
  ping_result=$(echo $ping_test | cut -d= -f 4)
fi
ping_test=$(ping -t 1 -c 1 $ping_two | grep 'bytes from' 2>/dev/null)
if [[ $? -ne 0 ]]; then
  ping2_result='ping failed'
else
  ping2_result=$(echo $ping_test | cut -d= -f 4)
fi

# DNS tests
dns_test=$(dig +time=1 +tries=1 $dns_host | grep 'Query time' 2>/dev/null)
if [[ $? -ne 0 ]]; then
  dns_result='lookup failed'
else
  dns_result=$(echo $dns_test | cut -d\  -f 4-)
  my_dns=$(host $my_addy)
  if echo $my_dns | grep -q NXDOMAIN; then
    my_dns=""
  else
    my_dns=\($(echo $my_dns | sed 's/^.* \(.*\)./\1/')\)
  fi
fi



#       ========================================================================
#       Output
#       ========================================================================
echo Network Status
echo --------------
echo '   'Interface: $iface $wifi
echo '   'Address:'   '$my_addy $my_dns
echo
echo Connectivity
echo --------------
echo '   'Gateway:'   '$gateway $gate_reach
echo '   'Ping Test: $ping_one \($ping_result\)
echo '              '$ping_two \($ping2_result\)
echo '   'DNS Test:'  '$dns_host \($dns_result\)
