简体   繁体   English

编写telnet客户端

[英]Writing a telnet client

HI, 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. 补充:最终我希望通过web api / webservice公开它。 The platform is linux. 该平台是linux。

If Python is an option you could use telnetlib . 如果Python是一个选项,你可以使用telnetlib

Code example : 代码示例

#!/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()

telnet's protocol is pretty straightforward... you just create a TCP connection, and send and receive ASCII data. telnet的协议非常简单......你只需创建一个TCP连接,然后发送和接收ASCII数据。 That's pretty much it. 这就是它。

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()周围的简单事件循环就足够了。

While telnet is almost just a socket tied to a terminal it's not quite. 虽然telnet几乎只是一个连接到终端的套接字,但它并不完全。 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(或tshark或tcpdump)并监控您的连接。 Wireshark (formerly ethereal) is cross platform and pretty easy to use for simple stuff. Wireshark(以前称为ethereal)是跨平台的,非常容易用于简单的东西。 Filter with tcp.port == 23 使用tcp.port == 23过滤

可能对您有用。

Download Putty source code. 下载Putty源代码。 Examine TELNET.C, the Telnet backend for Putty. 检查TELNET.C,Putty的Telnet后端。

我真的觉得Beej的网络编程指南很好地介绍了C语言中的网络编程。

Unless the application is trivial, a better starting point would be to figure out how you're going to create the GUI. 除非应用程序是微不足道的,否则更好的起点是弄清楚如何创建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. 你首先提到C,但后来开始谈论Python,这让我相信你在这件事上相对灵活。

Once you are set on a language/platform, then look for a telnet library -- you should find something reasonable already implemented. 一旦你在语言/平台上设置,然后寻找一个telnet库 - 你应该找到已经实现的合理的东西。

See options in your telnet client: on an arbitrary listening socket and hit escape] to enter the client. 请参阅telnet客户端中的选项:在任意侦听套接字上并按“ escape]进入客户端。

telnet> help
telnet> set ?

Check out the source code here . 在这里查看源代码。 It has helped me out a lot in understanding Telnet protocol. 它帮助了我很多理解Telnet协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM