Skip to content

Instantly share code, notes, and snippets.

@skamithi
Last active June 23, 2019 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skamithi/7034952 to your computer and use it in GitHub Desktop.
Save skamithi/7034952 to your computer and use it in GitHub Desktop.
Bash script to detect if SSH port is active on remote host. Used it orchestrating vm creation using ansible.
#!/bin/bash
# Script for checking if SSH port is open
# Only checks that port is open. Not that actually SSH connection can occur
counter=0
result="ssh disabled"
if [ -z "$1" ]
then
echo "hostname argument required. Example ssh_port_checker.sh 10.1.1.1"
exit
fi
while [ $counter -lt 20 ]; do
echo "check ssh port connection for $1"
telnet_output=`echo quit | telnet $1 22 2>/dev/null | grep Connected`
case "$telnet_output" in
*Connected*)
let counter=100
result="ssh enabled"
;;
esac
let counter=counter+1
echo "sleep for 5 seconds"
sleep 5
done
echo "$result on $1"
if [[ "$result" =~ "enabled" ]]
then
exit 0
fi
exit -1
# ansible code
- name: copy port checker script to tmp dir
copy: src=ssh_port_checker.sh dest=/tmp
- name: check if ssh port is open.
shell: /tmp/ssh_port_checker.sh 10.100.1.2
- name: sleep for a couple of seconds if ssh is just restarting on the host
pause: seconds=5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment