How can I open a telnet connection and run a few c

2019-02-08 17:38发布

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!

标签: c# telnet
2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-08 17:47

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();
查看更多
登录 后发表回答