B4J Question Redirect result from Shell on Rasberry Pi

Peter Lewis

Well-Known Member
Licensed User
Longtime User
Hi,
I am trying to redirect the results on a ping or the results of a tracert.
The shell just has standard outputs, like Success but the actual text I want is what goes to the Logs Window



This is the LOGS window, I want between out and error to be saved to the lblresult.text label
exit code 0
out
Pinging djpeter.co.za [173.212.203.13] with 32 bytes of data:
Reply from 173.212.203.13: bytes=32 time=190ms TTL=47
~l0144302349:
Ping statistics for 173.212.203.1
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 190ms, Maximum = 190ms, Average = 190ms
error
Success true

B4X:
Private Sub btnPing_Click
    Dim sh2 As Shell
    Dim apd As String = dtaPingAddress.Text
    sh2.Initialize("sh2","ping",Array(apd, "-n ", "1"))
    sh2.Run(1000)
End Sub

Sub sh2_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    lblResult.Text="Successful " & Success
    Pane7.Left=0
    Pane7.Top=0
    Pane7.BringToFront
    Log("exit code "&ExitCode)
    Log("out "&StdOut)
    Log("error "&StdErr)
    Log("Success "&Success)
End Sub

In Pane7 I have a Results field which i want to see all the pings and information from the ping or tracert

Thank you
 
Solution
I solved it by using the redirect code from @Erel

B4X:
Private Sub btnPing_Click
    Dim sh2 As Shell
    Dim apd As String = dtaPingAddress.Text
    sh2.Initialize("sh2","ping",Array(apd, "-n ", "1"))
    sh2.Run(1000)
End Sub

Sub sh2_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    RedirectOutput(File.DirApp, "logsping.txt")
        Pane7.Left=5
        Pane7.Top=0
        Pane7.BringToFront
        Log("exit code "&ExitCode)
        Log("out "&StdOut)
        Log("error "&StdErr)
        Log("Success "&Success)
    lblResult.Text= File.ReadString(File.DirApp,"logsping.txt")
End Sub


Sub RedirectOutput (Dir As String, FileName As String)

   Dim out As OutputStream =...

Peter Lewis

Well-Known Member
Licensed User
Longtime User
I solved it by using the redirect code from @Erel

B4X:
Private Sub btnPing_Click
    Dim sh2 As Shell
    Dim apd As String = dtaPingAddress.Text
    sh2.Initialize("sh2","ping",Array(apd, "-n ", "1"))
    sh2.Run(1000)
End Sub

Sub sh2_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    RedirectOutput(File.DirApp, "logsping.txt")
        Pane7.Left=5
        Pane7.Top=0
        Pane7.BringToFront
        Log("exit code "&ExitCode)
        Log("out "&StdOut)
        Log("error "&StdErr)
        Log("Success "&Success)
    lblResult.Text= File.ReadString(File.DirApp,"logsping.txt")
End Sub


Sub RedirectOutput (Dir As String, FileName As String)

   Dim out As OutputStream = File.OpenOutput(Dir, FileName, False) 'Set to True to append the logs
   Dim ps As JavaObject
   ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
   Dim jo As JavaObject
   jo.InitializeStatic("java.lang.System")
   jo.RunMethod("setOut", Array(ps))
   jo.RunMethod("setErr", Array(ps))

End Sub
 
Upvote 0
Solution
Top