
On occasion, Hamachi may be in a failed state on your always-on device. Alternately, maybe when awaking from sleep-mode, Hamachi may not be functional but still reporting online.
Thusly, it might be very useful for a cronjob to check the state and reset. This might especially be useful on your remote machines that you need connectivity to. For example maybe a remote machine that is in sleep mode, but WOL is possible from another remotely-accessible LAN device.
I have written a BASH script for checking Hamachi and forcing re-login if necessary. Maybe this script is not all-encompassing, but it’s a good start.
Let’s assume you have 2 or more client IP’s and also assume they are in an always-on state. (If for example you have 4 clients, but only 3 remain always-on, you will only use the 3 that you expect to be on.)
Below is my script which you will need to edit (IP addresses and hamachi network name). It will ping each hamachi neighbor and only reset if ALL are unreachable. Alternatively it will go-online-only if failures>0 and failures<neighbors.
This script uses bash installed from Entware-NG or Optware or Optware-NG. You will have to heavily modify this script if you prefer the built-in ash shell that is default with Synology.
This script uses bash installed from Entware-NG or Optware or Optware-NG. You will have to heavily modify this script if you prefer the built-in ash shell that is default with Synology.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/bin/bash # Synology (requires bash from entware-ng or optware[-ng]) | |
# #!/bin/bash # Other *nix | |
############# EDIT BLOCK ############## | |
array_ping_list=("25.1.2.3" "25.2.3.4" "25.3.4.5") #only the IP's expected always-on | |
array_network_list=("my.net" "my-other.net") #all your networks independent of IP's | |
log_file=~/check-hamachi.log | |
#### OS dependent config: | |
## Linux: | |
hamachi_service_stop="sudo /etc/init.d/logmein-hamachi stop" | |
hamachi_service_start="sudo /etc/init.d/logmein-hamachi start" | |
hamachi_tool=/usr/bin/hamachi | |
##--for synology, try: | |
# hamachi_service_stop="kill -15 $(ps | grep [h]amachid | awk '{print $1}')" | |
# hamachi_service_start="sh /opt/logmein-hamachi/hamachid.sh" | |
# hamachi_tool="sh /opt/logmein-hamachi/hamachi.sh" | |
##--end synology | |
##--for OSX, try: | |
# hamachi_service_stop="sudo launchctl stop com.logmein.hamachi" | |
# hamachi_service_start="sudo launchctl start com.logmein.hamachi" | |
# hamachi_tool=#locate your tool | |
##--end OSX | |
########### END EDIT BLOCK ############ | |
array_ping_list_size=${#array_ping_list[@]} | |
ping_fail_count=0 | |
dt=$(date +"%Y-%m-%d %H:%M") | |
function hamachi_reset { | |
echo "${dt} - hamachi ${ping_fail_count}/${array_ping_list_size} failures; Full-reset" | tee -a ${log_file} | |
${hamachi_service_stop} | |
sleep 3 | |
${hamachi_service_start} | |
sleep 7 | |
${hamachi_tool} logout | |
sleep 7 | |
${hamachi_tool} login | |
sleep 7 | |
} | |
function hamachi_go_online { | |
echo "${dt} - hamachi ${ping_fail_count}/${array_ping_list_size} failures; hamachi go-online; " | tee -a ${log_file} | |
for (( j = 0 ; j < ${#array_network_list[@]} ; j++ )) ; do | |
${hamachi_tool} go-online ${array_network_list[$j]} | |
sleep 3 | |
done | |
} | |
if (${hamachi_tool} | grep "logged in" > /dev/null ) | |
then | |
echo "hamachi already logged in." | |
echo "pinging ${array_ping_list_size} neighbors:" | |
for (( i = 0 ; i < ${array_ping_list_size} ; i++ )) ; do | |
if ! ( ping -c 1 -W 2 "${array_ping_list[$i]}" > /dev/null 2>&1 ) ; | |
then | |
echo "${array_ping_list[$i]} offline" | |
((ping_fail_count++)) | |
else | |
echo "${array_ping_list[$i]} online" | |
fi | |
done | |
if [ ${ping_fail_count} -eq ${array_ping_list_size} ] | |
then | |
hamachi_reset | |
hamachi_go_online | |
elif [ ! ${ping_fail_count} -eq 0 ] && [ ${ping_fail_count} -lt ${array_ping_list_size} ] | |
then | |
hamachi_go_online | |
else | |
echo "${dt} - hamachi ${ping_fail_count}/${array_ping_list_size} failures; no action taken" | tee -a ${log_file} | |
fi | |
else | |
hamachi_reset | |
hamachi_go_online | |
fi | |
You should edit, save, and mark the script executable. (e.g. chmod +x ~/scripts/check-hamachi.sh).
Since the script executes sudo /etc/init.d/logmein-hamachi start , you must add the command to your sudoers file:
Run the command EDITOR=nano sudo visudo and AT THE VERY LAST LINE (or elsewhere if you know what you are doing) add:
username ALL=(root) /etc/init.d/logmein-hamachi , where username is your account.
Lastly cronjob (crontab -e) it to every 5 minutes under your own user account. (e.g.:
Run the command EDITOR=nano sudo visudo and AT THE VERY LAST LINE (or elsewhere if you know what you are doing) add:
username ALL=(root) /etc/init.d/logmein-hamachi , where username is your account.
Lastly cronjob (crontab -e) it to every 5 minutes under your own user account. (e.g.:
*/5 * * * * ~/scripts/check-hamachi.sh
)
As always, Good luck!
---
Please consider crypto tipping: