Pexpect and a Linux Terminal

Michael Bentley michael at jedimindworks.com
Mon Dec 24 21:59:56 EST 2007


On Dec 24, 2007, at 7:06 PM, asgarde 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 :')
> child.sendline(pwd+"\r\n")
> child.sendline(cmd2)
>
> (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 :)
>

Sure thing -- you're almost there!  I suspect that you're sending 'Mot  
de passe :' as a response to the 'Mot de passe :' prompt ;-) Just  
change it to something like:

child = pexpect.spawn(cmd1)
child.expect('Mot de passe :')
child.sendline(pwd)
child.expect('#')
child.sendline(cmd2)

hth,
Michael

---
Our network was brought down by a biscuit??? --Steven D'Aprano






More information about the Python-list mailing list