Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use subprocess to send a password

I'm attempting to use the python subprocess module to log in to a secure ftp site and then grab a file. However I keep getting hung up on just trying to send the password when it is requested. I so far have the following code:

from subprocess import Popen, PIPE  proc = Popen(['sftp','user@server', 'stop'], stdin=PIPE) proc.communicate('password') 

This still stops at the password prompt. If I enter the password manually it then goes to the ftp site and then enters the password on the command line. I've seen people suggest using pexpect but long story short I need a standard library solution. Is there anyway with subprocess and/or any other stdlib? What am I forgetting above?

like image 296
The Jug Avatar asked Mar 05 '10 15:03

The Jug


People also ask

How do you pass a password in Python?

Prompting For Password You need to ask user for password. You could use input() , but that would show the password in terminal, to avoid that you should use getpass instead: import getpass user = getpass. getuser() password = getpass.

What is subprocess popen ()?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What is difference between subprocess Popen and call?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

What does subprocess Check_output do?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.


2 Answers

Try

proc.stdin.write('yourPassword\n') proc.stdin.flush() 

That should work.

What you describe sounds like stdin=None where the child process inherits the stdin of the parent (your Python program).

like image 56
Aaron Digulla Avatar answered Sep 28 '22 06:09

Aaron Digulla


Perhaps you should use an expect-like library instead?

For instance Pexpect (example). There are other, similar python libraries as well.

like image 27
codeape Avatar answered Sep 28 '22 07:09

codeape