Last active
August 29, 2015 14:06
-
-
Save bot11/d7627e4ca9c54e67b88a to your computer and use it in GitHub Desktop.
Telnet library that verifies the remote port open
This file contains 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
class Connect: | |
def __init__(self, ip, port): | |
self.ip = ip | |
self.port = port | |
def verify(self): | |
import socket | |
import telnetlib | |
session = None | |
try: | |
#telnet.Telnet(ip, port, timeout in sec) | |
session = telnetlib.Telnet(self.ip, self.port, 3) | |
except socket.error as err: | |
#print err.message | |
return False | |
else: | |
return True | |
finally: | |
if session is not None: | |
session.close() | |
if __name__ == "__main__": | |
# file ip_ports have all the ips and ports seperated with spaces | |
# Eg: | |
#172.16.0.103 80 | |
#172.16.0.104 80 | |
f = open('ip_ports', 'r') | |
for line in f.readlines(): | |
line.strip() | |
#print line | |
(ip, port) = line.split() | |
port = int(port) | |
connection = Connect(ip, port) | |
if not connection.verify(): | |
print ("%s:%d closed" % (ip, port)) | |
else: | |
print ("%s:%d open" % (ip, port)) | |
f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment