Skip to content

Instantly share code, notes, and snippets.

@sharanpeetani
Last active February 17, 2022 18:40
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 sharanpeetani/3716510f5527102f594819111c092c62 to your computer and use it in GitHub Desktop.
Save sharanpeetani/3716510f5527102f594819111c092c62 to your computer and use it in GitHub Desktop.
script to check ssh connection and uptime
#!/bin/sh
USR='username'
for host in $(cat host_list)
do
sshpass -f pd_file ssh -q -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 ${USR}@${host} exit
let ret_code=$?
if [ $ret_code -eq 5 ]; then
echo "${i} Connection Refused!" $ret_code
elif [ $ret_code -eq 255 ]; then
echo "${i} server down!" $ret_code
elif [ $ret_code -eq 0 ]; then
echo "${i} is up and Connected!" $ret_code
echo "uptime: `uptime | awk -F'( |,|:)+' '{
d=h=m=0;
if ($7=="min")
m=$6;
else {
if ($7~/^day/) { d=$6; h=$8; m=$9}
else {h=$6;m=$7}
}
}
{
print d+0,"days,",h+0,"hours,",m+0,"minutes."
}'`"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment