471,354 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Pexpect and a Linux Terminal

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 :)

Have a nice day !
Dec 25 '07 #1
8 5679
On Dec 24, 6:06 pm, "asga...@msn.com" <asga...@msn.comwrote:
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 !
Dec 25 '07 #2

On Dec 24, 2007, at 7:06 PM, as*****@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

Dec 25 '07 #3
Yes it's work ! :-D

I use prompt = '.*#' to detect the prompt expect. Thank you for you'r
help !
Vive Python et TK :-D
Dec 25 '07 #4
On 25 déc, 09:41, "asga...@msn.com" <asga...@msn.comwrote:
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 :)
Dec 25 '07 #5
On 25 déc, 10:14, "asga...@msn.com" <asga...@msn.comwrote:
On 25 déc, 09:41, "asga...@msn.com" <asga...@msn.comwrote:
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 :)

One time this script with the mkdir command work, and one tine no... i
don't understand my problem, there is a TTY problem ?
Dec 25 '07 #6
On 25 déc, 10:14, "asga...@msn.com" <asga...@msn.comwrote:
On 25 déc, 09:41, "asga...@msn.com" <asga...@msn.comwrote:
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
Dec 25 '07 #7
On Dec 25, 8:42 am, "asga...@msn.com" <asga...@msn.comwrote:
On 25 déc, 10:14, "asga...@msn.com" <asga...@msn.comwrote:
On 25 déc, 09:41, "asga...@msn.com" <asga...@msn.comwrote:
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

Dec 25 '07 #8
On 25 déc, 15:49, prika...@gmail.com wrote:
On Dec 25, 8:42 am, "asga...@msn.com" <asga...@msn.comwrote:
On 25 déc, 10:14, "asga...@msn.com" <asga...@msn.comwrote:
On 25 déc, 09:41, "asga...@msn.com" <asga...@msn.comwrote:
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
Thank you for your help ! it work very fine !

Have a nice day
Dec 26 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Gianluca Trombetta | last post: by
2 posts views Thread by Adrian Casey | last post: by
reply views Thread by Eric Myers | last post: by
reply views Thread by Adrian Casey | last post: by
4 posts views Thread by fivestars | last post: by
4 posts views Thread by jonathan.sabo | last post: by
AmberJain
3 posts views Thread by AmberJain | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.