pexpect module

John Hazen john at hazen.net
Mon May 17 17:07:08 EDT 2004


* Gianluca Trombetta <gianluca.trombetta at tin.it> [2004-05-17 09:10]:
> But I need to run some cisco commands like "show ip bgp summary", this
> command print a table and put a "More" by default, if the table is too big
> for a single terminal screen.
> So I need to remove this behavior because if I match the " --More-- " with
> expect module i can match only the first one "More", then this the program
> crash and receives a timeout.

Why don't you post your code and the traceback you're getting.  I think
you should be able to expect the "More" multiple times.

Try using expect with a list argument, and detect which one matched.

[untested]

console = pexpect.spawn("whatever_command_you_use")
# probably other stuff here (enabling, etc.) ...
console.sendline("sh ip bgp summ")
# look for more or prompt
return_index = console.expect([" --More-- ","#"])
while return_index == 0:
    console.send(" ")
    return_index = console.expect([" --More-- ","#"])
# matched prompt, so now you're good to go.


Note, that you may have to save the console.before in the while loop, so
you don't lose it before you match the prompt.

HTH-

John

P.S. [OT]: The cisco command I use to disable the pager is:
# terminal length 0




More information about the Python-list mailing list