[2005] Telnet client with vb.net-VBForums
Results 1 to 14 of 14

Thread: [2005] Telnet client with vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    [2005] Telnet client with vb.net

    I am trying to make an application which logs on to a server using the telnet protocol runs a couple of commands and stores the output of these commands in a text file.

    The problem is that I cant really seem to figure out how to negotiate the telnet commands. I have done a packet capture of a telnet session to different servers using a packet capturing program and I dont seem to find any fixed pattern. Every server negotiates the command in its own different way and in a different sequence, moreover the commands exchanged are not the same.

    What I want to do is to exchange the bare minimum commands which will allow me to log on to any server via telnet, run my commands, take the output and logout.

    I have searched the internet for such a set of 'general' telnet commands but I wasnt able to find any. Can anyone please help me out with this.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    89,091

    Re: [2005] Telnet client with vb.net

    Telnet is a specific protocol, like FTP or HTTP. If you want to know how to use it you simply need to look up the spec on the Web.

    http://www.google.com.au/search?q=te...ient=firefox-a

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: [2005] Telnet client with vb.net

    Telnet is a specific protocol, like FTP or HTTP. If you want to know how to use it you simply need to look up the spec on the Web.

    http://www.google.com.au/search?q=te...ient=firefox-a
    I know that telnet is a protocol and I know how to search on Google. Thankyou very much!!!!!!!!!!!!!

    But as I mentioned earlier, I am having trouble in understanding how to implement the protocol. I dont even want to implement it with its full blown options I just want enough to run a couple of commands on a server and get the output.

    I hope someone can post a link to some sort of tutorial of telnet rather than a mere link to a google search !!!!!!!!!!!

  4. #4
    Member
    Join Date
    May 2006
    Location
    UK
    Posts
    54

    Re: [2005] Telnet client with vb.net

    Is it the sockets aspect of the project you are having difficulty with or the data that needs to be sent received.

    <CrLf> is enter.

    Say for example your telnet client was connected to an POP mail server. To get the list of emails on the server you would send the text "LIST <CrLf>"

    The server would respond with the text:
    "+OK 3 messages (880 octets)<CrLf>
    1 120<CrLf>
    2 200<CrLf>
    3 560<CrLf>"

    Its just sending plain text over sockets.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: [2005] Telnet client with vb.net

    Is it the sockets aspect of the project you are having difficulty with or the data that needs to be sent received.

    <CrLf> is enter.

    Say for example your telnet client was connected to an POP mail server. To get the list of emails on the server you would send the text "LIST <CrLf>"

    The server would respond with the text:
    "+OK 3 messages (880 octets)<CrLf>
    1 120<CrLf>
    2 200<CrLf>
    3 560<CrLf>"

    Its just sending plain text over sockets.

    The socket aspect is not a problem. Implementing the initial negotiations of the telnet protocol is the real problem.

    In other protocols eg. in ftp, specific requests are sent by the client and the server always responds in the same specific manner to those commands and after the initial negotiations, the data transfer begins. But in telnet, from what I have studied so far, this is not the case. Different servers respond in completely different manner. This can also be seen from the packet captures which I did of two different telnet sessions using the Wireshark packet capture program. I have attached the capture output.

    Telnet commands are embedded in the data being sent.These are usually three byte commands. The structure of the telnet command is:

    IAC(255) 251(WILL) 32(Terminal Speed)

    all the command options can be seen from the link below:

    http://www.networksorcery.com/enp/pr...tm#Description

    as you can see there can be so many different combinations, I am really confused, and then there are also 'Sub Negotiations' (which I havent been able to understand at all).

    As you can see there is too much confusion . As I said earlier, I really dont want to handle all the options mentioned in the link above. I just want enough to be able to start sending and receiving data.
    Attached Files Attached Files

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: [2005] Telnet client with vb.net

    Oh come on! Has no one ever written a telnet client. I see so many places at work where it can save me loads of time. Plz help me out here.

  7. #7
    New Member
    Join Date
    Jun 2008
    Posts
    2

    Re: [2005] Telnet client with vb.net

    I feel your pain. I have been trying to figure this out on and off for quite a while. I am trying to do basically the same thing in c# and was having similar difficulties. I am connecting to a SUN OS server and only need to send one command. I finally had some success today that I thought I would share my results since my searches turned up many questions about it but no easy solutions. I finally figured out that you don't have to answer all of the server's requests for negotiations (or at least I didn't). The first thing I did after connecting up and getting the stream was to let the server know I wasn't interested in negotiating all the options that it seemed to want to do. The sun box was asking for the following:
    Do terminal type
    Do negotiate about window size
    Do x display location
    Do new environment option
    Do environment option

    I sent the following commands
    WONT terminal type
    WONT window size
    WONT x display location
    WONT new environment option
    WONT environment option

    I then sent
    DO echo
    DO suppress go ahead

    I was then able to watch for the server to finally give me the login: prompt I have been trying to get.

    Next I send the password followed by \r\n
    I then wait for the desired password again followed by \r\n
    For some reason (maybe the server wasn't quite ready for it or something) the server only received the \r\n
    I haven't figured out an easy fix for that yet but if I just resend the password \r\n am able to log in.
    I then wait for last login: and when I get that I can finally send my command.

    Here's the code I used. I am not an experienced programmer and people may find many flaws to this logic but this thing has irritated me so much that once I finally had some degree of success I wanted to share the wealth so hopefully I could save someone else a few hours. I wrote this in C# 2008 express but it should be very similar to what you are trying to do.

    Here are two methods or subs I created to cut down on the repetition.
    Code:
            public string WriteNReadBuffer(byte [] TelnetNegotiation, NetworkStream stream)
        {
                stream.Write(TelnetNegotiation, 0, TelnetNegotiation.Length);
                Byte[] data = new Byte[256];
                Int32 bytes = stream.Read(data, 0, data.Length);
                string returnval;
                returnval = BitConverter.ToString(data, 0, bytes) + "\r\n";
                //textBox2.AppendText(BitConverter.ToString(data, 0, bytes) + "\r\n");
                return returnval;
        }
            public void Wait4Prompt(string PromptSearch, NetworkStream stream)
            {
                Byte[] data6 = new Byte[256];
                Int32 bytes6 = stream.Read(data6, 0, data6.Length);
                string bufferstring = (BitConverter.ToString(data6, 0, bytes6) + "\r\n");
    
                //Wait for particular prompt - Look for hex value for login: or password:
                while ((bufferstring.Contains(PromptSearch) != true))
                {
                    Byte[] data7 = new Byte[256];
                    Int32 bytes7 = stream.Read(data7, 0, data7.Length);
                    bufferstring = (BitConverter.ToString(data7, 0, bytes7) + "\r\n");
                }
            }
    I had a simple form set up for my testing. You basically just need to create a form with a button and multiline text box. Adjust the code as needed. Here's the code that runs on the click of the button.
    Code:
            private void button3_Click(object sender, EventArgs e)
            {
                string server = "10.x.x.x";
                Int32 port = 23;
                TcpClient client = new TcpClient(server, port);
                NetworkStream stream = client.GetStream();
    
    //          WONT terminal type, window size, x display location, new environment option, environment option
                textBox2.AppendText(WriteNReadBuffer(new byte[] { 255, 252, 24, 255, 252, 31, 255, 252, 35, 255, 252, 39, 255, 252, 36 }, stream));
    
    //          DO echo, suppress go ahead
                textBox2.AppendText(WriteNReadBuffer(new byte[] { 255, 253, 01, 255, 253, 03 }, stream));
    
                //Wait for login prompt - Look for hex value for login:
                Wait4Prompt("6C-6F-67-69-6E-3A-20\r\n",stream);
    
    //          Send user1
                textBox2.AppendText(WriteNReadBuffer(new byte[] { 117, 115, 101, 114, 49, 13, 10 }, stream));
    
                //Wait for password prompt - Look for hex value for password:
                Wait4Prompt("50-61-73-73-77-6F-72-64-3A-20", stream);                    
    //          Send pass1
                textBox2.AppendText(WriteNReadBuffer(new byte[] { 112, 97, 115, 115, 49, 13, 10 }, stream));
    //          Send pass1
                textBox2.AppendText(WriteNReadBuffer(new byte[] { 112, 97, 115, 115, 49, 13, 10 }, stream));
    
    //         Wait for last login message - Look for hex value for last login:
                Wait4Prompt("4C-61-73-74-20-6C-6F-67-69-6E-3A-20", stream);
    //          Send ./sync
                textBox2.AppendText(WriteNReadBuffer(new byte[] { 46, 47, 115, 121, 110, 99, 13, 10 }, stream));
                
                stream.Close();
                client.Close(); 
    
    
            }
    The data being sent to the stream are the decimal versions of the required telnet commands.
    They are as follows:
    First set of commands -
    255 - Interpret as command (IAC) character
    252 - WONT
    24 - terminal type
    255 - IAC
    252 - WONT
    31 - negotiate about window size
    255 - IAC
    252 - WONT
    35 - x display location
    255 - IAC
    252 - WONT
    39 - new environment option
    255 - IAC
    252 - WONT
    36 - environment option

    Second Set of commands -
    255 - IAC
    253 - DO
    01 - echo
    255 - IAC
    253 - DO
    03 - suppress go ahead

    I am then waiting for "6C-6F-67-69-6E-3A-20\r\n" which is the hex equivalent of "login: "(there is also a CRLF on the end there)
    6c - l
    6F - o
    67 - g
    69 - i
    6e - n
    3a - :
    \r - CR
    \n - LF

    When that's found I send the username "user1" and \r\n
    117 - u
    115 - s
    101 - e
    114 - r
    49 - 1
    13 - CR
    10 - LR

    Then I wait for password: to be found the same as I did login:
    when that shows up I send my password which is the same as the user in this case. - user1 and \r\n.

    As I mentioned above for some reason the server only seems to see the \r\n so I send the password again. This time I have better success.

    Next I wait for "4C-61-73-74-20-6C-6F-67-69-6E-3A-20" which is "last login: "

    Once I get that I can finally send my command to run the script on my server which is "./sync" in my case which I add the \r\n to again.

    Next I close the stream and the client and am done.

    I hope this helps you or someone else out.

  8. #8
    New Member
    Join Date
    Apr 2010
    Posts
    1

    Thumbs up Re: [2005] Telnet client with vb.net

    Hi!

    Blaramore, I registered in this forum just to say thank you so much!
    You saved me a lot of time by explaining what were the "??$" messages!!

    Thank you again!


  9. #9
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: [2005] Telnet client with vb.net

    how about this for vb.net

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: [2005] Telnet client with vb.net

    Quote Originally Posted by justme369 View Post
    how about this for vb.net
    how about converting it. wont take very long and you might even be able to just use one of the free online C# -> VB converters
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11
    New Member
    Join Date
    Jun 2008
    Posts
    2

    Re: [2005] Telnet client with vb.net

    justme369,
    I used chris128's suggestion and converted the code to vb from c#. It seems to work fine with a little tweaking. I used vb express 2008 for this test and created a windows form application. I added a button and a multiline textbox. If you do that and use the code below you should be at a good starting point. I am able to connect to a sun server and execute a command which is all I needed to do. You will obviously want to change the ip address and username / password to match your scenario. I am attaching a pdf that shows the form in action and shows a capture of the negotiation in wireshark. This is really handy to diagnose any issues that may be going on. Check out my previous post for details on the telnet negotiation itself.
    Best of Luck
    Code:
    
    Public Class Form1
    
        Public Function WriteNReadBuffer(ByVal TelnetNegotiation As Byte(), ByVal stream As System.Net.Sockets.NetworkStream) As String
            stream.Write(TelnetNegotiation, 0, TelnetNegotiation.Length)
            TextBox1.AppendText("Command From Client:   " & BitConverter.ToString(TelnetNegotiation) & vbCr & vbLf)
            Dim data As [Byte]() = New [Byte](255) {}
            Dim bytes As Int32 = stream.Read(data, 0, data.Length)
            Dim returnval As String
            returnval = BitConverter.ToString(data, 0, bytes) & vbCr & vbLf
            Return returnval
        End Function
        Public Sub Wait4Prompt(ByVal PromptSearch As String, ByVal stream As System.Net.Sockets.NetworkStream)
            Dim data6 As [Byte]() = New [Byte](255) {}
            Dim bytes6 As Int32 = stream.Read(data6, 0, data6.Length)
            Dim bufferstring As String = (BitConverter.ToString(data6, 0, bytes6) & vbCr & vbLf)
            'Wait for particular prompt - Look for hex value for login: or password:
            While (bufferstring.Contains(PromptSearch) <> True)
                Dim data7 As [Byte]() = New [Byte](255) {}
                Dim bytes7 As Int32 = stream.Read(data7, 0, data7.Length)
                bufferstring = (BitConverter.ToString(data7, 0, bytes7) & vbCr & vbLf)
            End While
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim server As String = "10.20.62.10"
            Dim port As Int32 = 23
            Dim client As New System.Net.Sockets.TcpClient(server, port)
            Dim stream As System.Net.Sockets.NetworkStream = client.GetStream()
            Dim user1 As Byte() = System.Text.Encoding.ASCII.GetBytes("oracle" & vbCr & vbLf)
            Dim pass1 As Byte() = System.Text.Encoding.ASCII.GetBytes("oracle" & vbCr & vbLf)
            ' WONT terminal type, window size, x display location, new environment option, environment option
            TextBox1.AppendText("Response From Server: " & WriteNReadBuffer(New Byte() {255, 252, 24, 255, 252, 31, 255, 252, 35, 255, 252, 39, 255, 252, 36}, stream))
            ' DO echo, suppress go ahead
            TextBox1.AppendText("Response From Server: " & WriteNReadBuffer(New Byte() {255, 253, 1, 255, 253, 3}, stream))
            'Wait for login prompt - Look for hex value for login:
            Wait4Prompt("6C-6F-67-69-6E-3A-20" & vbCr & vbLf, stream)
            ' Send user1
            TextBox1.AppendText("Response From Server: " & WriteNReadBuffer(user1, stream))
            'Wait for password prompt - Look for hex value for password:
            Wait4Prompt("50-61-73-73-77-6F-72-64-3A-20", stream)
            ' Send pass1
            TextBox1.AppendText("Response From Server: " & WriteNReadBuffer(pass1, stream))
            ' Send pass1
            TextBox1.AppendText("Response From Server: " & WriteNReadBuffer(pass1, stream))
            ' Wait for last login message - Look for hex value for last login:
            Wait4Prompt("4C-61-73-74-20-6C-6F-67-69-6E-3A-20", stream)
            ' Send ./sync
            TextBox1.AppendText("Response From Server: " & WriteNReadBuffer(New Byte() {46, 47, 115, 121, 110, 99, 13, 10}, stream))
            stream.Close()
            client.Close()
        End Sub
    End Class
    Attached Images Attached Images

  12. #12
    New Member
    Join Date
    Mar 2011
    Posts
    1

    Re: [2005] Telnet client with vb.net

    hi!
    i am try this code in my project ,in sunos 5.8 can run successful,but in sunos 5.10 can't unsuccessful
    do you have any solution? thank you

  13. #13
    New Member
    Join Date
    Oct 2011
    Location
    Sichuan of China
    Posts
    1

    Re: [2005] Telnet client with vb.net

    Thanks blaramore,your program code is very usefull for me.

  14. #14
    Lively Member agent_007's Avatar
    Join Date
    Jan 2010
    Posts
    93

    Re: [2005] Telnet client with vb.net


    Im Pro in C#,VB.NET,Batch,Socket Programming, SPY Software Programming, VB6, Win32Api, VBscript, Windows Registry, ASP.NET, PHP, Jquery, AJAX.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.