Python:通过SSH发送按键事件 [英] Python: sending key press events over SSH

查看:705
本文介绍了Python:通过SSH发送按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何使用Python 2.7 Paramiko模块在远程服务器(机上没有X.org)上模拟按键事件.我需要相应地发送 F2 Enter .

I am trying to find out how to simulate key press events on remote server (wich has no X.org aboard) using Python 2.7 Paramiko module. I need to send F2 and Enter consequentially.

根据此讨论我已经实现了以下代码:

According to this discussion I have implemented this code:

import paramiko
client = paramiko.SSHClient()
client.connect(...)
channel = client.get_transport().open_session()
channel.get_pty("vt100")
channel.settimeout(100)

假定 F2 等于'\e[12~' Python字符串(这来自提到的答案 xterm控制序列),我正在尝试将其发送到远程服务器:

Assuming that F2 is equal to the '\e[12~' Python string (this follows from mentioned answer and xterm control sequences) I am trying to send it to the remote server:

channel.send('\e[12~')

此后脚本挂起.我究竟做错了什么?谢谢.

After that the script hangs. What am I doing wrong? Thank you.

推荐答案

首先,我建议 PDF版本;它最初是为纸张设计的,并且无法自动转换为HTML.

Firstly, I recommend the PDF version of the xterm control sequences document; it was originally designed for paper and the automatic conversion to HTML is not perfect.

终端仿真器的历史悠久而复杂,但是有一点是,VT100仅具有四个功能键(PF1至PF4),并且当其后续产品VT220添加了另外16个功能键时,原始的编码方案就用光了房间,所以他们切换到了另一个房间...但是出于兼容性考虑,他们使用F1到F4的旧编码.因此,尽管F5为CSI 15 ~,但F2为CSI 12 ~并非正确.相反,特别是如果您要告诉Paramiko您要模拟VT100,则应使用F2的VT100编码,它是SSE Q'\eOQ作为Python字符串.

The history of terminal emulators is long and complicated, but one point is that the VT100 only had four function keys (PF1 through PF4), and when its successor the VT220 added another 16 function keys, the original coding scheme ran out of room, so they switched to a different one... but they used the old encodings of F1 through F4 for compatibility's sake. Therefore, although F5 is CSI 15 ~, it's not true that F2 is CSI 12 ~. Instead, especially if you're telling Paramiko that you're impersonating a VT100, you should use the VT100 encoding of F2, which is SSE Q or '\eOQ as a Python string.

我不知道这到底是不是导致您的问题的原因,但这可能是其中的一部分.

I don't know if that's exactly what's causing your problem, but it's probably part of it.

这篇关于Python:通过SSH发送按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆