Skip to content

Instantly share code, notes, and snippets.

@meoso
Last active April 13, 2021 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoso/141973b2f29a051e09ff to your computer and use it in GitHub Desktop.
Save meoso/141973b2f29a051e09ff to your computer and use it in GitHub Desktop.
bash script to check hamachi status and perform reset tasks if applicable
#!/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
@meoso
Copy link
Author

meoso commented Oct 10, 2016

changed ping -c 1 -W 1 to ping -c 1 -W 2 because some links are slow, and script would unnecessarily [re]start services.

@meoso
Copy link
Author

meoso commented Oct 11, 2016

Be sure to add your PATH=... in crontab; otherwise scripts will not work as expected (especially OSX).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment