Pexpect和GRUB - 为什么Pexpect会向我显示一个空白的GRUB菜单?

时间:2017-02-02 21:53:23

标签: boot pexpect grub

我正在尝试编写系统重启的脚本,我有几个GRUB条目。 Pexpect似乎没有"看到"菜单项。

以下是代码段:

def get_menu_selections(xtn):
    print "Waiting for GNU GRUB to show"
    xtn.expect_exact("GNU GRUB", timeout=480)
    time.sleep(3)
    xtn.expect_exact('Use the ^ and v keys')
    print xtn.before
    print xtn.after

def main():
    connection = pexpect.spawn('ssh -l user -p2288 1.2.3.4')
    # reboot box
    get_menu_selections(connection)

main()

解释为什么我的代码片段是这样的:一次" GNU GRUB"在屏幕上,然后我的超时停止,这意味着等待系统重启已经结束。在那一点上,我猜测GRUB绘制框,然后将其填入,所以我睡了3秒钟等待GRUB菜单的内容在屏幕上绘制。在我等待之后,我将匹配"使用^和v键"作为我的比赛所以我可以获得一个前后。

这就是我的GRUB的样子:

                  GNU GRUB  version 1.98+20100804-14+squeeze1

 +--------------------------------------------------------------------------+
 |Base OS                                                                   |
 |Base OS -> ttyS0                                                          |
 |Base OS (recovery mode)                                                   |
 |Base OS -> ttyS0 (recovery mode)                                          |
 |System Rescue                                                             |
 |System Rescue -> ttyS0                                                    |
 |                                                                          |
 +--------------------------------------------------------------------------+

  Use the ^ and v keys to select which entry is highlighted.
  Press enter to boot the selected OS, 'e' to edit the commands
  before booting or 'c' for a command-line.

  The highlighted entry will be executed automatically in 0s.

我没有看到菜单项,而是只看到绘制的轮廓和底部的文字。这是我的代码打印到屏幕上的内容:

+--------------------------------------------------------------------------+     
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
+--------------------------------------------------------------------------+

Use the ^ and v keys

我希望将菜单选项放入缓冲区("在","匹配"或""之后)以便我可以盘点。知道如何抓住菜单项吗?

1 个答案:

答案 0 :(得分:2)

我怀疑GRUB在打印菜单条目之前首先打印Use the ^ and v keys消息。所以试试这样:

def get_menu_selections(xtn):
    print "Waiting for GNU GRUB to show"
    xtn.expect_exact("GNU GRUB", timeout=480)
    time.sleep(3)
    xtn.expect_exact('Use the ^ and v keys')
    time.sleep(3)
    xtn.expect_exact('System Rescue -> ttyS0')
    print xtn.before
    print xtn.after