Python Example: Check for open SSH
I posted this bit of code some time ago as pysync.py, but I wanted to present it again in it’s latest form. Right now, all it does is print a message stating whether or not it has an SSH port open. This can be used as the basis of a script to do something based on an open SSH port. The port could also be changed to check for HTTP, telnet or what have you.
'''
Python code to check if SSH is open on a server
Updated: January 23, 2009
Brad Peters
brad (at) endperform (dot) org
'''
import socket
import os
'''
This code actually attempts to check the SSH port
'''
def checkServer():
serverSocket = socket.socket()
serverSocket.settimeout(0.25)
try:
serverSocket.connect(("192.168.1.20", 22))
except socket.error:
return 1
'''
If status is defined, we know the connection failed
'''
status = checkServer()
if status:
print "Server is down"
else:
print "I can ssh!"
This code is free, use it as you like. I just wanted to present it to show what I’ve been messing with. I have an idea for this at work. I haven’t messed with classes yet, but that’s next on my agenda.
Comments