This project is based on a response by @NJDude in an old post, that I can't find at the moment. When I finish it, I will add this library shared by @Erel for B4I.
Every segment in bold and red should be returned to a variable. Currently, I'm using IndexOf, but I think it would be better to do it with RegEx. Below is the text and a current example of how I get the host and IP with IndexOf:
PING www.google.com (142.251.133.4) 56(84) bytes of data.
64 bytes from eze10s01-in-f4.1e100.net (142.251.133.4): icmp_seq=1 ttl=116 time=17.5 ms
--- www.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4011ms
rtt min/avg/max/mdev = 17.570/23.754/33.455/6.264 ms
The code so far is as follows; I only have the last 2 lines of the statistics left.
Every segment in bold and red should be returned to a variable. Currently, I'm using IndexOf, but I think it would be better to do it with RegEx. Below is the text and a current example of how I get the host and IP with IndexOf:
PING www.google.com (142.251.133.4) 56(84) bytes of data.
64 bytes from eze10s01-in-f4.1e100.net (142.251.133.4): icmp_seq=1 ttl=116 time=17.5 ms
--- www.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4011ms
rtt min/avg/max/mdev = 17.570/23.754/33.455/6.264 ms
B4X:
'First line
toHost = lneResult.SubString2(lneResult.IndexOf(" "), lneResult.IndexOf(" (")).Trim
toIP = lneResult.SubString2(lneResult.IndexOf("(") + 1, lneResult.IndexOf(")")).Trim
'Ping responses
Dim intSeq As Byte = lneResult.SubString2(lneResult.IndexOf("icmp_seq=") + 9, lneResult.IndexOf("ttl")).Trim.As (Byte)
Dim mapSeq As Map
mapSeq.Initialize
mapSeq.Put("from", lneResult.SubString2(lneResult.IndexOf("from ") + 5, lneResult.IndexOf("(")).trim)
mapSeq.Put("ttl", lneResult.SubString2(lneResult.IndexOf("ttl=") + 4, lneResult.IndexOf("time")).trim)
mapSeq.Put("time", lneResult.SubString2(lneResult.IndexOf("time=") + 5, lneResult.IndexOf("ms")).trim)
icmp_seq.Put(intSeq, mapSeq)
B4X:
Sub Process_Globals
Type tpePing (Success As Boolean, toHost As String, toIP As String, mapResponse As Map, mapStatistics As Map, Msg As String)
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim pingResult As tpePing = Ping("www.google.com", 5, 100)
Log(pingResult.Msg)
End Sub
Public Sub Ping(URL As String, Attempts As Int, Timeout As Int) As tpePing
Dim pingResult As tpePing
pingResult.Initialize
If Attempts = 0 Then
pingResult.Msg = "Attempts must be greater than or equal to 1"
Else
Dim sb As StringBuilder
Dim p As Phone
sb.Initialize
p.Shell("ping -c" & Attempts & " -W" & Timeout & " -v " & URL, Null, sb, Null)
If sb.Length = 0 Then
pingResult.Msg = "Host unreachable"
Else
pingResult.Success = True
pingResult.Msg = sb.ToString
Dim aryResult() As String = Regex.Split(CRLF, pingResult.Msg)
Log(sb.ToString)
Dim blnIsFirstLine As Boolean = False
Dim blnStats As Boolean = False
Dim bteLine As Byte = 0
Dim icmp_seq As Map
icmp_seq.Initialize
For Each lneResult As String In aryResult
If Not (blnIsFirstLine) Then
blnIsFirstLine = True
pingResult.toHost = lneResult.SubString2(lneResult.IndexOf(" "), lneResult.IndexOf(" (")).Trim
pingResult.toIP = lneResult.SubString2(lneResult.IndexOf("(") + 1, lneResult.IndexOf(")")).Trim
Else If Not (blnStats) Then
If lneResult <> "" And lneResult.IndexOf("ping statistics") = -1 Then
Dim intSeq As Byte = lneResult.SubString2(lneResult.IndexOf("icmp_seq=") + 9, lneResult.IndexOf("ttl")).Trim.As (Byte)
Dim mapSeq As Map
mapSeq.Initialize
mapSeq.Put("from", lneResult.SubString2(lneResult.IndexOf("from ") + 5, lneResult.IndexOf("(")).Trim)
mapSeq.Put("ttl", lneResult.SubString2(lneResult.IndexOf("ttl=") + 4, lneResult.IndexOf("time")).Trim)
mapSeq.Put("time", lneResult.SubString2(lneResult.IndexOf("time=") + 5, lneResult.IndexOf("ms")).Trim)
icmp_seq.Put(intSeq, mapSeq)
End If
Else If blnStats Then
Dim mapStats As Map
mapStats.Initialize
pingResult.mapStatistics.Initialize
If bteLine = 0 Then
mapStats.Put("packets transmitted", lneResult.SubString2(0, lneResult.IndexOf(" packets")).Trim)
mapStats.Put("received", lneResult.SubString2(lneResult.IndexOf("transmitted, ") + 13, lneResult.IndexOf("received")).Trim)
mapStats.Put("packet loss", lneResult.SubString2(lneResult.IndexOf("received, ") + 10, lneResult.IndexOf("%")).Trim)
mapStats.Put("time", lneResult.SubString2(lneResult.IndexOf("time ") + 5, lneResult.IndexOf("ms")).Trim)
pingResult.mapStatistics.Put("StatsLine1", mapStats)
Else If bteLine = 1 Then
Dim strDataSegment As String = lneResult.SubString2(lneResult.IndexOf("= ") + 2, lneResult.IndexOf(" ms")).Trim
Dim aryDataSegment() As String = Regex.Split("/", strDataSegment)
mapStats.Put("packets transmitted", aryDataSegment(0))
mapStats.Put("received", aryDataSegment(1))
mapStats.Put("packet loss", aryDataSegment(2))
mapStats.Put("time", aryDataSegment(3))
pingResult.mapStatistics.Put("StatsLine2", mapStats)
End If
bteLine = bteLine + 1
End If
If Not (blnStats) Then
'Skip --- DEST_HOST ping statistics --- string and add map with all ping's result to pingResult
If lneResult.IndexOf("ping statistics") > 0 Then
pingResult.mapResponse = icmp_seq
blnStats = True
End If
End If
Next
End If
End If
Return pingResult
End Sub
Last edited: