是否可以使用telnetlib发送“resize pty”命令?(Is it possible to send “resize pty” command with telnetlib?)

编程入门 行业动态 更新时间:2024-10-10 09:15:09
是否可以使用telnetlib发送“resize pty”命令?(Is it possible to send “resize pty” command with telnetlib?)

我正在寻找一种在使用telnetlib时调整终端大小的方法。 我使用Paramiko的resize_pty在SSH上取得了类似的效果,但我也需要支持telnet协议。 这甚至可能(telnet甚至有控制流)?

请注意,telnetlib不是必需的 - 如果有更好的库,我会很乐意使用它。

更新 (更多背景):我正在构建一个基于Web的界面,用于连接到网络设备。 前端是使用JS / AJAX构建的,它基本上只是向后端发送击键并从中接收屏幕内容。 后端是用Python编写的,负责打开设备的SSH / telnet会话,向它发送击键并获取输出流,然后通过VT100虚拟终端( pyte )传递。 然后将虚拟屏幕的内容发送回前端。 当用户想要在他的浏览器中调整终端屏幕大小时,问题就出现了。 使用SSH我只需通过resize_pty()发送resize_pty() ,然后调整pyte的虚拟终端屏幕大小。 但是使用telnet,我无法找到适当的调整大小函数,告诉设备它应该调整其终端的大小。 这可能吗?

I am looking for a way to resize terminal when using telnetlib. I have achieved a similar effect on SSH with Paramiko's resize_pty, but I need to support telnet protocol too. Is this even possible (does telnet even have a control stream)?

Note that telnetlib is not a requirement - if there is a better library I would be happy to use it.

UPDATE (more background): I am building a web-based interface for connecting to networking devices. Frontend is built using JS/AJAX, it basically just sends keystrokes to backend and receives screen content from it. Backend is written in Python and takes care of opening a SSH/telnet session to a device, sends keystrokes to it and fetches an output stream, which is then passed through VT100 virtual terminal (pyte). The contents of virtual screen are then sent back to frontend. The problem arises when user wants to resize the terminal screen size in his browser. With SSH I just send resize_pty() through Paramiko and then also resize the pyte's virtual terminal screen size. But with telnet I was unable to find the appropriate resize function that would tell the device that it should resize its terminal. Is this possible?

最满意答案

好的,我已经能够组装以下杰作:

naws_command = struct.pack('!BBBHHBB', 255, 250, 31, # IAC SB NAWS width, height, 255, 240) # IAC SE t.get_socket().send(naws_command)

现在说几句话。

首先, telnetlib不支持直接发送命令; 它只是逃避了它们。 因此,要发送命令,我们必须直接使用底层套接字。 我们使用Telnet对象实例的get_socket()方法(这里是t )来实现。

此处汇编的NAWS命令由RFC 1073定义。 width和height变量是常规的Python整数,它们被打包成两个16位无符号整数。

请注意,这不是一个完美的解决方案,我不确定它是否真的适合您。 最重要的是,在功能协商期间, telnetlib将通知服务器它WON'T NAWS ,因此特定服务器实际上可能忽略这些命令。

如果是这种情况,您可能必须使用set_option_negotiation_callback() 。 遗憾的是,这意味着您必须处理telnetlib通常为您提供的所有选项。 而AFAIK则没有任何便利。

Ok, I've been able to assemble the following masterpiece:

naws_command = struct.pack('!BBBHHBB', 255, 250, 31, # IAC SB NAWS width, height, 255, 240) # IAC SE t.get_socket().send(naws_command)

Now a few words of explanation.

First of all, telnetlib does not support sending commands directly; it simply escapes them. Thus, to send the command we have to use the underlying socket directly. We do that using the get_socket() method of the Telnet object instance (t here).

The NAWS command assembled here is defined by RFC 1073. The width and height variables are regular Python integers which get packed into two 16-bit unsigned integers.

Note that this isn't a perfect solution and I'm not sure if it will actually work for you. Most importantly, during the capabilities negotiation, telnetlib will inform the server that it WON'T NAWS, so a particular server may actually ignore the commands.

If that's the case, you'd probably have to use set_option_negotiation_callback(). Sadly, that means you will have to handle all the options which normally telnetlib does for you. And AFAIK it has no conveniences for that.

更多推荐

本文发布于:2023-08-07 17:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   命令   telnetlib   pty   resize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!