python 编写 telnet 客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2519598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a telnet client
提问by MAC
HI,
你好,
I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.
我有一个暴露 telnet 接口的设备,您可以使用用户名和密码登录该接口,然后操纵设备的工作。
I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.
我必须编写一个 C 程序,从客户端隐藏 telnet 方面,而是为用户提供一个界面来控制设备。
What would be a good way to proceed. I tried writing a simple socket program but it stops at the login prompt. My guess is that i am not following the TCP protocol.
什么是进行的好方法。我尝试编写一个简单的套接字程序,但它在登录提示处停止。我的猜测是我没有遵循 TCP 协议。
Has anyone attempted this, is there an opensource library out there to do this?
有没有人尝试过这个,有没有开源库可以做到这一点?
Thanks
谢谢
Addition: Eventually i wish to expose it through a web api/webservice. The platform is linux.
补充:最终我希望通过 web api/webservice 公开它。平台为linux。
回答by compie
If Python is an option you could use telnetlib.
如果 Python 是一个选项,您可以使用telnetlib。
代码示例:
#!/usr/bin/env python
import getpass
import sys
import telnetlib
HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
回答by Jeremy Friesner
telnet's protocol is pretty straightforward... you just create a TCP connection, and send and receive ASCII data. That's pretty much it.
telnet 的协议非常简单……您只需创建一个 TCP 连接,然后发送和接收 ASCII 数据。差不多就是这样。
So all you really need to do is create a program that connects via TCP, then reads characters from the TCP socket and parses it to update the GUI, and/or writes characters to the socket in response to the user manipulating controls in the GUI.
因此,您真正需要做的就是创建一个通过 TCP 连接的程序,然后从 TCP 套接字读取字符并解析它以更新 GUI,和/或将字符写入套接字以响应用户在 GUI 中操作控件。
How you would implement that depends a lot on what software you are using to construct your interface. On the TCP side, a simple event loop around select() would be sufficient.
您将如何实现这在很大程度上取决于您使用什么软件来构建您的界面。在 TCP 端,一个围绕 select() 的简单事件循环就足够了。
回答by nategoose
While telnet is almost just a socket tied to a terminal it's not quite. I believe that there can be some control characters that get passed shortly after the connection is made. If your device is sending some unexpected control data then it may be confusing your program. If you haven't already, go download wireshark (or tshark or tcpdump) and monitor your connection. Wireshark (formerly ethereal) is cross platform and pretty easy to use for simple stuff. Filter with tcp.port == 23
虽然 telnet 几乎只是一个绑定到终端的套接字,但它并不完全是。我相信在建立连接后不久可能会传递一些控制字符。如果您的设备正在发送一些意外的控制数据,那么它可能会混淆您的程序。如果您还没有,请下载wireshark(或tshark 或tcpdump)并监控您的连接。Wireshark(前身为ethereal)是跨平台的,对于简单的东西来说非常容易使用。使用 tcp.port == 23 过滤
回答by Frank
I really find Beej's Guide to Network Programminga good introduction to network programming in C.
我真的发现Beej 的网络编程指南很好地介绍了 C 中的网络编程。
回答by systempuntoout
回答by Tiberiu Ana
Unless the application is trivial, a better starting point would be to figure out how you're going to create the GUI. This is a bigger question and will have more impact on your project than how exactly you telnet into the device. You mention C at first, but then start talking about Python, which makes me believe you are relatively flexible in the matter.
除非应用程序是微不足道的,否则更好的起点是弄清楚您将如何创建 GUI。这是一个更大的问题,对您的项目的影响比您如何准确地 telnet 到设备更重要。你一开始提到C,然后开始谈论Python,这让我相信你在这件事上比较灵活。
Once you are set on a language/platform, then look for a telnet library -- you should find something reasonable already implemented.
一旦你在一种语言/平台上进行设置,然后寻找一个 telnet 库——你应该找到一些已经实现的合理的东西。
回答by sivabudh
回答by bbaassssiiee
See options in your telnet client: on an arbitrary listening socket and hit escape]
to enter the client.
查看您的 telnet 客户端中的选项:在任意侦听套接字上并点击escape]
进入客户端。
telnet> help
telnet> set ?