Skip to content

Instantly share code, notes, and snippets.

@rcarrata

rcarrata/check_port.py

Last active Apr 10, 2020
Embed
What would you like to do?
#!/usr/bin/python
import socket
import time
retry = 2
delay = 1
timeout = 1
ips = {}
def isOpen(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((ip, int(port)))
s.shutdown(socket.SHUT_RDWR)
return True
except:
return False
finally:
s.close()
def checkHost(ip, port):
print("\n--- Testing Connections ---")
ipup = False
for i in range(retry):
if isOpen(ip, port):
ipup = True
break
else:
print("Retry...")
time.sleep(delay)
return ipup
def check_port():
check_active = True
while check_active:
ip = input("\nWhat's the IP to check? ")
port = input("What's the Port to check? ")
ips[ip] = port
if checkHost(ip, port):
print ("INFO: Connection to IP/FQDN: "+ ip + " on port:" + str(port) + " ESTABLISHED")
else:
print("\n--- Connectivity results ---")
print ("ERROR: Connection to IP/FQDN: "+ ip + " on port:" + str(port) + " FAILED")
print("----------------------------")
repeat = input("\nCheck another port? (y/n) ")
if repeat == 'n':
print("Goodbye!")
check_active = False
check_port()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment