473,734 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c md1)
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 5871
On Dec 24, 6:06 pm, "asga...@msn.co m" <asga...@msn.co mwrote:
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(c md1)
child.sendline( 'Mot de passe :')
Make that child.expect('M ot 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(pr ompt)
child.sendline( cmd2)
Again add child.expect(pr ompt) 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(c md1)
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(c md1)
child.expect('M ot 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.co m" <asga...@msn.co mwrote:
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.as kopenfilename(i nitialdir="/home/user",filetypes =
[("iso", "*.iso")])
lbl2=Label(fen1 )
cmd2="mount -o loop "+iso+" /mnt/disk"
child = pexpect.spawn(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
child.send(cmd2 )
lbl2.configure( text=cmd2)
lbl2.pack()
fen1=Tk()
entr1=Entry(fen 1)
lbl1=Label(fen1 )
entr1.pack()
lbl1.pack()
bou1= Button(fen1,tex t='Parcourir',c ommand=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.co m" <asga...@msn.co mwrote:
On 25 déc, 09:41, "asga...@msn.co m" <asga...@msn.co mwrote:
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.as kopenfilename(i nitialdir="/home/user",filetypes =
[("iso", "*.iso")])
lbl2=Label(fen1 )
cmd2="mount -o loop "+iso+" /mnt/disk"
child = pexpect.spawn(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
child.send(cmd2 )
lbl2.configure( text=cmd2)
lbl2.pack()
fen1=Tk()
entr1=Entry(fen 1)
lbl1=Label(fen1 )
entr1.pack()
lbl1.pack()
bou1= Button(fen1,tex t='Parcourir',c ommand=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.co m" <asga...@msn.co mwrote:
On 25 déc, 09:41, "asga...@msn.co m" <asga...@msn.co mwrote:
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.as kopenfilename(i nitialdir="/home/user",filetypes =
[("iso", "*.iso")])
lbl2=Label(fen1 )
cmd2="mount -o loop "+iso+" /mnt/disk"
child = pexpect.spawn(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
child.send(cmd2 )
lbl2.configure( text=cmd2)
lbl2.pack()
fen1=Tk()
entr1=Entry(fen 1)
lbl1=Label(fen1 )
entr1.pack()
lbl1.pack()
bou1= Button(fen1,tex t='Parcourir',c ommand=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.co m" <asga...@msn.co mwrote:
On 25 déc, 10:14, "asga...@msn.co m" <asga...@msn.co mwrote:
On 25 déc, 09:41, "asga...@msn.co m" <asga...@msn.co mwrote:
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.as kopenfilename(i nitialdir="/home/user",filetypes =
[("iso", "*.iso")])
lbl2=Label(fen1 )
cmd2="mount -o loop "+iso+" /mnt/disk"
child = pexpect.spawn(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
child.send(cmd2 )
lbl2.configure( text=cmd2)
lbl2.pack()
fen1=Tk()
entr1=Entry(fen 1)
lbl1=Label(fen1 )
entr1.pack()
lbl1.pack()
bou1= Button(fen1,tex t='Parcourir',c ommand=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(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
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(pr ompt). 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.co m" <asga...@msn.co mwrote:
On 25 déc, 10:14, "asga...@msn.co m" <asga...@msn.co mwrote:
On 25 déc, 09:41, "asga...@msn.co m" <asga...@msn.co mwrote:
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.as kopenfilename(i nitialdir="/home/user",filetypes =
[("iso", "*.iso")])
lbl2=Label(fen1 )
cmd2="mount -o loop "+iso+" /mnt/disk"
child = pexpect.spawn(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
child.send(cmd2 )
lbl2.configure( text=cmd2)
lbl2.pack()
fen1=Tk()
entr1=Entry(fen 1)
lbl1=Label(fen1 )
entr1.pack()
lbl1.pack()
bou1= Button(fen1,tex t='Parcourir',c ommand=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(c md1)
child.expect('M ot de passe :')
child.sendline( pwd)
child.expect(pr ompt)
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(pr ompt). 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
3293
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" inside, i don't know what i must expect! An example: if i want to run an "ls -l | more" on a remote host, it don't return me a prompt, but a "-------------More-------------"...thus i don't know how much
2
2362
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 scripts to work with various flavours of UNIX. However, OpenVMS is causing me problems. The tcl scripts work perfectly across UNIX and VMS. I'm converting them from tcl to python simply because python is more scalable and allows for better code...
0
3109
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 wanted to make use of the ANSI.py module in the pexpect package to handle screen-based telnet sessions in Python, but I could never break the ice with the thing. After reading an article by Greg Jorgenson where he recounts using the package to...
0
1936
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 command', consider the following example which simply runs the 'yes' command which prints an endless series of 'y' characters to the terminal. I want to timeout after 10 seconds -: child=pexpect.spawn('ssh me@remote_host') child.sendline('yes')...
4
2008
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 on specified coordinates? And also: is it possible to override, from python code, something on a
4
1879
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 replaced restored from tape. The uptime on this beast is like 12 years and counting. Just this week the last linux terminal was replaced with a new Dell running XP Pro. The client wants to keep the software but wants the space where the server fits...
4
4626
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 == 0: m = re.search('((#.+\r\n){20,25})(\s.*)', s.before) #<---------- MY PROBLEM
2
2628
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
3601
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 Linux 9 which is installed on my system. But when it comes to Linux terminal, I know not much about it. Q1. Which one is easy to learn-linux or unix terminal commands?
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.