Skip to content

Instantly share code, notes, and snippets.

@ajitam
Created October 7, 2013 11:15
Show Gist options
  • Save ajitam/6866216 to your computer and use it in GitHub Desktop.
Save ajitam/6866216 to your computer and use it in GitHub Desktop.
WhereWhen
#!/bin/bash
#clear
# Ping to get ART table (in case there were no activity for some time)
/sbin/ping -t 1 255.255.255.255
#echo -----------------------
# Scan network and find UG (U - Up, G - Gateway)
netstat=$(/usr/sbin/netstat -nr | grep UG)
#echo Netstat output: $netstat
#echo -----------------------
# Get only IP address
gatewayip=$(echo $netstat | cut -d ' ' -f 2)
#echo Gateway IP address: $gatewayip
#echo -----------------------
arpoutput=$(echo "/usr/sbin/arp -an | grep '($gatewayip)'")
arpgateway=$(eval $arpoutput)
gatewaymac=$(echo $arpgateway | cut -d ' ' -f 4)
# Check if gateway MAC addres is empty
# => you are offline
if [ "$gatewaymac" == "" ]
then
gatewaymac="0:00:00:00:00:00"
fi
#echo Gateway MAC address: $gatewaymac
#echo -----------------------
# Get timestamp
timestamp=`date +%Y%m%d%H%M`
#echo $timestamp
#echo -----------------------
# Create input string (what will be witten to a file)
input=$timestamp#$gatewaymac
#echo $input
#echo -----------------------
# Write to file
echo $input >> /Users/user/Documents/WhereWhenLog.txt
#echo DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment