Using Python to check if remote port is open and accessible.
Taken from http://snipplr.com/view/19639/test-if-an-ipport-is-open/
Usage:
- Open a Python prompt
- Run the script in is_port_open.py
- Call isOpen with a host and a port parameter. Example: isOpen("www.google.com", 80)
Yes an old thread but still usefull. Can anyone shed some light as to how you can have multiple hosts that it polls until the app is exited?
as in..
domain1.com host id UP
domain2.com host id UP
domain3.com host id DOWN
domain4.com host id UP
What Im using so far...
#!/usr/bin/python3
import sys
import socket
import time
ip = "domain.com"
port = 443
retry = 1
delay = 1
timeout = 2
def isOpen(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
s.shutdown(2)
return True
except:
return False
def checkHost(ip, port):
ipup = False
for i in range(retry):
if isOpen(ip, port):
ipup = True
break
else:
time.sleep(delay)
return ipup
if checkHost(ip, port):
print ip + " port " + str(port) + u" is \u001b[32;1mUP!\u001b[0m"
else:
print ip + " port " + str(port) + u" is \u001b[31;1mDOWN!\u001b[0m"