By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

pexpect module

P: n/a
Someone know pexpect module?
I've a problem working with it...

I need to run some commands on remote hosts, like ls, df -k etc..All right.
Although, when i launch a command that have a "more" inside, i don't know
what i must expect!
An example:

if i want to run an "ls -l | more" on a remote host, it don't return me a
prompt, but a "-------------More-------------"...thus i don't know how much
this.

I've tried with:

child.expect('.*')

it works, but do not return any value.

Any idea?
I hope to have clearly explained my problem with my bad english...:-)

Thank you in advance
Regards

Gianluca Trombetta


Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Fri, 14 May 2004 19:50:54 +0200, Gianluca Trombetta wrote:
Someone know pexpect module?
I've a problem working with it...

I need to run some commands on remote hosts, like ls, df -k etc..All
right. Although, when i launch a command that have a "more" inside, i
don't know what i must expect!


Why don't you use a local more ? Or write your own "more" in python.

Simon.
Jul 18 '05 #2

P: n/a
On 2004-05-14, Gianluca Trombetta <gi****************@tin.it> wrote:
Someone know pexpect module?
I've a problem working with it...

I need to run some commands on remote hosts, like ls, df -k etc..All right.
Although, when i launch a command that have a "more" inside, i don't know
what i must expect!
An example:

if i want to run an "ls -l | more" on a remote host, it don't return me a
prompt, but a "-------------More-------------"...thus i don't know how much
this.

The only reason I can see for piping the command through more would
be if there were a person sitting there wanting to read the output
before going on to read the next page of output. That seems counter
to the idea of using pexpect to automate the process. Do you
really need to pipe through more?

Jul 18 '05 #3

P: n/a
hmmm, i'm not so stupid...:-).
The command ls -l | more was an example...i'm not really need to run ls -l |
more in an automate program.
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.

Hi
Gianluca

"Lee Harr" <mi*****@frontiernet.net> ha scritto nel messaggio
news:P9*****************@news01.roc.ny...
On 2004-05-14, Gianluca Trombetta <gi****************@tin.it> wrote:
Someone know pexpect module?
I've a problem working with it...

I need to run some commands on remote hosts, like ls, df -k etc..All right. Although, when i launch a command that have a "more" inside, i don't know what i must expect!
An example:

if i want to run an "ls -l | more" on a remote host, it don't return me a prompt, but a "-------------More-------------"...thus i don't know how much this.

The only reason I can see for piping the command through more would
be if there were a person sitting there wanting to read the output
before going on to read the next page of output. That seems counter
to the idea of using pexpect to automate the process. Do you
really need to pipe through more?

--
http://mail.python.org/mailman/listinfo/python-list



Jul 18 '05 #4

P: n/a
In article <ma************************************@python.org >,
Gianluca Trombetta <gi****************@tin.it> wrote:
hmmm, i'm not so stupid...:-).
The command ls -l | more was an example...i'm not really need to run ls -l |
more in an automate program.
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.

Jul 18 '05 #5

P: n/a
* Gianluca Trombetta <gi****************@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

Jul 18 '05 #6

P: n/a
First of all, look up the manual for Cisco's 'show' command. See if there
is a way to turn off the paging. If not, try to fool cisco into thinking
you have a very large page size on your terminal (e.g. 10,000 lines).

If this is not possible, you can loop in your python script -:

while 1:
i=child.expect('--More--',pexpect.TIMEOUT)
if i==0:
child.sendline(' ')
else:
break

Adrian.

Gianluca Trombetta wrote:
hmmm, i'm not so stupid...:-).
The command ls -l | more was an example...i'm not really need to run ls -l
| more in an automate program.
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.

Hi
Gianluca

"Lee Harr" <mi*****@frontiernet.net> ha scritto nel messaggio
news:P9*****************@news01.roc.ny...
On 2004-05-14, Gianluca Trombetta <gi****************@tin.it> wrote:
> Someone know pexpect module?
> I've a problem working with it...
>
> I need to run some commands on remote hosts, like ls, df -k etc..All right. > Although, when i launch a command that have a "more" inside, i don't know > what i must expect!
> An example:
>
> if i want to run an "ls -l | more" on a remote host, it don't return me a > prompt, but a "-------------More-------------"...thus i don't know how much > this.
>

The only reason I can see for piping the command through more would
be if there were a person sitting there wanting to read the output
before going on to read the next page of output. That seems counter
to the idea of using pexpect to automate the process. Do you
really need to pipe through more?

--
http://mail.python.org/mailman/listinfo/python-list


Jul 18 '05 #7

P: n/a
Ooops - forgot the search for the prompt :-(
Change the expect line to -:
i=child.expect(['--More--', PROMPT],pexpect.TIMEOUT)

(Assumes PROMPT evaluates to a regular expression matching the cisco
prompt).

Adrian Casey wrote:
First of all, look up the manual for Cisco's 'show' command. See if there
is a way to turn off the paging. If not, try to fool cisco into thinking
you have a very large page size on your terminal (e.g. 10,000 lines).

If this is not possible, you can loop in your python script -:

while 1:
i=child.expect('--More--',pexpect.TIMEOUT)
if i==0:
child.sendline(' ')
else:
break

Adrian.

Gianluca Trombetta wrote:
hmmm, i'm not so stupid...:-).
The command ls -l | more was an example...i'm not really need to run ls
-l
| more in an automate program.
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.

Hi
Gianluca

"Lee Harr" <mi*****@frontiernet.net> ha scritto nel messaggio
news:P9*****************@news01.roc.ny...
On 2004-05-14, Gianluca Trombetta <gi****************@tin.it> wrote:
> Someone know pexpect module?
> I've a problem working with it...
>
> I need to run some commands on remote hosts, like ls, df -k etc..All

right.
> Although, when i launch a command that have a "more" inside, i don't

know
> what i must expect!
> An example:
>
> if i want to run an "ls -l | more" on a remote host, it don't return
> me

a
> prompt, but a "-------------More-------------"...thus i don't know how

much
> this.
>
The only reason I can see for piping the command through more would
be if there were a person sitting there wanting to read the output
before going on to read the next page of output. That seems counter
to the idea of using pexpect to automate the process. Do you
really need to pipe through more?

--
http://mail.python.org/mailman/listinfo/python-list


Jul 18 '05 #8

P: n/a
Great!

I've disable the pager with the command "terminal length 0" and now it seem
work very well.
Thank you very much John and thank to all other!

Regards,
Gianluca Trombetta

"John Hazen" <jo**@hazen.net> ha scritto nel messaggio
news:20********************@gate2.hazen.net...
* Gianluca Trombetta <gi****************@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

--
http://mail.python.org/mailman/listinfo/python-list



Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.