Search code examples
vb.netsocketstelnet

telnet options negotiation issue


I'm trying to write a telnet client that we can use to automate a bunch of commands we normally send to the server. When I use this to connect to our server, I get the Telnet Options Negotiation commands first, and then the welcome message, followed by the username: prompt. I sent the proper DONT commands, but the server just echoes them and doesn't respond to anything. Below is my code. Hopefully someone can make some sense of this and what I'm doing wrong. Let me know if there's anything else I can elaborate on.

Imports System.Text
Imports System.Net.Sockets
Module Module1
    Dim Full_Stop As String = ""
    Dim TelnetClient As New TcpClient
    Sub Main()
        TelnetClient.Connect("telnet.server.com", 23) 'Connecting to the IP Given
        Dim thr As New Threading.Thread(AddressOf Receive_thread) 'define the thread that will handle data received
        thr.Start() ' start the thread
        Dim SendData As String = ""
        While SendData <> "quit" 'loop until we quit the program
            SendData = Console.ReadLine
            If SendData <> "quit" Then
                Send_Sub(SendData) 'send a command
            End If
        End While
        Full_Stop = "Stop" 'kill the thread
        TelnetClient.Close()
    End Sub
    Sub Send_Sub(ByVal msg As String)
        Dim byt_to_send() As Byte = System.Text.Encoding.ASCII.GetBytes(msg)
        TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None)
    End Sub
    Sub Receive_thread()
re:
        If Full_Stop = "Stop" Then Exit Sub 'If you set Full_Stop string to "Stop" the thread will end
        If TelnetClient.Client.Available > 0 Then 'Check if there is any Data to receive
            Dim byt_to_receive(TelnetClient.Available - 1) As Byte
            TelnetClient.Client.Receive(byt_to_receive, 0, byt_to_receive.Length, SocketFlags.None) 'receive the data
            For lc = 0 To byt_to_receive.Length - 2 'go through the entire byte array
                If byt_to_receive(lc) = 255 Then 'looking for commands
                    Select Case byt_to_receive(lc + 1)
                        Case 251 'if a WILL command is received
                            Dim byt_to_send() As Byte = {255, 254, lc + 2} 'send a DONT command
                            TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None)
                        Case 253 'if a DO command is received
                            Dim byt_to_send() As Byte = {255, 252, lc + 2} 'send a WONT command
                            TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None)
                    End Select
                End If
            Next
            Dim String_From_Byte As String = System.Text.Encoding.ASCII.GetString(byt_to_receive)
            Console.Write(String_From_Byte)
        End If
        GoTo re 'this will NOT allow the thread to End by sending it back to re: statement, unless the Full_Stop is "Stop"
    End Sub
End Module

Solution

  • Fixed! I was sending vbnewline instead of sending {10, 13} as byte.

    Not I have to figure out why I'm getting a bunch of garbage with the text I want...