I need to execute a telnet-command (cat /proc/stb/info/model) on a server and read the result for further processing. (I can't intervene on the server since it uses a standard BusyBox incorporated in the firmware.)
I have managed to create a telnet-session and write code so I can login, send password and execute the above mentioned cat-command. The problem is reading and capturing the result of the cat-command.
This is probably due to the fact that I don't know how to write proper networking-code. What I would like to do is to be able, as mentioned above, to read and capture the result of the cat-command for further processing but I don't know how to do it properly.
In any case, I noted I can get the result by using log(msg) and if it is possible, perhaps there is way to parse the logs.
In addition, I noted some strange things in the sense that sometimes it seems like some code is not executed. For instance, the msgbox I put in for testing is only being executed in debug-mode and when it has a corresponding break-point. Please see my notes in the code I am posting here. I am also posting below the log I get using this telnet-session.
Some comments would be appreciated. Maybe I have taken not the correct approach to handle a telnet-session? Anything I should change in the code?
Thanks!
Edit: based on code-example from user rbsoft. Thx
Code:
Log:
I have managed to create a telnet-session and write code so I can login, send password and execute the above mentioned cat-command. The problem is reading and capturing the result of the cat-command.
This is probably due to the fact that I don't know how to write proper networking-code. What I would like to do is to be able, as mentioned above, to read and capture the result of the cat-command for further processing but I don't know how to do it properly.
In any case, I noted I can get the result by using log(msg) and if it is possible, perhaps there is way to parse the logs.
In addition, I noted some strange things in the sense that sometimes it seems like some code is not executed. For instance, the msgbox I put in for testing is only being executed in debug-mode and when it has a corresponding break-point. Please see my notes in the code I am posting here. I am also posting below the log I get using this telnet-session.
Some comments would be appreciated. Maybe I have taken not the correct approach to handle a telnet-session? Anything I should change in the code?
Thanks!
Edit: based on code-example from user rbsoft. Thx
Code:
B4X:
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim AStreams As AsyncStreams
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim btnConnect As Button
Dim InfoArrived As Boolean
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime = True Then
btnConnect_Click
End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnConnect_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.1.6",23,5000) 'telnet-session
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
Log("Connected")
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
Else
Log("Server not available")
End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg=BytesToString(Buffer,0,Buffer.Length,"UTF-8")
Log(msg)
If msg.IndexOf("login:") <> -1 Then
SendData("root" & CRLF)
End If
If msg.IndexOf("Password:") <> -1 Then
SendData("dreambox" & CRLF)
End If
If InfoArrived = False Then 'the boolean InfoArrived is used so the cat command is not being sent anymore
If msg.IndexOf("root@") <> -1 Then
InfoArrived=True
SendData("cat /proc/stb/info/model" & CRLF)
End If
End If
If msg.Trim = "dm7020hd" Then 'seems to be true only in debug-mode when msgbox has a breakpoint enabled. Speed issue?
'Log(msg)
Log("FINISHED") - 'it is not being written in the log. Why?
Msgbox("FINISHED","") 'works in debug-mode but only with BreakPoint enabled on msgbox
AStreams.Close
Socket1.Close
End If
End Sub
Sub SendData(msg As String)
Dim Buffer() As Byte
Delay
Buffer = msg.GetBytes("UTF8")
AStreams.Write(Buffer)
End Sub
Sub Delay
'create some delay - don't know if it is necessary
For x = 1 To 10000
x=x+1
Next
End Sub
Log:
PackageAdded: package:b4a.telnettest
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Connected
????????
OpenDreambox 1.6.0 dm7020hd
dm7020hd login:
root
Password:
root@dm7020hd:~#
c
at /proc/stb/info/model
dm7020hd
root@dm7020hd:~#
** Activity (main) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Last edited: