borgen DD-WRT Novice
Joined: 17 Mar 2014 Posts: 10
|
Posted: Tue Apr 08, 2014 8:12 Post subject: Connect to dd-wrt router through Visual Basic Application |
|
Hello,
Trying to create a simple Visual Basic application that connects to a dd-wrt router and collects some information. But I am stuck with a problem that I hope someone can help me with.
I can login to the box but then it wont accept any commands that I send.
Here are my simple code so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim session As New System.Net.Sockets.TcpClient
Dim networkstream As Net.Sockets.NetworkStream
Dim bytes(session.ReceiveBufferSize) As Byte
Dim ne_data As String
Dim ne_data2 As String
Dim ne_data3 As String
Dim send_command1("UserName".Length) As Byte
Dim send_command2("Password".Length) As Byte
Dim send_command3("Command".Length) As Byte
session.Connect("192.168.1.1", 23)
If session.Connected Then
Debug.Print("connected")
If session.GetStream.CanRead Then
networkstream = session.GetStream
Debug.Print("Sending user name")
send_command1 = System.Text.Encoding.ASCII.GetBytes("root" & vbCrLf)
networkstream.Write(send_command1, 0, send_command1.Length)
Threading.Thread.Sleep(2000) ' wait to receive data
networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))
ne_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Debug.Print("Device said NE1:" & ne_data & vbCrLf)
Debug.Print("Sending password")
send_command2 = System.Text.Encoding.ASCII.GetBytes("qwerty" & vbCrLf)
networkstream.Write(send_command2, 0, send_command2.Length)
Threading.Thread.Sleep(2000) ' wait to receive data
networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))
ne_data2 = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Debug.Print("Device said NE2:" & ne_data2 & vbCrLf)
Debug.Print("Sending Help command")
send_command3 = System.Text.Encoding.ASCII.GetBytes("help" & vbCrLf)
networkstream.Write(send_command3, 0, send_command3.Length)
Threading.Thread.Sleep(2000) ' wait to receive data
ne_data3 = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Debug.Print("Device said NE3:" & ne_data3 & vbCrLf)
networkstream.Close()
session.Close()
End If
End If
End Sub |
|
Tsaukpaetra DD-WRT Novice
Joined: 27 Sep 2008 Posts: 40
|
Posted: Fri Jul 04, 2014 5:23 Post subject: |
|
I don't believe you should be sending both a CR and LF after each command.
Since the issue with your app is the client side (I'm assuming you can telnet with a normal app just fine), I suggest a quick Google for "VB.net telnet client" will probably be better for you.
It seems that your code is quite a bit more complicated than it should be.
Something like this from post #4 from this thread:
Code: |
Dim client As Net.Sockets.TcpClient
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim ns As Net.Sockets.NetworkStream
Dim response As String
'connecting to a server
client = New Net.Sockets.TcpClient
Try
client.Connect("127.0.0.1", 23) ' Change the IP to your liking.
Catch e As Exception
MessageBox.Show("Failed to connect: " & e.ToString, "Error")
Return
End Try
ns = client.GetStream
sr = New IO.StreamReader(ns, Encoding.ASCII, True) ' this sends our commands to the server
sw = New IO.StreamWriter(ns) ' and this will read server's response
sw.WriteLine("admin")
sw.WriteLine("password")
' Read the stream further to get the output
|
Though the comments from the code above got the stream reader/writer backwards. |
|