How to connect Telnet using .NET?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dyte
    New Member
    • Jul 2010
    • 10

    How to connect Telnet using .NET?

    Hello,

    I need to connect to telnet. For eg, assume that, I entered in command window;

    C:\telnet 10.41.xx.xx

    How to do this in C#?

    Thanks anyway..
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by dyte
    Hello,

    I need to connect to telnet. For eg, assume that, I entered in command window;

    C:\telnet 10.41.xx.xx

    How to do this in C#?

    Thanks anyway..
    what you can do is create a batch file with the above code...
    and execute the batch file using Process class

    Comment

    • dyte
      New Member
      • Jul 2010
      • 10

      #3
      First of all thank you for answering and I am sorry for about answering so late..
      But, can you say me how can I handle overloading proccess problem ie. login and password ... with your method?

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        Another option you have is to use a TcpClient and NetworkStream to communicate with a telnet server. Here's a small example of doing this (It's setup in a console application for this example).

        We got 2 methods (other than Main), one for writing data to the steam and one for reading the response received back:

        WriteIn
        Code:
        private static void WriteIn(string input)
        {
            if (stream.CanWrite)
            {
                writeBuffer = System.Text.Encoding.ASCII.GetBytes(input);
                stream.Write(writeBuffer, 0, writeBuffer.Length);
            }
        }
        ReadOut
        Code:
        private static string ReadOut()
        {
            string output = null;
        
            if (stream.CanRead)
            {
                readBuffer = new byte[tcpClient.ReceiveBufferSize];
                stream.Read(readBuffer, 0, tcpClient.ReceiveBufferSize);
        
                output = System.Text.Encoding.ASCII.GetString(readBuffer).Trim();
            }
        
            return output;
        }
        For this example we have 4 variables that are global to our application

        Code:
        private static NetworkStream stream;
        private static TcpClient tcpClient;
        private static byte[] writeBuffer;
        private static byte[] readBuffer;
        And our Main method does all the communicating with the telnet server:

        Main
        Code:
        static void Main(string[] args)
        {
            try
            {
                using (tcpClient = new TcpClient())
                {
                    Console.WriteLine("Please enter the host IP:");
                    string host = Console.ReadLine();
        
                    Console.WriteLine("Please provide the port to connect on:");
                    int port = int.Parse(Console.ReadLine());
        
                    tcpClient.Connect(host, port);
        
                    using (stream = tcpClient.GetStream())
                    {
                        Console.WriteLine(ReadOut());
        
                        Console.WriteLine("Enter your username:");
                        WriteIn(Console.ReadLine());
        
                        Console.WriteLine(ReadOut());
        
                        Console.WriteLine("Enter your password:");
                        WriteIn(Console.ReadLine());
        
                        Console.WriteLine(ReadOut());
        
                                
                    }
                }
                //keep it open
                Console.ReadLine();
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Socket Error: {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }
        Once you enter your username & password then you can issue your commands with WriteIn and use ReadOut to read the response.

        Hope this helps and gives you another option

        Comment

        • dyte
          New Member
          • Jul 2010
          • 10

          #5
          OK, it works thank you. But now the problem is it works even if I enter incorrect password..!?

          If I use process method and than connect to
          "telnet 10.51.12.1"
          then in the process, first I need to press a key combination like <shift+a> and than <username> and <password>..
          I mean, when i connect telnet by process method and run it, then it opens in its own command window. Then I have to press some keys there, as i said above.

          Can this be done?

          Thanks again..

          Comment

          • dyte
            New Member
            • Jul 2010
            • 10

            #6
            OK, I think I have done it by using
            >> SendKeys.send(" ");
            command.
            Now, the aim is to reach all the vlans and so finally the MACs.
            Thank you all again..

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              @dyte

              Don't forget to mark the post you consider as the best answer.

              Mary

              Comment

              Working...