Automatically restart Logmein Hamachi

Jun 24, '14 07:00:00AM

Contributed by: Anonymous

If you are having trouble with Logmein Hamachi starting up correctly, the following script will check to see if the connection is up. If it is, it will attempt to restart and then send you an email when it's done.

You'll need to update these variables with your own data:

email_address='XXXXXXX@gmail.com'

hamachi_network="xxx-xxx-xxx"
Also update the machine names and IP address (e.g. test_ip_address) in the case statement.

You can use the command hamachi list (from a Terminal window) to get your network ID and IP addresses.

Once you set this up, you can run this from any of your connected machines and it will try to connect to the other machine. If it cannot, it attempts to get Hamachi working again.

Here's the script:
#!/bin/bash
# Script to restart the Hamachi connection if it is not working
#
# user command "hamachi list" to find the hamachi network you are connecting to
#
 
email_address='XXXXXXX@gmail.com'
hamachi_network="xxx-xxx-xxx"
public_ip_address='google.com'
 
#
 
machine_name=`uname -n`
case $machine_name in
 
        Mac1.local)  test_ip_address="25.xxx.xxx.xxx"
                        test_machine="Mac1"
                        ;;
 
        Mac2.local) test_ip_address="25.xxx.xx.xx"
                        test_machine="Mac2"            
                        ;;      
        *) echo "You are using an unknown machine, named [$machine_name]. Exiting"
           exit
        ;;
esac
 
echo "Checking Logmein Hamachi network connectivity..."
echo "You are using [$machine_name]. Checking IP Address [$test_ip_address] on [$test_machine]"
IS=`/sbin/ping -c 5 $public_ip_address 2> /dev/null | grep -c "64 bytes"`
if (test "$IS" -eq  "0") then
   echo "Your internet connection does not appear to be working. Aborting check"
   exit
fi
 
IS=`/sbin/ping -c 5 $test_ip_address 2> /dev/null | grep -c "64 bytes"`
if (test "$IS" -gt "2") then
   echo "Your Logmein Hamachi connection appears to be working."
   exit
else
   echo "There appears to be a problem with your Logmein Hamachi connection."
   echo "I will check again in 10 seconds..."
   sleep 10
   IS=`/sbin/ping -c 5 $test_ip_address 2> /dev/null | grep -c "64 bytes"`
   if (test "$IS" -gt "2") then
       echo "Your Logmein Hamachi connection appears to be working now."
       exit
   else
       echo "There is still a problem with your Logmein Hamachi connection. Attempting to fix by restarting Logmein Hamachi..."
       hamachi logout
       sleep 10
       hamachi login
       sleep 10
       hamachi go-offline $hamachi_network
       sleep 10
       hamachi go-online $hamachi_network
       echo "OK, we should be back up!"
       echo "Your Logmein Hamachi connection on $machine_name needed to be cycled on/off.
 
Could not connect to [$test_ip_address].
 
It may also mean that $test_machine is down." | mail -s "Hamachi Connection Down on $(date '+%m/%d/%y @ %H:%M:%S')" $email_address -f ipdown@no-reply.com -F "Hamachi Connection Problem on $machine_name"
   fi
fi

[crarko adds: I haven't tested this one.]

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20140622113515722