bash 如何:将 shell 中的击键输入模拟到在嵌入式目标中运行的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4545532/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:10:25  来源:igfitidea点击:

How to: Simulating keystroke inputs in shell to an app running in an embedded target

bashshellkeyboardembedded-linuxkeystroke

提问by fzkl

I am writing an automation script that runs on an embedded linux target.

我正在编写一个在嵌入式 linux 目标上运行的自动化脚本。

A part of the script involves running an app on the target and obtaining some data from the stdout. Stdout here is the ssh terminal connection I have to the target.

脚本的一部分涉及在目标上运行应用程序并从标准输出获取一些数据。这里的标准输出是我与目标的 ssh 终端连接。

However, this data is available on the stdout only if certain keys are pressed and the key press has to be done on the keyboard connected to the embedded target and not on the host system from which I have ssh'd into the target. Is there any way to simulate this?

但是,只有在按下某些键并且按键必须在连接到嵌入式目标的键盘上而不是在我通过 ssh 连接到目标的主机系统上完成时,才能在 stdout 上使用此数据。有没有办法模拟这个?

Edit: Elaborating on what I need -

编辑:详细说明我需要什么 -

I have an OpenGL app that I run on the embedded linux (works like regular linux) target. This displays some graphics on the embedded system's display device. Pressing f on the keyboard connected to the target outputs the fps data onto the ssh terminal from which I control the target.

我有一个 OpenGL 应用程序,可以在嵌入式 linux(像普通 linux 一样工作)目标上运行。这会在嵌入式系统的显示设备上显示一些图形。在连接到目标的键盘上按 f 将 fps 数据输出到我控制目标的 ssh 终端上。

Since I am automating the process of running this OpenGL app and obtaining the fps scores, I can't expect a keyboard to be connected to the target let alone expect a user to input a keystroke on the embedded target keyboard. How do I go about this?

由于我正在自动化运行此 OpenGL 应用程序并获取 fps 分数的过程,因此我不能期望键盘连接到目标,更不用说期望用户在嵌入式目标键盘上输入按键。我该怎么做?

Edit 2: Expect doesn't work since expect can issue strokes only to the ssh terminal. The keystroke I need to send to the app has to come from the keyboard connected to the target (this is the part that needs simulation without actually having a keyboard connected to it).

编辑 2:Expect 不起作用,因为 expect 只能向 ssh 终端发出笔画。我需要发送到应用程序的击键必须来自连接到目标的键盘(这是需要模拟但实际上没有连接键盘的部分)。

Thanks.

谢谢。

采纳答案by Oppermann

I'm not at home right now (so no linux at hand), so can't actually try it out. But you should be able to emulate keystrokes if you echo the desired keypresses (possibly the keyboard codes) to the keyboard node located under /dev on your target. This could be done through your ssh.

我现在不在家(所以手头没有 linux),所以实际上无法尝试。但是,如果您将所需的按键(可能是键盘代码)回显到目标上 /dev 下的键盘节点,则您应该能够模拟击键。这可以通过您的 ssh 完成。

回答by Cameron Laird

This is exactlythe domain of Expect, which stackoverflow incidentally recognizes with its own tag.

正是Expect的域,stackoverflow 偶然地用自己的标签识别了它。

The quickest way to achieve the OpenGL automation you're after, while learning as little of Expect as necessary, is likely by way of autoexpect.

实现您所追求的 OpenGL 自动化的最快方法,同时尽可能少地学习 Expect,可能是通过autoexpect的方式。

回答by Ben Hymanson

A solution which would satisfy both QA and manufacturing test is to build a piece of hardware which looks like a keyboard to the embedded device and has external control. Depending on how complex your input needs to be this could be anything from a store-bought keyboard with the spacebar taped down to a microcontroller talking PS/2 or USB on the keyboard side and something else (serial, USB, ethernet) on the control side.

一种同时满足 QA 和制造测试的解决方案是构建一个硬件,它看起来像嵌入式设备的键盘并具有外部控制。根据您输入的复杂程度,这可以是任何东西,从商店购买的带有空格键的键盘到键盘侧的 PS/2 或 USB 微控制器以及控件上的其他东西(串行、USB、以太网)边。

With the LUFAlibrary it is remarkably easy to make a USB keyboard with AT90USB series parts. Some of them even have 2 USB ports and could be automated by USB connected to another system (or if you want to get cheeky you could have it enumerate both as a keyboard and the control device and loop the keyboard input through the embedded system).

使用LUFA库,使用 AT90USB 系列部件制作 USB 键盘非常容易。其中一些甚至有 2 个 USB 端口,并且可以通过连接到另一个系统的 USB 自动化(或者,如果你想变得厚颜无耻,你可以将它作为键盘和控制设备进行枚举,并通过嵌入式系统循环键盘输入)。

回答by Demiurg

How about echo "f" > /dev/console ?

怎么样 echo "f" > /dev/console ?

回答by vehumet

How about create text file with inputs and run as follows?

如何使用输入创建文本文件并按如下方式运行?

cat inputs.txt | target_executable

Contents of inputs.txt can be something like follows,

input.txt 的内容可以是这样的,

y
y
y
n
y