Pexpect and a Linux Terminal

prikar20 at gmail.com prikar20 at gmail.com
Tue Dec 25 15:49:44 EST 2007


On Dec 25, 8:42 am, "asga... at msn.com" <asga... at msn.com> wrote:
> On 25 déc, 10:14, "asga... at msn.com" <asga... at msn.com> wrote:
>
>
>
> > On 25 déc, 09:41, "asga... at msn.com" <asga... at msn.com> wrote:
>
> > > Yes it's work ! :-D
>
> > > I use  prompt = '.*#' to detect the prompt expect. Thank you for you'r
> > > help !
>
> > > Vive Python et TK :-D
>
> > I have another probleme, not directly from Pexpect() function. There
> > is my code :
>
> > from Tkinter import *
> > from sys import *
> > import tkMessageBox
> > from tkColorChooser import askcolor
> > from tkFileDialog   import askopenfilename
> > import tkFileDialog as Selector
> > from os.path import exists, join
> > from os import pathsep
> > import pexpect
> > import os, sys
> > def test():
> >  cmd1="su -"
> >  pwd="mypass"
> >  prompt ='.*#'
> >  iso=Selector.askopenfilename(initialdir="/home/user",filetypes =
> > [("iso", "*.iso")])
> >  lbl2=Label(fen1)
> >  cmd2="mount -o loop "+iso+" /mnt/disk"
> >  child = pexpect.spawn(cmd1)
> >  child.expect('Mot de passe :')
> >  child.sendline(pwd)
> >  child.expect(prompt)
> >  child.send(cmd2)
> >  lbl2.configure(text=cmd2)
> >  lbl2.pack()
> > fen1=Tk()
> > entr1=Entry(fen1)
> > lbl1=Label(fen1)
> > entr1.pack()
> > lbl1.pack()
> > bou1= Button(fen1,text='Parcourir',command=test)
> > bou1.pack()
> > fen1.mainloop()
>
> > All that's ok when if cmd2 command like : mkdir /root/toto but when i
> > want to replace it for : mount loop -o /home/user/myiso.iso /mnt/disk
> > nothing happened :-( I tryed the command during many times and i don't
> > know why it doesn't work :s
>
> > if you can help me another time i will be apprecied :-P
>
> > Thank you :)
>
> When want to test the mkdir command, it work but ONLY if my TTY as
> root is closed, very weired no ?
> the mount command still not work :s

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

Try sendline(cmd2). Most cases you may never need to use send() with
pexpect.
Since you used send here, the command is not yet entered to the shell;
it's as though you typed the command and forgot to press enter.

Again try adding a wait using child.expect(prompt). This will ensure
'cmd2' completed and then you may want to clean up using a call to
child.close()

If things still don't work, try some simple commands like you are
doing with mkdir. Of course you want to first ensure you can manually
do all the steps that you are trying to automate with pexpect.

Karthik






More information about the Python-list mailing list