Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I am pinging the below IP using the os.system() it is pinging and giving response value 0 if ping successful else 1 when not reachable

but the problem is when I add that in while if destination ip is unreachable also it is returning 0 and 1 in alternative manner (after some time delay)

if ip reachable is it returning 0 continuity

This the link I tried both subprocess.call both the API are giving same output.

https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/6 Python Function to test ping

What I have tried:

import os
from time import sleep

def add():
    print("in add function")

def sub():
    print("in subtract function")

def ping():
    online = os.system("ping -n 1 192.168.0.9")
    if(online == 0):
         print("Avilabe with ",online)
         return True
    else:
         print("Ofline with ",online)
         return False

while True:
   add()
   sleep(0.5)
   if( ping()):
         sub()
    '''
    do other things if ping is successful
    '''
   sleep(0.5)
any alternative API to get return value when system pinging
Posted
Updated 1-Aug-20 2:47am
v2

The return value is from the ping command and will depend on whatever response is received by the command. This has nothing to do with Python programming but is a feature of the Windows ping command. If you run the following in a command window to check:
ping -n 1 192.168.0.9
echo %ERRORLEVEL%

The ERRORLEVEL variable give the result of the previous command.
 
Share this answer
 
v2
Comments
Yashvanth B 1-Aug-20 9:17am    
Hi thanks for the replay what i need is if ping is successful then connect to server and do other operation, if it fails then try to ping the server ip until server is available for u tried the above but it's giving same values any alternative API in python to get return values
Richard MacCutchan 1-Aug-20 9:50am    
Ping is not designed as an API function, but a method of manually checking the state of a remote site. You need to do some more testing at a command prompt level to check exactly what value gets set for different situations. As I said above, this is nothing to do with Python.

If you want to get direct control over what is happening then you should probably use the Python socket library.
[no name] 1-Aug-20 12:35pm    
As far as I know (and doing myself also) ping will be used a lot of times to check whether an ip is available. Anyway a 5.
Richard MacCutchan 1-Aug-20 13:03pm    
Possibly. But it can fail for other reasons, such as the route being broken at some point. So it is not a reliable test of whether a specific site is available.
Richard MacCutchan 1-Aug-20 13:29pm    
I have run a number of tests locally and the value returned from the ping command is not valid. It returns 0 whether the site is reachable or not.
Those are local IP addresses, so we can't duplicate your exact system - which means we can't test your code under the same circumstances you do, and that may be relevant.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900