Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open a telnet connection and run a few commands in C#

Tags:

c#

telnet

IS this straightforward? Does any one have any good examples? All my google searches return items on how to make telnet clients in dotNet but this overkill for me. I'm trying to do this in C#.

Thanks!

like image 920
Matt Avatar asked Jun 27 '09 19:06

Matt


People also ask

How do I create a telnet script?

Write the telnet session inside a BAT Dos file and execute. You cannot control / script an FTP session from inside a BATch file, you can only begin an FTP session. To script an FTP session you must use the script command -s:filename. txt to identify a text file containing your script.

What can I do after telnet connects?

View the current settings for Telnet Client. Type display for a list of the current operating parameters. If you are in a Telnet session (connected to a Telnet server), to modify the parameters, press Ctrl+] to leave the Telnet session. To return to the Telnet session, press Enter.

How do I run a shell script in telnet?

# execute some commands on the local system # access a remote system with an IP address: 10.1. 1.1 (for example) telnet 10.1. 1.1 # execute some commands on the remote system # log all the activity (in a file) on the Local system # exit telnet # continue on with executing the rest of the script.


2 Answers

C# 2.0 and Telnet - Not As Painful As It Sounds
http://geekswithblogs.net/bigpapa/archive/2007/10/08/C-2.0-and-Telnet---Not-As-Painful-As-It.aspx

Or this alternative link.

If you're going to use the System.Net.Sockets class, here's what you do:

  • Create an IPEndpoint, which points to the specified server and port. You can query DNS.GetHostEntry to change a computer name to an IPHostEntry object.
  • Create a socket object with the following parameters: AddressFamily.InterNetwork (IP version 4), SocketType.Stream (rides on InterNetwork and Tcp parameters), ProtocolType.Tcp (reliable, two-way connection)
  • Open the socket like this: socket.Connect(endpoint); //yup, it's that simple Send your data using socket.Send(... wait, I forgot something. You have to encode the data first so it can fly across them wires.
  • Use Encoding.ASCII.GetBytes to convert the nice message you have for the server into bytes. Then use socket.Send to send those bytes on their way.
  • Listen for a response (one byte at a time, or into a byte array) using socket.Receive Don't forget to clean up by calling socket.Close()

You can [also] use a System.Net.Sockets.TcpClient object instead of a socket object, which already has the socket parameters configured to use ProtocolType.Tcp. So let's walk through that option:

  1. Create a new TcpClient object, which takes a server name and a port (no IPEndPoint necessary, nice).
  2. Pull a NetworkStream out of the TcpClient by calling GetStream()
  3. Convert your message into bytes using Encoding.ASCII.GetBytes(string) Now you can send and receive data using the stream.Write and stream.Read methods, respectively. The stream.Read method returns the number of bytes written to your receiving array, by the way.
  4. Put the data back into human-readable format using Encoding.ASCII.GetString(byte array).
  5. Clean up your mess before the network admins get mad by calling stream.Close() and client.Close().
like image 62
Robert Harvey Avatar answered Sep 17 '22 18:09

Robert Harvey


For simple tasks (such as connecting to a specialized hardware device with telnet-like interface) connecting via socket and just sending and receiving text commands might be enough.

If you want to connect to real telnet server you might need to handle telnet escape sequences, face terminal emulation, handle interactive commands etc. Using some already tested code such as Minimalistic Telnet library from CodeProject (free) or some commercial Telnet/Terminal Emulator library (such as our Rebex Telnet) might save you some time.

Following code (taken from this url) shows how to use it:

// create the client 
Telnet client = new Telnet("servername");

// start the Shell to send commands and read responses 
Shell shell = client.StartShell();

// set the prompt of the remote server's shell first 
shell.Prompt = "servername# ";

// read a welcome message 
string welcome = shell.ReadAll();

// display welcome message 
Console.WriteLine(welcome);

// send the 'df' command 
shell.SendCommand("df");

// read all response, effectively waiting for the command to end 
string response = shell.ReadAll();

// display the output 
Console.WriteLine("Disk usage info:");
Console.WriteLine(response);

// close the shell 
shell.Close();
like image 32
Martin Vobr Avatar answered Sep 19 '22 18:09

Martin Vobr