-
-
Save joglomedia/acc998c61b402ed8da8734ba87269d51 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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