473,395 Members | 1,791 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,395 software developers and data experts.

Why my program (using pexpect to switch user) doesn't work well?

I'm new to python
I want to write a simple script to switch user,for example,from user_A
to user_B.
This my codes:

#!/usr/bin/python
import pexpect
import os
passwd="user_B"
child = pexpect.spawn('su user_B')
child.expect('Password:')
child.sendline(passwd)
child.expect('$')
child.close()

Maybe it's the easiest pexpect program.Sometimes ,it work well,it
switch to user_B successfully .But after i type the command exit to
switch back to user_A,execute the python script again,it can't work,do
nothing or just waiting.Why it have different results?
Sorry for my poor English,and many thanks to all.

Blackjade
Jan 10 '08 #1
2 3405
On Jan 10, 12:59 am, BlackjadeLin <hoo.s...@gmail.comwrote:
I'm new to python
I want to write a simple script to switch user,for example,from user_A
to user_B.
This my codes:

#!/usr/bin/python
importpexpect
import os
passwd="user_B"
child =pexpect.spawn('su user_B')
child.expect('Password:')
child.sendline(passwd)
child.expect('$')
child.close()

Maybe it's the easiest pexpect program.Sometimes ,it work well,it
switch to user_B successfully .But after i type the command exit to
switch back to user_A,execute the python script again,it can't work,do
nothing or just waiting.Why it have different results?
Sorry for my poor English,and many thanks to all.

Blackjade
When you call child.close() that will kill the child process.
Possibly you want to use the interact() method.
It is not clear from your message if you want to interact with
the child process as if it were your new shell. If you do,
then take a look at the interact() method.

#!/usr/bin/python
importpexpect
import os
passwd="user_B"
child =pexpect.spawn('su user_B')
child.expect('Password:')
child.sendline(passwd)
child.expect('$')
child.interact()

--
Noah
Jan 10 '08 #2
On Jan 11, 1:49 am, Noah <n...@noah.orgwrote:
On Jan 10, 12:59 am, BlackjadeLin <hoo.s...@gmail.comwrote:
I'm new to python
I want to write a simple script to switch user,for example,from user_A
to user_B.
This my codes:
#!/usr/bin/python
importpexpect
import os
passwd="user_B"
child =pexpect.spawn('su user_B')
child.expect('Password:')
child.sendline(passwd)
child.expect('$')
child.close()
Maybe it's the easiest pexpect program.Sometimes ,it work well,it
switch to user_B successfully .But after i type the command exit to
switch back to user_A,execute the python script again,it can't work,do
nothing or just waiting.Why it have different results?
Sorry for my poor English,and many thanks to all.
Blackjade

When you call child.close() that will kill the child process.
Possibly you want to use the interact() method.
It is not clear from your message if you want to interact with
the child process as if it were your new shell. If you do,
then take a look at the interact() method.

#!/usr/bin/python
importpexpect
import os
passwd="user_B"
child =pexpect.spawn('su user_B')
child.expect('Password:')
child.sendline(passwd)
child.expect('$')
child.interact()

--
Noah
Thank you very much!
The interact() method complete my achievement.
Jan 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Axel Koehler | last post by:
Hallo, I want to start an extern program and simple stop it, after a given time, if not succesfull finished. OS is Linux Axel
6
by: tigrfire | last post by:
I've been working on a program to try and play a game of Craps, based on a version I found elsewhere - I didn't code the original, but I added a few things such as a balance and wager system. I'm...
3
by: 31337one | last post by:
Hello everyone, I am using a python script as a way to test another program that I have written in C++. The program cannot be altered. It needs to stay the way it is. I need to simulate a few...
1
by: tereglow | last post by:
Hello, I am trying to convert some Expect/Tcl code into Python by using the Pexpect module. The environment is such that authenticated keys are disabled although SSH is available. I do not...
2
by: pradeep nair | last post by:
I would like to know how to pass keyboard input for a python script which is ran by another script. for eg: hello1.py: import os if __name__=='__main__':
0
by: geoharish | last post by:
Dear All, I started using Python just a few days back and there was a requirement of an assigned project to change the user Password through script without user interaction. I could write the...
8
by: asgarde | last post by:
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 *...
11
by: Emmanouil Angelakis | last post by:
Hi, I am tryiong to do something obviously trivial such as: I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to...
2
by: yellowblueyellow | last post by:
Hey , I need to SSH into a server .. (10.8.42.38) using pexpect the username is 'admin' and password is 'abc123' so far i have the following code import pexpect import sys import time...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.