从 Python 将按键插入 Linux 控制台

发布于 2024-11-04 04:52:41 字数 756 浏览 1 评论 0原文

我最近面临着一项相当奇怪的任务,其中一个结果是需要能够使用 DTMF(又名“按键音”)音来控制非 X Linux 计算机的终端。计算机有一个调制解调器,可以通过 ALSA 访问,因此可以通过 sox“rec”程序访问,这就是我正在读取输入的内容。否则,相关计算机是完全隔离的,没有任何以太网或其他网络接口。我使用的 Goertzel 算法实现效果非常好,eSpeak 语音合成引擎也是唯一的输出源;这应该适用于任何按键式电话。它读回输入(输入是八进制数字,一次一个 ASCII 字节)以及 dash shell 反馈的任何内容——提示符、命令的输出等,并使用 ASCII 助记符进行控制人物。

我当前使用的与 dash 交互的方法以及通过它启动的程序是 pexpect 模块。但是,我需要它能够根据需要读回光标所在行的全部内容,并且我不记得 pexpect 能够执行此操作(如果是,我不能说。)。我能想到的唯一其他解决方案是以某种方式使用 Python 来控制或充当键盘和控制台驱动程序。

这确实是唯一的方法(如果是的话,Python 是否可以实现?),或者是否有其他方法可以直接访问控制台的内容?

编辑:运气不好,我最近发现 PExpect 的 SVN 版本有 pexpect.screen。但是,它没有任何方法在其下实际运行程序。我得关注它的发展。

I have recently been faced with a rather odd task, one result being the necessity for the ability to use DTMF (aka "Touch Tone") tones to control a non-X Linux computer's terminal. The computer has a modem which can be accessed through ALSA, and therefore the sox "rec" program, which is what I am reading the input through. The computer in question is otherwise completely isolated, having no Ethernet or other network interfaces whatsoever. The Goertzel algorithm implementation I am using works very well, as does the eSpeak speech synthesis engine which is the only source of output; this is supposed to work with any Touch Tone phone. It reads back both input (input being octal digits, one ASCII byte at a time)and whatever the dash shell feeds back -- the prompt, the output from commands, etc., using ASCII mnemonics for control characters.

The current method that I am using for interacting with dash and the programs launched through it is the pexpect module. However, I need it to be able to, on demand, read back the entire contents of the line on which the cursor is positioned, and I do not recall pexpect being able to do this (If it is, I cannot tell.). The only other solution that I can think of is to somehow use Python to either control, or act as, the keyboard and console drivers.

Is this, indeed, the only way to go about it (and if so, is it even possible with Python?), or is there another way of having direct access to the contents of the console?

Edit: Through dumb luck, I just recently found that the SVN version of PExpect has pexpect.screen. However, it does not have any way of actually running a program under it. I'll have to keep an eye on its development.

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

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

发布评论

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

评论(2

心安伴我暖 2024-11-11 04:52:41

简单的解决方案是使用 Linux 内核 uinput 接口。它允许您将按键和鼠标事件插入内核,就像它们来自物理人机界面设备一样。这基本上会将您的应用程序变成键盘/鼠标。

由于您使用的是 Python,因此我建议您查看 python-uinput 模块。

如果您对 Python 中的二进制 I/O 感到满意,那么您当然可以在不使用任何库的情况下执行相同的操作;只需检查 /usr/include/linux/uinput.h 头文件中涉及的结构(接口完全稳定),也许还有一些 C 语言的 uinput 教程

请注意,访问 /dev/uinput/dev/input/uinput 设备(取决于您的发行版)通常需要 root 权限。我个人会以专用于该服务的用户和组的身份运行 Python 服务,并修改/添加 udev 规则(检查 rules.d 下的所有文件)以允许对 uinput 设备进行读写访问但是

SUBSYSTEM=="input", ENV{ID_INPUT}=="", IMPORT{builtin}="input_id"
KERNEL=="uinput", MODE="0660", GROUP="the-dedicated-group"

,如果您的 Python 应用程序只是执行程序,则应该将其设为终端模拟器 - 例如,使用 这个.您也可以使用 Python pty 来完成此操作,而无需任何额外的库;然而,主要工作是用 ANSI 转义序列模拟终端,这样应用程序就不会混淆,现有的终端模拟器有这样的代码。

The simple solution is to use the Linux kernel uinput interface. It allows you to insert keypresses and mouse events into the kernel, exactly as if they came from a physical human interface device. This would basically turn your application into a keyboard/mouse.

Since you are working with Python, I recommend you take a look at the python-uinput module.

If you are comfortable with binary I/O in Python, then you can certainly do the same without any libraries; just check out the /usr/include/linux/uinput.h header file for the structures involved (the interface is completely stable), and perhaps some uinput tutorials in C, too.

Note that accessing the /dev/uinput or /dev/input/uinput device (depending on your distribution) normally requires root privileges. I would personally run the Python service as a user and group dedicated to the service, and modify/add a udev rule (check all files under rules.d) to allow read-write access to the uinput device to that group, something like

SUBSYSTEM=="input", ENV{ID_INPUT}=="", IMPORT{builtin}="input_id"
KERNEL=="uinput", MODE="0660", GROUP="the-dedicated-group"

However, if your Python application simply executes programs, you should make it a terminal emulator -- for example, using this. You can do it too without any extra libraries, using the Python pty; the main work is to however simulate a terminal with ANSI escape sequences, so that applications don't get confused, and the existing terminal emulators have such code.

合久必婚 2024-11-11 04:52:41

如果您想操作控制台的内容,您可能需要使用curses。 此处对此有详细记录。查看window.getch()window.getyx()

If you want to manipulate the contents of the console, you probably want to use curses. It's well documented here. Look at window.getch() and window.getyx().

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