Advertisement
Guest User

arpmon for OSX

a guest
Jun 21st, 2012
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.24 KB | None | 0 0
  1. #!/bin/bash
  2. #This script monitors the MAC address of your default gateway and alerts you via Growl of changes
  3. function getmac {
  4. dg=$(netstat -rn | grep -Eo 'default.*([0-9]{1,3}\.){3}[0-9]{1,3}')     #grab the default gateway (DG)
  5. dg_ip=$(echo $dg | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}')          #strip the DG to just IP
  6. dg_arp=$(arp -a | grep -w $dg_ip)                                   #grab the arp entry for DG
  7. dg_mac=$(echo $dg_arp | grep -Eo '[0-9a-f]{1,2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}') #strip the ARP entry to just the MAC
  8. }
  9.  
  10. function stop {
  11. arpmon_pid=$(ps ax | grep arpmon | cut -d" " -f1) #find the PID for arpmon
  12. kill $arpmon_pid
  13. }
  14.  
  15. case "$1" in
  16.     start)
  17.         getmac #run the function to get the IP and ARP of the DG
  18.         kg_mac=$dg_mac #keep the initial ARP entry as the known good MAC
  19.  
  20.         while true;do
  21.             getmac #re-run the function to find the current MAC for the DG
  22.             if [ $dg_mac != $kg_mac ]; #has the MAC address changed
  23.  
  24.             then #if so run growlnotify
  25.                 growlnotify -p 2 -w -m "The MAC address for your Default Gateway ($dg_ip) has changed from $kg_mac to $dg_mac."
  26.             fi
  27.         sleep 5 #wait five seconds and rerun
  28.         done
  29. ;;
  30.  
  31.     stop)
  32.         stop
  33.         ;;
  34.  
  35.     *)
  36.         echo "Usage: arpmon {start|stop}"
  37.         exit 1
  38. esac
  39. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement