Skip to content

Instantly share code, notes, and snippets.

@ypchen
Created November 3, 2017 05:40
Show Gist options
  • Save ypchen/a6f93df3b4c93b300459845270bcb87a to your computer and use it in GitHub Desktop.
Save ypchen/a6f93df3b4c93b300459845270bcb87a to your computer and use it in GitHub Desktop.
Check if the given host (e.g., ptt.cc) is alive by using ping or nc
#!/bin/bash
# toTelegram.bash is at https://gist.github.com/ypchen/66714ab493a645ac059fc7bd72aa4884
#checkCommandPrefix='ping -c 1 -w 10'
#checkCommandSuffix=''
checkCommandPrefix='nc -z -w 10 -v'
checkCommandSuffix='22'
hostToCheck="ptt.cc" ; sleepSec=20 ; successValue=0 ; while true ; do date ; ip=`nslookup ${hostToCheck} | grep "Address" | tail -n 1 | cut -f 2 -d ' ' | tr -d '\n'` ; echo "Check ${hostToCheck} [${ip}] by using \"${checkCommandPrefix} ${ip} ${checkCommandSuffix}\"" ; ${checkCommandPrefix} ${ip} ${checkCommandSuffix} &> /dev/null ; if [ $? -eq $successValue ]; then break ; fi ; echo Sleep for ${sleepSec} seconds ; sleep ${sleepSec} ; done ; toTelegram.bash "${hostToCheck} is alive." &> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment