473,382 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 5847
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gianluca Trombetta | last post by:
Someone know pexpect module? I've a problem working with it... I need to run some commands on remote hosts, like ls, df -k etc..All right. Although, when i launch a command that have a "more"...
2
by: Adrian Casey | last post by:
I have a collection of tcl expect scripts which I am converting to python using the excellent pexpect module (http://pexpect.sourceforge.net/). So far I've had great success in getting all my...
0
by: Eric Myers | last post by:
Hello folks: (This message is also posted on the help forum at the pexpect sourceforge page, but all indentation in the code got stripped away when I submitted the post.) For some time I've...
0
by: Adrian Casey | last post by:
I have a python script which uses pexpect and I want to timeout (i.e. raise pexpect.TIMEOUT) if a long running command does not produce the output I am expecting. To simulate the 'long running...
4
by: fivestars | last post by:
Hi there. I'm computer science student at the end of my degree. I'm new about python. I've a question for all of you. Do you know how to write, from python code, on a unix(linux) terminal...
4
by: Stylus277 | last post by:
Hello, I am working with a client that has an old Linux server with a custom cbase program. The server has been humming along since 1989, with the exception of a HDD fail in 1994, which was...
4
by: jonathan.sabo | last post by:
I have a pexpect script to walk through a cisco terminal server and I was hoping to get some help with this regex because I really suck at it. This is the code: index = s.expect() if index...
2
by: vaskarbasak | last post by:
Hi all, i tried to run Linux command from Java Application. Here i past my code:- package com; public class linux_java { public static void main(String args) {
3
AmberJain
by: AmberJain | last post by:
HELLO, I have some queries. Please help. Let me tell you experts that I'm a linux newbie and an ABSOLUTE UNIX illiterate person. I know a bit of linux and can easily handle GUI of Red hat...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.