Skip to content

Instantly share code, notes, and snippets.

@rcarrata
Last active April 10, 2020 11:26
Show Gist options
  • Save rcarrata/0707998f903ce992e88d0f311f437d06 to your computer and use it in GitHub Desktop.
Save rcarrata/0707998f903ce992e88d0f311f437d06 to your computer and use it in GitHub Desktop.
#!/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