期望 - 根据行和列从屏幕区域获取变量 [英] Expect - get variable from screen region based on row and column
问题描述
我正在使用 Expect 自动与 SSH 会话和 ERP 程序交互.
I'm auto-interacting with an SSH session and an ERP program using Expect.
不是依靠正则表达式来捕获我的预期脚本中的变量,是否有可能在收到用户的某个击键后将屏幕区域(例如一个字段)捕获到代码中的变量中?向服务器发送一些其他命令并重新发送字段?
Rather than relying on a regular expression to capture a variable in my expect script would it be possible upon receiving a certain keystroke from a user to capture a screen region, say one field, into a variable in the code? Send the server some other commands and resend the field?
假设订单号包含在 6、12、6、18(其中 6 是行,12-18 是列)包含我的 6 位订单号.我想从第 6 行第 12 到 18 列中获取该订单号,将其复制到一个变量中.然后允许用户进行更多交互(或期望移动到另一个菜单),然后在另一个菜单中重新发送订单号.
Say an order number is contained at 6, 12, 6, 18 (where 6 is the row and 12-18 are the columns) containing my 6 digit order number. I want to get that order number from row 6 columns 12 to 18 copy that into a variable. Then allow the user to interact some more (or expect a move into another menu), then re-send the order number in another menu.
所以我想我的问题是:当前屏幕的内容是否在一个缓冲区中?(不是整个会话)您能否仅提取仅存在于屏幕上该行和列范围内的某个数据元素?
So I guess my question is: Are the current screen's contents in one buffer? (not the whole session) Can you extract just a certain data element that would only exist at that row and column range on the screen?
示例伪代码:
#!/usr/bin/expect -f
set env(TERM) vt100
spawn ssh -Y user@domain
#... set user/pass and other vars...
#... send commands to log into ERP
#don't time out
set timeout -1
interact {
-reset $CTRLZ {exec kill -STOP [pid]}
$CTRLA {
exp_send "menu_address
y
"
}
$CTRLO {
#...acquire order number variable...
#...some code I don't understand yet...
exp_send "menu_exit_sequence
"
exp_send "menu_address
"
exp_send $ordernumvar
}
~~
}
推荐答案
不,您不能从屏幕上的特定行/列抓取某些内容.但是,首先要考虑信息是如何到达屏幕的.如果它是一个使用curses 的基于tty 的应用程序,它会使用特殊的转义序列输出到stdout,导致它出现在该行/列.因此,期望"那些特定的转义序列以获取该位置的内容.
No, you can't grab something off of the screen at a particular row/column. However, think about how the information got to the screen in the first place. If it's a tty-based application using curses it was output to stdout with special escape sequences that caused it to appear at that row/column. So, 'expect' those specific escape sequences to get what is at that position.
这篇关于期望 - 根据行和列从屏幕区域获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!