469,352 Members | 2,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to connect Telnet using .NET?

10
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..
Jul 29 '10 #1

✓ answered by PsychoCoder

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
Expand|Select|Wrap|Line Numbers
  1. private static void WriteIn(string input)
  2. {
  3.     if (stream.CanWrite)
  4.     {
  5.         writeBuffer = System.Text.Encoding.ASCII.GetBytes(input);
  6.         stream.Write(writeBuffer, 0, writeBuffer.Length);
  7.     }
  8. }
ReadOut
Expand|Select|Wrap|Line Numbers
  1. private static string ReadOut()
  2. {
  3.     string output = null;
  4.  
  5.     if (stream.CanRead)
  6.     {
  7.         readBuffer = new byte[tcpClient.ReceiveBufferSize];
  8.         stream.Read(readBuffer, 0, tcpClient.ReceiveBufferSize);
  9.  
  10.         output = System.Text.Encoding.ASCII.GetString(readBuffer).Trim();
  11.     }
  12.  
  13.     return output;
  14. }
For this example we have 4 variables that are global to our application

Expand|Select|Wrap|Line Numbers
  1. private static NetworkStream stream;
  2. private static TcpClient tcpClient;
  3. private static byte[] writeBuffer;
  4. private static byte[] readBuffer;
And our Main method does all the communicating with the telnet server:

Main
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2. {
  3.     try
  4.     {
  5.         using (tcpClient = new TcpClient())
  6.         {
  7.             Console.WriteLine("Please enter the host IP:");
  8.             string host = Console.ReadLine();
  9.  
  10.             Console.WriteLine("Please provide the port to connect on:");
  11.             int port = int.Parse(Console.ReadLine());
  12.  
  13.             tcpClient.Connect(host, port);
  14.  
  15.             using (stream = tcpClient.GetStream())
  16.             {
  17.                 Console.WriteLine(ReadOut());
  18.  
  19.                 Console.WriteLine("Enter your username:");
  20.                 WriteIn(Console.ReadLine());
  21.  
  22.                 Console.WriteLine(ReadOut());
  23.  
  24.                 Console.WriteLine("Enter your password:");
  25.                 WriteIn(Console.ReadLine());
  26.  
  27.                 Console.WriteLine(ReadOut());
  28.  
  29.  
  30.             }
  31.         }
  32.         //keep it open
  33.         Console.ReadLine();
  34.     }
  35.     catch (SocketException ex)
  36.     {
  37.         Console.WriteLine("Socket Error: {0}", ex.Message);
  38.     }
  39.     catch (Exception ex)
  40.     {
  41.         Console.WriteLine("ERROR: {0}", ex.Message);
  42.     }
  43. }
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

6 18492
ThatThatGuy
449 Expert 256MB
@dyte
what you can do is create a batch file with the above code...
and execute the batch file using Process class
Jul 29 '10 #2
dyte
10
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?
Aug 1 '10 #3
PsychoCoder
465 Expert Mod 256MB
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
Expand|Select|Wrap|Line Numbers
  1. private static void WriteIn(string input)
  2. {
  3.     if (stream.CanWrite)
  4.     {
  5.         writeBuffer = System.Text.Encoding.ASCII.GetBytes(input);
  6.         stream.Write(writeBuffer, 0, writeBuffer.Length);
  7.     }
  8. }
ReadOut
Expand|Select|Wrap|Line Numbers
  1. private static string ReadOut()
  2. {
  3.     string output = null;
  4.  
  5.     if (stream.CanRead)
  6.     {
  7.         readBuffer = new byte[tcpClient.ReceiveBufferSize];
  8.         stream.Read(readBuffer, 0, tcpClient.ReceiveBufferSize);
  9.  
  10.         output = System.Text.Encoding.ASCII.GetString(readBuffer).Trim();
  11.     }
  12.  
  13.     return output;
  14. }
For this example we have 4 variables that are global to our application

Expand|Select|Wrap|Line Numbers
  1. private static NetworkStream stream;
  2. private static TcpClient tcpClient;
  3. private static byte[] writeBuffer;
  4. private static byte[] readBuffer;
And our Main method does all the communicating with the telnet server:

Main
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2. {
  3.     try
  4.     {
  5.         using (tcpClient = new TcpClient())
  6.         {
  7.             Console.WriteLine("Please enter the host IP:");
  8.             string host = Console.ReadLine();
  9.  
  10.             Console.WriteLine("Please provide the port to connect on:");
  11.             int port = int.Parse(Console.ReadLine());
  12.  
  13.             tcpClient.Connect(host, port);
  14.  
  15.             using (stream = tcpClient.GetStream())
  16.             {
  17.                 Console.WriteLine(ReadOut());
  18.  
  19.                 Console.WriteLine("Enter your username:");
  20.                 WriteIn(Console.ReadLine());
  21.  
  22.                 Console.WriteLine(ReadOut());
  23.  
  24.                 Console.WriteLine("Enter your password:");
  25.                 WriteIn(Console.ReadLine());
  26.  
  27.                 Console.WriteLine(ReadOut());
  28.  
  29.  
  30.             }
  31.         }
  32.         //keep it open
  33.         Console.ReadLine();
  34.     }
  35.     catch (SocketException ex)
  36.     {
  37.         Console.WriteLine("Socket Error: {0}", ex.Message);
  38.     }
  39.     catch (Exception ex)
  40.     {
  41.         Console.WriteLine("ERROR: {0}", ex.Message);
  42.     }
  43. }
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
Aug 1 '10 #4
dyte
10
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..
Aug 2 '10 #5
dyte
10
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..
Aug 3 '10 #6
MMcCarthy
14,534 Expert Mod 8TB
@dyte

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

Mary
Aug 4 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by jtagpgmr | last post: by
1 post views Thread by ibnusina202003 | last post: by
reply views Thread by VIGIN | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.