13 June 2010

Hamachi VPN Watchdog (KeepAlive) for Linux (Ubuntu 9.10 and likely 9.04, 10.04, etc.)

UPDATE: there is a new version at http://computarz.blogspot.com/2010/06/updated-hamachi-keep-alive-script.html

I just finished writing a script for keep LogMeIn Hamachi online.

You can get a script for installing and configuring hamachi at: http://www.webupd8.org/2010/05/script-to-install-hamachi-with-gui-in.html
#!/bin/bash

if [ `ip r | grep default -c` -lt 1 ]; then
  # No internet gateway, exiting!
  exit 1
fi

hamachi_online=`hamachi list | egrep '\* [0-9]' | awk '{print $2}'`

me_online=0
for IP in $hamachi_online; do
  ping -c 1 $IP >/dev/null
  if [ $? -eq 0 ]; then me_online=1; break; fi
done

if [ $me_online -eq 0 ]; then
  echo "hamachi is offline - restarting"
  if [ `pgrep -c tuncfg` -eq 0 ]; then sudo /sbin/tuncfg; fi
  hamachi stop >/dev/null
  sleep 1
  for ham_pid in `pgrep -f hamachi -l | grep -v hamachi_watchdog.sh | awk '{print $1}'`; do
      kill -9 ham_pid
  done
  hamachi start
  CNT=1;
  echo -n "Logging in... " ;
  while [ `hamachi login | grep -c failed` -eq 1 -a $CNT -lt 100 ]; do 
      echo -n "$CNT "; 
      let CNT=$CNT+1; 
      sleep 2; 
  done;
  echo
  hamachi get-nicks
#else
  #echo "hamachi is online"
fi
I configured cron to run this script every 5 minutes, by running in terminal:
crontab -e

and then adding this line:
# m h  dom mon dow   command
*/5 * * * * $HOME/bin/hamachi_watchdog.sh

No comments: