Problems with telnet

moster67

Expert
Licensed User
Longtime User
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:

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:

 
Last edited:

moster67

Expert
Licensed User
Longtime User
Thanks for your suggestion.

Can't test right now since I am at work but I don't think it will work since the word I am looking for, in this case, dm7020hd is also included in the telnet prompt (as you can see from the log posted) and in this case I think the contain-method would return true whenver dm7020hd is present in "msg".

Ideal would be to find the way how to read the reply produced by the cat-command (without knowing before-hand the result).

what happens if you use msg.contains instead of msg.trim?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Ideal would be to find the way how to read the reply produced by the cat-command (without knowing before-hand the result).
This can only happen if you keep a variable LastCommand, and parse the result based on what was last sent.

This does look a bit like a buffering problem, as you said, when you add a pause, the If condition triggers, but then how would the Log(msg) command be executed.
You can also look at Buffer.Length to see how much data there is.
 
Upvote 0

nicieri

Active Member
Licensed User
Longtime User
I can't connect to the localhost

I'm trying the same.. send a telnet command

but i can't connect to the ip with port 23.. always is "server not available"

i'm trying with the ip:

Socket1.Connect("192.168.1.6",23,5000) 'telnet-session

and

Dim MyLan As ServerSocket
MyLan.Initialize(0, "")
MyLan.Listen
Msgbox(MyLan.GetMyIP, "")
Socket1.Connect(MyLan.GetMyIP,23,5000) 'telnet

ALWAYS: "Server not available"


I need some special permission or open the port or the port is in use? or what?


Thanks.






 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…