如何在 C# 中打开 telnet 连接并运行一些命令

发布于 2024-07-25 19:39:04 字数 117 浏览 5 评论 0原文

这简单吗? 有人有什么好的例子吗? 我所有的谷歌搜索都会返回有关如何在 dotNet 中创建 telnet 客户端的项目,但这对我来说太过分了。 我正在尝试用 C# 来做到这一点。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2024-08-01 19:39:05

对于简单的任务(例如连接到具有类似 telnet 接口的专用硬件设备),通过套接字连接并仅发送和接收文本命令可能就足够了。

如果您想连接到真正的 telnet 服务器,您可能需要处理 telnet 转义序列、面对终端仿真、处理交互式命令等。使用一些已经测试过的代码,例如 CodeProject 的简约 Telnet 库(免费)或一些商业 Telnet/终端仿真器库(例如我们的 Rebex Telnet)可能会节省您一些时间。

以下代码(取自此网址)显示了如何使用它:

// 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();

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();
疯了 2024-08-01 19:39:04

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 类,请执行以下操作:

  • 创建一个 IPEndpoint,它指向指定的服务器和端口。
    您可以查询 DNS.GetHostEntry 将计算机名称更改为
    IPHostEntry 对象。
  • 使用以下内容创建套接字对象
    参数:AddressFamily.InterNetwork(IP 版本 4)、
    SocketType.Stream(依赖于 InterNetwork 和 Tcp 参数),
    ProtocolType.Tcp(可靠,双向连接)
  • 像这样打开套接字
    这个:socket.Connect(端点); //是的,就是这么简单发送您的
    使用 socket.Send(... 等等,我忘记了一些东西。你必须
    首先对数据进行编码,以便它可以穿过电线。
  • 使用
    Encoding.ASCII.GetBytes 将您拥有的好消息转换为
    服务器到字节。 然后使用 socket.Send 将这些字节发送到它们的
    方式。
  • 监听响应(一次一个字节,或一个字节数组)
    使用 socket.Receive 不要忘记通过调用进行清理
    套接字.关闭()

您还可以使用 System.Net.Sockets.TcpClient 对象来代替
socket对象,已经配置了socket参数
使用 ProtocolType.Tcp。 那么让我们来看看这个选项:

  1. 创建一个新的 TcpClient 对象,它采用服务器名称和端口(不需要 IPEndPoint,很好)。
  2. 通过调用 GetStream() 从 TcpClient 中提取 NetworkStream
  3. 使用 Encoding.ASCII.GetBytes(string) 将消息转换为字节
    现在您可以分别使用stream.Write 和stream.Read 方法发送和接收数据。 顺便说一句,stream.Read 方法返回写入接收数组的字节数。
  4. 使用 Encoding.ASCII.GetString(byte array) 将数据恢复为人类可读的格式。
  5. 在网络管理员生气之前,通过调用stream.Close()和client.Close()来清理你的混乱。

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().
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文