这是直截了当的吗?有人有什么好的例子吗?我所有的谷歌搜索都返回了关于如何在 dotNet 中制作 telnet 客户端的项目,但这对我来说太过分了。我正在尝试在 C# 中执行此操作。
谢谢!
C# 2.0 和 Telnet - 不像听起来那么痛苦
http://geekswithblogs.net/bigpapa/archive/2007/10/08/C-2.0-and-Telnet---Not-As-Painful-As-It.aspx
或者这个替代链接。
如果您要使用 System.Net.Sockets 类,请执行以下操作:
您可以 [还] 使用 System.Net.Sockets.TcpClient 对象而不是套接字对象,该对象已经将套接字参数配置为使用 ProtocolType.Tcp。所以让我们来看看这个选项:
对于简单的任务(例如连接到具有类似 telnet 接口的专用硬件设备),通过套接字连接并发送和接收文本命令可能就足够了。
如果您想连接到真正的 telnet 服务器,您可能需要处理 telnet 转义序列、面对终端仿真、处理交互式命令等。使用一些已经测试过的代码,例如CodeProject 的 Minimalistic Telnet 库(免费)或一些商业 Telnet/终端仿真器库(例如我们的Rebex Telnet)可能会为您节省一些时间。
以下代码(取自此 url)显示了如何使用它:
// 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();