Pexpect and a Linux Terminal

Karthik Gurusamy kar1107 at gmail.com
Mon Dec 24 21:50:01 EST 2007


On Dec 24, 6:06 pm, "asga... at msn.com" <asga... at msn.com> wrote:
> hello,
>
> I'm new in Python and i would like to use Pexpect to execute a root
> command (i want to mount via a Pyhton script a drive)
>
> so that's my script for the moment :
>
> from os import *
> import pexpect
> import os
> cmd1="su -"
> cmd2="mount -o loop /home/user/my.iso /mnt/disk"
> pwd="mypassword"
>
> child = pexpect.spawn(cmd1)
> child.sendline('Mot de passe :')

Make that child.expect('Mot de passe :')

> child.sendline(pwd+"\r\n")

With sendline no need for the trailing "\r\n". Just do
child.sendline(pwd)

Here you may want to do something like
prompt = '.*#'  # assumes your shell prompt for root ends in #

child.expect(prompt)

> child.sendline(cmd2)

Again add child.expect(prompt) so that you wait the completion of cmd2
and then child.close()

Karthik

>
> (is a French Terminal so 'Mot de passe' means Password :'
>
> After that i try to execute it, and nothing happened, i know how to
> lunch py file via python but i supposed the script don't detect the
> prompt Password.
>
> if anyone can help me please :)
>
> Have a nice day !




More information about the Python-list mailing list