I would use a port scanner. The original question says you do not want to use the port. Then you need to specify which protocol (yes, you need a port) that you want to control (HTTP, VNC, SSH, etc.). If you want to control via ICMP, you can use the subprocess and ping control parameters, number of pings, timeout, size, etc.
import subprocess try: res = subprocess.Popen(['ping -t2 -c 4 110.10.0.254 &> /dev/null; echo $?'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) out, err = res.communicate() out = out.rstrip() err = err.rstrip() print 'general.connectivity() Out: ' + out print 'general.connectivity() Err: ' + err if(out == "0"): print 'general.connectivity() Successful' return True else: print 'general.connectivity() Failed' return False except Exception,e: print 'general.connectivity() Exception' return False
If you need a port scanner
import socket from functools import partial from multiprocessing import Pool from multiprocessing.pool import ThreadPool from errno import ECONNREFUSED NUM_CORES = 4 def portscan(target,port): try: # Create Socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socketTimeout = 5 s.settimeout(socketTimeout) s.connect((target,port)) print('port_scanner.is_port_opened() ' + str(port) + " is opened") return port except socket.error as err: if err.errno == ECONNREFUSED: return False # Wrapper function that calls portscanner def scan_ports(server=None,port=None,portStart=None,portEnd=None,**kwargs): p = Pool(NUM_CORES) ping_host = partial(portscan, server) if portStart and portStart: return filter(bool, p.map(ping_host, range(portStart, portStart))) else: return filter(bool, p.map(ping_host, range(port, port+1))) # Check if port is opened def is_port_opened(server=None,port=None, **kwargs): print('port_scanner.is_port_opened() Checking port...') try: # Add More proccesses in case we look in a range pool = ThreadPool(processes=1) try: ports = list(scan_ports(server=server,port=int(port))) print("port_scanner.is_port_opened() Port scanner done.") if len(ports)!=0: print('port_scanner.is_port_opened() ' + str(len(ports)) + " port(s) available.") return True else: print('port_scanner.is_port_opened() port not opened: (' + port +')') return False except Exception, e: raise except Exception,e: print e raise