Local ping network in python

Does anyone know how to use python to ping a localhost to find out if it is active or not? We (my team and I) have already tried using

os.system("ping 192.168.1.*") 

But the answer for the unreachable destination is the same as the answer for the host.

Thank you for your help.

+8
networking ping
source share
6 answers

Use this ...

 import os hostname = "localhost" #example response = os.system("ping -n 1 " + hostname) #and then check the response... if response == 0: print(hostname, 'is up!') else: print(hostname, 'is down!') 

If you use this script on unix / Linux, replace the -n switch with -c!
Thats all :)

+11
source share

I found that using os.system (...) leads to false positives (as OP said, "destination host is unavailable" == 0).

As stated earlier, using a subprocess works. For simplicity, I recommend doing this and then analyzing the results. You can easily do this, for example:

 if ('unreachable' in output): print("Offline") 

Just check the various outputs you want to check from the ping results. Do 'this' in this question.

Example:

 import subprocess hostname = "10.20.16.30" output = subprocess.Popen(["ping.exe",hostname],stdout = subprocess.PIPE).communicate()[0] print(output) if ('unreachable' in output): print("Offline") 
+6
source share

I encoded a little program a bit. This may not be the exact thing you are looking for, but you can always run a program on the host OS that opens the socket at startup. Here is the ping program itself:

 # Run this on the PC that want to check if other PC is online. from socket import * def pingit(): # defining function for later use s = socket(AF_INET, SOCK_STREAM) # Creates socket host = 'localhost' # Enter the IP of the workstation here port = 80 # Select port which should be pinged try: s.connect((host, port)) # tries to connect to the host except ConnectionRefusedError: # if failed to connect print("Server offline") # it prints that server is offline s.close() #closes socket, so it can be re-used pingit() # restarts whole process while True: #If connected to host print("Connected!") # prints message s.close() # closes socket just in case exit() # exits program pingit() #Starts off whole process 

And here you have a program that can receive a ping request:

 # this runs on remote pc that is going to be checked from socket import * HOST = 'localhost' PORT = 80 BUFSIZ = 1024 ADDR = (HOST, PORT) serversock = socket(AF_INET, SOCK_STREAM) serversock.bind(ADDR) serversock.listen(2) while 1: clientsock, addr = serversock.accept() serversock.close() exit() 

To run the program without showing it, just save the file as .pyw instead of .py. This makes it invisible until the user checks the running processes.

Hope this helped you

+5
source share

Try the following:

 ret = os.system("ping -o -c 3 -W 3000 192.168.1.10") if ret != 0: print "Host is not up" 

-o only one packet is waiting

-W 3000 gives it only 3000 ms to respond to a packet.

-c 3 allows you to try several times so that your ping does not run forever

+1
source share

For simplicity, I use home-made socket-based functions.

 def checkHostPort(HOSTNAME, PORT): """ check if host is reachable """ result = False try: destIp = socket.gethostbyname(HOSTNAME) except: return result s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(15) try: conn = s.connect((destIp, PORT)) result = True conn.close() except: pass return result 

if Ip: Port is available, return True

If you want to simulate Ping, you can refer to ping.py

+1
source share

Use this and parse line output

 import subprocess output = subprocess.Popen(["ping.exe","192.168.1.1"],stdout = subprocess.PIPE).communicate()[0]
import subprocess output = subprocess.Popen(["ping.exe","192.168.1.1"],stdout = subprocess.PIPE).communicate()[0] 
0
source share

All Articles