Pages

Friday, March 18, 2016

Shell script to check SSH Connection

Shell script to check SSH Connection

My requirement was to check how many system(s) in the network have port 22 in opened state. There are many ways to achieve this.

1. Use Shell Script
2. Use Paramiko, a Python SSH API

I tried to implement with Shell Script.
See the Sample Bash Code below

uphost=127.0.01 # Replace this with the IP Address 
a=`nmap $uphost -PN -p ssh | grep open`
echo $a (or) echo $a | wc -c 

If the Port No. 22 is open, The output will be as follows.

22/tcp open ssh



If port is closed or not accessible from outside, we will get empty string

To Check the output , word count (wc) can be used.

If the Word Count of Characters of the Output is 1, then Port is closed
else Port is open

Solution Reference:
http://stackoverflow.com/questions/1405324/how-to-create-a-bash-script-to-check-the-ssh-connection






No comments:

Post a Comment