Check if remote host is removed in Python

How can I check if a remote host failed without a port number? Is there any other way that I could test others using regular ping.

There is a possibility that the remote host might refuse ping packets

+6
source share
7 answers

This worked fine for me:

HOST_UP = True if os.system("ping -c 1 " + SOMEHOST) is 0 else False 
+12
source

It is best to use PING at the protocol level, that is, connect to the server and interact with it so that it does not perform real work. This is because it is the only real way to make sure that the service is running. ICMP ECHO (aka ping) will only tell you that the interface of the other end network is inserted, and even then it can be blocked; FWIW, I saw machines in which all user processes were clogged with bricks, but which still could be pinged. On these days, application servers, even getting a network connection may not be enough; What if the hosted application does not work or otherwise does not work? As I said, talking about sweet nights for the actual service that interests you is the best, most reliable approach.

+1
source

The best you can do is:

  • Try connecting to a known port (for example, port 80 or 443 for HTTP or HTTPS); or
  • Ping on the site. See the ping site in Python?

Many sites block ICMP (the portfolio used for ping sites), so you should know in advance if this host is enabled or not.

A port connection tells you mixed information. It really depends on what you want to know. The port may be open, but the site hangs efficiently, so you may get a false result. A more rigorous approach may include using the HTTP library to execute a web request on the site and verify the response to the request.

It all depends on what you need to know.

0
source

Many firewalls are configured to remove ping packets without response. In addition, some network adapters will respond to ICMP ping requests without entering from the network stack of the operating system, which means that the operating system may be unavailable, but the host still responds to pings (usually you will notice that you reboot the server, say, it will start responding to pings for a while before the OS actually appears and other services start).

The only way to make sure the host is up is to try connecting to it through some known port (for example, port 80 of the web server).

Why do you need to know if the host is up, maybe there is a better way to do this.

0
source

How about trying something that requires RPC, like the "tasklist" command combined with ping?

0
source

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 
0
source
 HOST_UP = True if os.system("ping -c 5 " + SOMEHOST.strip(";")) is 0 else False 

to remove nasty scripts just add .strip (";")

 -c 5 

increase the number of ping requests if everything went through than True

PS. Works only on Linux, on Windows always returns True

0
source

Source: https://habr.com/ru/post/1305405/


All Articles