2

I have the following block of Python code which i use to Ping multiple addresses

import subprocess as s  
import tkinter as tk
from tkinter import *

IPT.title("IP Ping Test Tool")
canvas_IPT = Canvas(IPT, width=401, height=551)
canvas_IPT.pack()   

Output_IP_Address_AN = Output_IP_Address[10]

IPT_Display_AN = Label(IPT, text=Output_IP_Address_AN, anchor=W)
IPT_Display_AN_Window = canvas_IPT.create_window(150, 235, anchor = 'sw', window = IPT_Display_AN)

def IPT_Test():
    print("Test Initiated")

    AN_Sel_State = var4.get()
    if (AN_Sel_State == 1):
        if(s.call(["ping", Output_IP_Address_AN])==0):
                print("AvI Navi 'Ping' Sucessful")
                IPT_Display_AN = Label(IPT, text=(Output_IP_Address_AN, "Ok"),fg='green', anchor=W)
                IPT_Display_AN_Window = canvas_IPT.create_window(150, 235, anchor = 'sw', window = IPT_Display_AN)
        else:
                print("Avi Navi 'Ping' NOT Sucessful")
                IPT_Display_AN = Label(IPT, text=(Output_IP_Address_AN, "Failed"),fg='red', anchor=W)
                IPT_Display_AN_Window = canvas_IPT.create_window(150, 235, anchor = 'sw', window = IPT_Display_AN)

    IPT_Display_AN = Label(IPT, text=Output_IP_Address_AN, anchor=W)
    IPT_Display_AN_Window = canvas_IPT.create_window(150, 235, anchor = 'sw', window = IPT_Display_AN)

var4 = IntVar()
IPT_Test_AN = Checkbutton(IPT, text = "AVI/NAVI", justify=LEFT,width=15, indicatoron=0,
                                variable = var4, command=AN_Sel)
IPT_Test_AN_Window = canvas_IPT.create_window(20, 240, anchor = 'sw', window = IPT_Test_AN)

IPTest_Button = tk.Button(IPT, text = 'TEST', command = IPT_Test, background ="#01AEAC", width=20, height=3,)

This works as expected in instances were packet data is received (Succesful) and also works when 'Request timed out' (Not Succesful) however when 'Host Unreachable' it returns the result is successful.

Having done some research on this and i am lead to believe that "Host Unreachable" returns as ==0 even though the ping wasn't actually successful (Still dont really understand why this is ?)

If this is the case how do i exclude "Host Unreachable" from displaying successful for my application when it has the same exit status as a successful Ping ?

M.Tomlinson
  • 81
  • 1
  • 9
  • Read the output of the command. – xrisk Sep 05 '18 at 07:35
  • `ping` isn't really properly scriptable in the first place. You should perhaps be looking at `fping` or `multiping` or friends. – tripleee Sep 05 '18 at 07:46
  • Tangentially, your code contains several undefined entities. We can guess `s` is an alias for `subprocess` but those others I couldn't guess. Probably look at the [help] guidance for creating a [mcve]. – tripleee Sep 05 '18 at 07:47
  • @tripleee apologies i was trying to keep the code example to a minimum i will edit the post to clarify – M.Tomlinson Sep 05 '18 at 07:53
  • @tripleee fping looks ideal but im using windows not linux. The result of this ping is used for other functions in the wider program otherwise i would as you suggested use a dedicated ping tool. – M.Tomlinson Sep 05 '18 at 14:02
  • A properly *minimal* example would remove all the graphical stuff, and basically just keep the `subprocess` and the `if`. But thanks for the update. – tripleee Sep 05 '18 at 16:35

2 Answers2

0

I am not sure why this happened in your local, I test locally, it works for me when I have the following code:

...
if(s.call(["ping", "-c", "1", "-W", "4", Output_IP_Address_AN])==0):
     print("AvI Navi 'Ping' Sucessful")
...

I let ping to send 1 package and timeout is 4 second.

in local, I run the following command to simulate Host Unreachable case:

 sudo ip route add unreachable 1.0.0.3

then run your code with changes above, and NOT Sucessful is print. when Output_IP_Address_AN is set to 1.0.0.3

Leogao
  • 294
  • 2
  • 3
  • I will test this again with what you have. What is "-c", "1", "-W", "4", doing ? – M.Tomlinson Sep 05 '18 at 13:40
  • So in my test scenario i am connecting to a device. When the device is configured correctly i get Successful, when the device isnt configured correctly i get 'Host Unreachable' (which returns sucessful) and if i unplug the device ethernet i get 'Request Timed Out' and returns Not Sucessful – M.Tomlinson Sep 05 '18 at 13:58
0

Since the PING request gets a Reply as:

Reply from xx.xx.xx.xx: Destination Host Unreachable

which technically is still a reply.

So try to parse the console output, which might serve your purpose as below:

ping -c 1 -w 4 ip_addr | findstr /i "TTL"

There is a similar question here: Destination Host unreachable does not result in an errorlevel 1