473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 19699
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

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

Similar topics

3
by: jtagpgmr | last post by:
I need help coming up with a way to create a c# program to help me Telnet to a hostIP.. Any suggestions.. or a way to start..
4
by: vee10 | last post by:
hi, I need help to connect to database(SQLSERVER2005) using javascript please help me
5
by: chathu03j | last post by:
how do you connect a MS Access database to a vb application using the ODBC connection?
3
by: yogesh | last post by:
how to connect MySQL using C++ in redhat .... can any one tell a sample of code to creat ,connect and execute the statement in c++ code ... please help me .....
1
by: ibnusina202003 | last post by:
hi, how to autologin telnet using ckermit
3
by: arasub | last post by:
ep 20, 2007 11:25:57 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found...
0
by: moyoal | last post by:
Dear All, this is my first experience to connect forms and database using VPN / DSL from other location. we have client/server architectures, .FMX are available in 212.147.1.63 server. ...
0
by: VIGIN | last post by:
Dear All, I am trying to connect my Sever using dyndns with php code. My Code is $dsn = 'DRIVER={SQL Server};SERVER=xxxx-xxxxx.dyndns.org,1028\SQLEXPRESS;DATABASE=Cxxxxxxx6'; $connection =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.