【问题标题】:return information using pexpect使用 pexpect 返回信息
【发布时间】:2012-06-01 05:30:52
【问题描述】:

我正在学习如何使用 pexpect,我有一个问题。我正在尝试查找远程服务器的版本号,并根据该版本号选择正确的驱动程序号。我不完全确定我应该如何去做。类似于 getline 函数的东西。让我知道是否有任何我可以使用的功能

【问题讨论】:

    标签: pexpect


    【解决方案1】:

    .before.after 属性分别包含标准输出。在匹配的期望之后。

    import pexpect
    
    child = pexpect.spawn('cat /etc/issue')
    child.expect('Ubuntu.*')
    print(child.after)
    

    哪些命令需要输入?如果是驱动安装,你可以使用fabric & fexpect:

    from ilogue.fexpect import expect, expecting, run
    
    output = run('get-system-version')
    
    prompts = []
    prompts += expect('Which driver version do you want to install?',output)
    
    with expecting(prompts):
        run('install-driver-command')
    

    【讨论】:

    • 这个想法是做一个cat /etc/issue 并检查版本号(使用正则表达式)。所以我想要的是能够得到cat /etc/issue 的输出我不能使用fabric 或fexpect,因为我是在以前的工作的基础上完成的,这些工作都是在pexpect 中完成的:(
    • 好的,我添加了一个例子;试试这个脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多