Can you ping an IP address or a computer name?


Is there a way to ping an IP address and/or a computer name? I am playing around and would like to make a status page with our servers and the current status of those servers.

I have just found Xojo on Monday and already love it. I am coming from VB and the ability to be more universal to multiple device types is such a exciting thing.


Some code I wrote a long time ago. I think if you pass the computer name instead of the ip address, it should work with that too.

Public Function Ping(ip As String) as String
  'Send a ping
  Dim My_Shell As New Shell
  #If TargetWindows Then
    Const sys = 0
    My_Shell.Execute "ping -c 2 "+ip
  #Elseif TargetMacOS 
    Const sys = 1
    My_Shell.Execute "ping -c 2 "+ip
  #Elseif TargetLinux
    Const sys = 2
    'This may need changes; not tested
    My_Shell.Execute "ping -c 2 "+ip
  return "System "+str(sys)+EndOfLine+ReplaceLineEndings(My_Shell.Result,EndOfLine)
End Function

Note the conditional compilation for the various targets allows you to customize the command line for each OS, and since each of these assigns a different value to the variable sys, you can use that value to help interpret the result in case each OS formats the return value differently.

1 Like

Charlie and Aaron wrote in 2005 an ICMP class. Kind of old now, not sure if working. Besides conversion for modern Xojo, probably needs updates.

Thanks for sending this over! I keep getting a System 0 response, maybe I am not using it right.

An alternative solution, I made a batch file that runs every X amount of time and writes the results to the file and the application can read the file to pull in the info. It sounds like that is more of an easier/possible task. This was results from testing the batch file:
TSL-FILES - Online - Wed 05/25/2022 16:55:05.26
TSL-GP18 - Online - Wed 05/25/2022 16:55:06.16
FAKE-SERVER-TEST - Offline - Wed 05/25/2022 16:55:08.19

However there would be some downsides to this method as if the program reads the file at the same second the batch file runs, it could be missing a few lines of the file.

I guess that’s why its good to always have more than one way to complete a task!

Thanks Rick! I will check these out!

I did come up with a “not-perfect-but-works” solution mentioned under Robert’s response :slightly_smiling_face:

Just testing the waters for all the capabilities.

That means that it’s running on Windows, and didn’t get any response from the ping. Looking at my old code, I don’t remember doing very much testing except on MacOS. It does work on my Mac. You could try changing this line in the #If TargetWindows section from this:
My_Shell.Execute "ping -c 2 "+ip
to this:
My_Shell.Execute "ping "+ip

Otherwise, maybe someone who’s more familiar with the windows command line format for ping will jump in with the correct syntax.

On windows “ping -c” should be “ping -n”.

Also this is older code, but works fine in my current projects in which specifying the number of pings instead of relying on the OS default value should be advantageous to you as it was for me. My goal was to issue the least amount of echo to receive the echo-replies to understand if my connection is up or down. I issue 2 echo’s using the following:

To run I call the function (Async) and use a timer to check for the results so I am not blocking anything:
Call pingCheck(2,"")

Public Function pingCheck(inNumOfPingsInt as Integer, inDestIP as String) as Boolean
PingShell = New Shell
Var Result as String
PingShell.Mode = 1
Var inNumOfPings as String = inNumOfPingsInt.ToString

#IF TargetMacOS = True Then
  PingShell.Execute("ping -W 1000 -c " + inNumOfPings + " " + inDestIP)
#ELSEIF TargetWin32 = True Then
  PingShell.Execute("ping -n " + inNumOfPings + " " + inDestIP)


End Function

The following I run from a timer to check for the results, parse, and make a decision.

Var pingResultStr as String = PingShell.Result
Var AreWeConnected as Boolean = parsePingResults(pingResultStr)
Public Function parsePingResults(inSourceString as String) as Boolean

Var ParsePingResults_RegEx as RegEx
Var ParsePingResults_RegExMatch as RegExMatch
Var ParsePingResults_HitText as String
ParsePingResults_RegEx = New RegEx
ParsePingResults_RegEx.Options.Greedy = False
ParsePingResults_RegEx.Options.caseSensitive = false

#IF TargetMacOS = True Then
  ParsePingResults_RegEx.SearchPattern = "(icmp_seq=[0-1])"
#ELSEIF TargetWin32 = True Then
  ParsePingResults_RegEx.SearchPattern = "(bytes=).+\s(time=)"
ParsePingResults_RegExMatch = ParsePingResults_RegEx.Search(inSourceString)

if ParsePingResults_RegExMatch <> nil then
  ParsePingResults_HitText = ParsePingResults_RegExMatch.SubExpressionString(0)
end if

if ParsePingResults_HitText <> "" Then
  System.DebugLog("online") // for testing
  Return True
  System.DebugLog("offline") // for testing
  Return False
End if

End Function
1 Like

you could also make a service that send a alive status to a other service.
each one send
other collect and save this into database
with a query you can make a nice system overview
with a query you get failures and could send mails or sms
if you have a server service you can collect much more infos

1 Like