pexpect ssh login and ls | grep

Karthik Gurusamy kar1107 at gmail.com
Tue Jan 1 09:25:25 CET 2008


On Dec 31 2007, 6:46 pm, crybaby <joemystery... at gmail.com> wrote:
> 1) what are these characters:
> \x1b]0;
> ~\x07\x1b[?1034h
>
> in line '\x1b]0;my at mycomp2:~\x07\x1b[?1034h[my at mycomp2 ~]'?

These are probably escape sequences in your shell prompt string.
Typically they are interpreted by the terminal, like xterm, to update
title bar.

>
> 2) Also, how come I don't get 0 or 2(excuting ls command exit code)
> from result.split('\r\n')[0] or result.split('\r\n')[1] ?

I don't think your expect worked fine to capture the desired output.

>
> This is what I get:>>> import pexpect
> >>> child=pexpect.spawn('ssh my at mycomp2')
> >>> child.sendline("ls mytest.log > /dev/null 2>&1; echo $?")
> 41
> >>> child.before
> >>> print child.before
> None
> >>> print child.after
> None

before/after makes sense only after an expect. At this point there is
only a sendline; not expect. So the above None output is expected.

> >>> child.expect([pexpect.TIMEOUT, '\$ '])
> 1

1 implies it matched the second pattern. You want to use raw strings.
r'\$' or else the re sent down is a plain $ which re interprets as end
of buffer.
Most important here is your prompt doesn't end with a $ (it's
something like [my at mycomp2 ~]). Thus make it,

child.expect(r'.*]')

Try the ls command and rest of the statements.

Karthik

> >>> result=child.before
> >>> print result.split('\r\n')[1]
> [my at mycomp2 ~]
> >>> print result.split('\r\n')[0]
>
> Last login: Mon Dec 31 20:52:09 2007 from com1>>> print result.split('\r\n')
>
> ['Last login: Mon Dec 31 20:52:09 2007 from com1\r',
> '\x1b]0;my at mycomp2:~\x07\x1b[?1034h[my at mycomp2 ~]']




More information about the Python-list mailing list