Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Created April 7, 2019 11:22
Show Gist options
  • Save HirbodBehnam/94b3792cc6f48db45b5e263a9b69837f to your computer and use it in GitHub Desktop.
Save HirbodBehnam/94b3792cc6f48db45b5e263a9b69837f to your computer and use it in GitHub Desktop.
Check if a server is blocked with ssh, results are send through a telegram bot
#!/bin/bash
TELE_BOT_API="Get your tag from @BotFather"
MY_ID="Your telegram ID. This is a number"
#Save your ip addresses and ports in a file named ip.txt, split by new line
#Save like ip:port of your ssh connection for example if your host is 1.1.1.1 and your ssh port is 223 it must be entered like 1.1.1.1:223
input="ip.txt" #Filename to read ip from
TIME_OUT=30 #Set timeout for ssh connection
while true; do
IP=()
while IFS= read -r var
do
IP+=("$var")
done < "$input"
for i in "${IP[@]}"
do
echo "Checking IP address $i"
split=(${i//:/ })
timeout "$TIME_OUT" ssh root@"${split[0]}" -p "${split[1]}"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
#If code reachs here then it means that your server is not accessible
ERROR_TXT="https://api.telegram.org/bot$TELE_BOT_API/sendMessage?chat_id=$MY_ID&text="
ERROR_TXT+='"'
ERROR_TXT+="Error on connecting to server $i\\nReturn Code: $EXIT_CODE"
curl "https://api.telegram.org/bot$TELE_BOT_API/sendMessage?chat_id=$MY_ID&text=$ERROR_TXT"
fi
done
#Change delay between changing IPs if you want
sleep 3600
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment