Pexpect and a Linux Terminal

A

asgarde

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 !
 
K

Karthik Gurusamy

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
 
M

Michael Bentley

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
 
A

asgarde

Yes it's work ! :-D

I use prompt = '.*#' to detect the prompt expect. Thank you for you'r
help !


Vive Python et TK :-D
 
A

asgarde

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

asgarde

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 ?
 
A

asgarde

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
 
P

prikar20

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
 
A

asgarde

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top