[HELP!] a doubt about entering password in python

D

douxin

Hi all:

i have some doubts in doing python programming

i wanted to execute a command "su -c 'fdisk -l'",and it needed a
password
so i wanted to write a python script to get this done.

i knew 'pexpect' would work fine,but i had to set a certain timeout
to take care of the real delay time which i probably didn't know

i tried to use 'subprocess' to do this,however,it did not work
well,and came the problem

i use Popen to execute "su -c 'fdisk -l'" in sub process,and
assigned subprocess.PIPE to stdin,stdout
i tried to enter password by doing "stdin.write("password"+"\n")"
and i expected i could get the output of "fdisk -l" by doing
"stdout.read()"
it didn't work.

will somebody tell me what is going on with that?
i'll appreciate i can learn from you

Dou
 
S

Steven D'Aprano

i use Popen to execute "su -c 'fdisk -l'" in sub process,and
assigned subprocess.PIPE to stdin,stdout i tried to enter password
by doing "stdin.write("password"+"\n")" and i expected i could get
the output of "fdisk -l" by doing "stdout.read()"
it didn't work.

will somebody tell me what is going on with that?

Would you like us to guess what happened? I love guessing games!

My guess is that it output "su: incorrect password", which means you have
the wrong password. Is that it?

If not, my guess is that it output "fdisk: command not found", in which
case your system is broken and the fdisk binary is missing or not on the
PATH. Am I close?

Last guess: you got a Python traceback with an error:

NameError: name 'subprocess' is not defined

You need to import the subprocess first.


If none of my guesses are correct, could we have some hints? Perhaps show
us the actual code you are using, and the actual results, copied and
pasted exactly.

Thank you.
 
M

MRAB

Would you like us to guess what happened? I love guessing games!

My guess is that it output "su: incorrect password", which means you have
the wrong password. Is that it?

If not, my guess is that it output "fdisk: command not found", in which
case your system is broken and the fdisk binary is missing or not on the
PATH. Am I close?

Last guess: you got a Python traceback with an error:

NameError: name 'subprocess' is not defined

You need to import the subprocess first.


If none of my guesses are correct, could we have some hints? Perhaps show
us the actual code you are using, and the actual results, copied and
pasted exactly.
It may, of course, be that for security reasons it won't accept a
password from
whatever happens to be connected to stdin, but instead insists that it's
entered
directly from the keyboard, if you see what I mean.
 
R

Ramchandra Apte

It may, of course, be that for security reasons it won't accept a

password from

whatever happens to be connected to stdin, but instead insists that it's

entered

directly from the keyboard, if you see what I mean.

I think you are correct - su uses some tty magic to stop ECHO and probably doesn't read the password from stdin.
 
R

Ramchandra Apte

It may, of course, be that for security reasons it won't accept a

password from

whatever happens to be connected to stdin, but instead insists that it's

entered

directly from the keyboard, if you see what I mean.

I think you are correct - su uses some tty magic to stop ECHO and probably doesn't read the password from stdin.
 
C

Chris Angelico

I think you are correct - su uses some tty magic to stop ECHO and probably doesn't read the password from stdin.

I believe that's right, but the 'sudo' command - at least on my Debian
and Ubuntu systems - accepts a -S parameter to read from stdin instead
of the terminal. Actually, sudo might be better suited to the OP's
task anyway, if it's available.

ChrisA
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top