如何通过Perl以编程方式控制交互式Unix应用程序? [英] How can I control an interactive Unix application programmatically through Perl?

查看:86
本文介绍了如何通过Perl以编程方式控制交互式Unix应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个已有20年历史的交互式命令行unix应用程序,该应用程序的供应商不再支持该应用程序.我们需要在此应用程序中自动化一些任务.

其中最麻烦的是创建数千个新记录,这些记录的参数略有不同(例如,不同的标识符,不同的名称).必须按顺序创建记录,一次创建一个,这将需要数月(因此需要美元)来手动进行.在大多数情况下,创建记录的命令键入,读取响应,键入其他命令等的模式非常可预测.但是,某些记录创建操作将导致错误条件(具有此标识符的记录已存在")一组不同的命令可以正常退出.

我可以看到几种不同的方法:

  • 命名管道.编写一个Perl脚本,该脚本在STDIN和STDOUT设置为命名管道的情况下运行目标应用程序,然后向目标应用程序发送命令序列以创建具有所需参数的记录,然后指示目标应用程序退出并关闭.然后,我们使用不同的参数根据需要运行脚本多次.

  • 应用程序.查找另一个可用于编写交互式程序脚本的Unix工具.我唯一能找到的是期望,但这似乎并没有得到最好的维持.和聊天,我很久以前就记得,它似乎或多或少地满足了我的要求,但似乎仅用于控制调制解调器.

另一个潜在的复杂性:我认为目标应用程序是为VT100终端编写的,它使用某种转义序列来进行诸如突出显示之类的事情.

我的问题是我应该采取什么方法?其中之一,还是完全不同的东西?我非常喜欢使用命名管道,然后拥有一个Perl脚本来打开FIFO并根据需要进行读写的想法,因为它提供了很多灵活性,但是从我的阅读看来,如果存在很多潜在的问题,我走这条路.

谢谢.

解决方案

我肯定会坚持使用Perl来获得额外的灵活性,正如混乱所暗示的那样.您是否知道 Expect perl模块?比命名管道方法好得多.

还请注意使用命名管道,您不能强制将来自旧应用程序的输出缓冲,这可能会很烦人.我认为 Expect.pm使用伪tty来解决此问题,但我不确定.请参阅 perlipc 中的讨论.有关更多详细信息,请参见href ="http://www.perl.com/doc/manual/html/pod/perlipc.html#Bidirectional_Communication_with" rel ="nofollow noreferrer">与另一个进程进行双向通信" . /p>

I have inherited a 20-year-old interactive command-line unix application that is no longer supported by its vendor. We need to automate some tasks in this application.

The most troublesome of these is creating thousands of new records with slightly different parameters (e.g. different identifiers, different names). The records have to be created in sequence, one at a time, which would take many months (and therefore dollars) to do manually. In most cases, creating a record has a very predictable pattern of keying in commands, reading responses, keying in further commands, etc. However, some record creation operations will result in error conditions ('record with this identifier already exists') that require a different set of commands to be exit gracefully.

I can see a few different ways to do this:

  • Named pipes. Write a Perl script that runs the target application with STDIN and STDOUT set to named pipes then sends the target application the sequence of commands to create a record with the required parameters, and then instructs the target application to exit and shut down. We then run the script as many times as required with different parameters.

  • Application. Find another Unix tool that can be used to script interactive programs. The only ones I have been able to find though are expect, but this does not seem top be maintained; and chat, which I recall from ages ago, and which seems to do more-or-less what I want, but appears to be only for controlling modems.

One more potential complication: I think the target application was written for a VT100 terminal and it uses some sort of escape sequences to do things like provide highlighting.

My question is what approach should I take? One of these, or something completely different? I quite like the idea of using named pipes and then having a Perl script that opens the FIFOs and reads and writes as required, as it provides a lot of flexibility, but from what I have read it seems like there's a lot of potential problems if I go down this path.

Thanks in advance.

解决方案

I'd definitely stick to Perl for the extra flexibility, as chaos suggested. Are you aware of the Expect perl module? It's a lot nicer than the named pipe approach.

Note also with named pipes, you can't force the output coming back from your legacy application to be unbuffered, which could be annoying. I think Expect.pm uses pseudo-ttys to get around this problem, but I'm not sure. See the discussion in perlipc in the section "Bidirectional Communication with Another Process" for more details.

这篇关于如何通过Perl以编程方式控制交互式Unix应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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