B4J Question Ping server

bogdanc

Active Member
Licensed User
Longtime User
Hi All!

While a go I asked how to ping server and get respond.
I've got some answer but it only tells me that server is responding.
How to get similar thing as it on win shell cmd with the number of packets received or lost and time of respond?

B4X:
Sub Ping(ipaddress As String)
    Log("started")
    Dim shl As Shell
    Dim lstParams As List: lstParams.Initialize
    Try
        Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
        If os.Contains("win") Then
            Log("Windows")
            'Windows
            lstParams.Add("-n")
        Else
            'Linux and Mac
            lstParams.Add("-c")
        End If
        lstParams.Add("1")
        lstParams.Add(ipaddress)
        
        shl.Initialize("shl", "ping", lstParams)
        shl.WorkingDirectory = File.DirApp
        shl.Run(-1)
    Catch
        Log(LastException.Message)
    End Try
End Sub

Thank you for any help in advance.
 

udg

Expert
Licensed User
Longtime User
Are you looking for the "feedback" from a successful ping command?
Did you add something like the following to your code?
B4X:
Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut AsString, StdErr AsString)
If Success AND ExitCode = 0 Then 
   Log("Success")
   Log(StdOut)
Else
   Log("Error: " & StdErr)
EndIf
ExitApplication
End Sub
Are you aware that many installations don't allow reply to a ping command?

udg
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
Thank You for You replay!

Yes. I need to have a feedback from shell.

No i'm getting some output.


B4X:
.....
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),   
Minimum = 3ms, Maximum = 3ms, Average = 3ms
.....

1)
is it a way to strip the info the get time and packet's losts?

put this to the variable and search for: Average = and cut it to display rest of string.

2) Second how to ping 4 ips on the same time?

B4X:
 lstParams.Add(ipaddress1)
lstParams.Add(ipaddress2)
.
.
.
lstParams.Add(ipaddressN)
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,

have a look at RegEx for a powerful method to extract info from a non-structured text (i.e. your StdOut as retuned by jshell after executing the ping command).
AFAIK, ping command works on a single address at time. You could list a few addresses in a global list, process on item at time (the top one), remove it an so on while you ha ve items in the list.
 
Upvote 0
Top