0

I want to check a ping if it was successfull or not. Now I have the problem that i had such a result with a manual ping:

Ping wird ausgeführt für 10.128.129.15 mit 32 Bytes Daten:
Antwort von 10.128.129.15: Zielhost nicht erreichbar.
Antwort von 10.128.129.15: Zielhost nicht erreichbar.
Antwort von 10.128.129.15: Zielhost nicht erreichbar.

Ping-Statistik für 10.128.129.15:
Pakete: Gesendet = 3, Empfangen = 3, Verloren = 0 (0% Verlust),
# which would be an "- Success" output in my script

For every non-german person, the ping command works, but in the end it say that the DestinationHost is unreachable. I want to check if it IS reachable with the ping request, thats why I tried something like this:

def pingtest(destination,device)
    print(" Pinging #{destination} #{device}: ")
#I tried to get the system output with several methods, the ping is printed
#in the consol but in the end it just returns "true"
    output = system("ping -n 3 #{destination} 2>&1")  #later I want >nul
    puts output         #prints "true"

#I tried some stuff like this:  the .include? method was an
# idea from ***another Question (with ssh) and this worked
 if output.content.include?("Zielhost nicht erreichbar")
        puts "Success Debug"
    end
#Backtick to check if the ping succeded or not..
    if $? == 0
        puts " - Success\n"
        return true
    else
        puts(" - Failed\n")
        return false
    end
end

***another question There i fixed my problem like this:

result = ssh.exec!"ping -c 3 #{@dest_ip}"
      if result.include?("100% packet loss") || result.include?("Zeitüberschreitung") || result.include?("100% Verlust") || and so on

I didn't found a work solution for my Problem now (without a SSH connection) on SO so maybe you will have a solution for that. My goal is it to securly say if a device is reachable in the network or not. Maybe a short review about my project: I have two NAT Gateways builded with RasberyPIs - with two profinets and a network including these both profinets. I want to check now every connection in the network of this two NAT Gateways. Im a (win) Service_PC in this case running this script. More or less I need to check the command-promt while running the ping. But how?

Community
  • 1
  • 1
Drayke
  • 367
  • 1
  • 3
  • 14

1 Answers1

0

ping, as well as any other handy utility, returns the status of command execution. You should never parse an output.

system('ping -c 2 localhost')
#⇒ true
system('ping -c 2 unknown')
#⇒ false

Everything you need is to check the return value of call to system.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • My problem is that even if the ping command execution was successful, I can't know if the device is really reachable or not. It could be that the "Host is unreachable" but `system('ping -c 2 localhost')` still returns true. – Drayke Apr 29 '15 at 10:09
  • It could not. If it does, you might try to use excellent https://github.com/chernesk/net-ping library instead of parsing output. – Aleksei Matiushkin Apr 29 '15 at 10:23