I am attempting to script a reboot of a system and I have several GRUB entries. Pexpect doesn't seem to "see" the menu items.
Here is a code snippet:
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()
To explain why my snippet is like it is: Once "GNU GRUB" is on the screen, then my timeout stops, which means the wait for the system to reboot is over. At that point, I am guessing that GRUB draws the box, then fills it in, so I'm sleeping 3 seconds to wait for the content of the GRUB menu to be drawn on the screen. After I wait, then I was going to match on "Use the ^ and v keys" as my match so I can get a before and after.
This is what my GRUB looks like:
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.
Instead of seeing the menu items, I'm only seeing the drawn outline and the text at the bottom. This is what my code prints to the screen:
+--------------------------------------------------------------------------+
| |
| |
| |
| |
| |
+--------------------------------------------------------------------------+
Use the ^ and v keys
I'd like to get the menu options into a buffer ("before", "match", or "after") so that I can take inventory. Any idea how to grab the menu items?
I suspect that GRUB first prints the Use the ^ and v keys
messages before printing the menu entries. So try like this:
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