I’ve been reading a article on the Python Subprocess Module, specifically on the subprocess.Popen
, which explains how Python allows communication with the executed process.
I made a small example for Windows (sorry, I don’t have a Linux to test this now) that changes the system date:
import subprocess
processo = subprocess.Popen(args = ['date'],
stdin = subprocess.PIPE,
stderr = subprocess.PIPE,
shell = True)
processo.communicate(b'01-01-01')
The above code was tested in the standard Python implementation (Cpython) version 3.3. Note that the line processo.communicate(b'01-01-01')
sends the value 01-01-01
for the command date
.
The console output is:
The current date is: Tue 01/28/2014
Enter the new date: (mm-dd-yy) 01-01-01
I believe you can adapt the command Popen
to execute the dump and then send the password through the method communicate
.
I could go into more detail, what would be the command?
– Felipe Avelar
I’m trying to dump into a database. The alternatives I found are using environment variable, but I want to get away from it. After typing the dump command the terminal is: "Passoword:" waiting for the user to enter the password.
– Thiago Silva
Which database? Please place the line as you are using. It would be helpful to ask for the Python password and enter it in the command line by parameters?
– Alexandre Marcondes
If the command allows, it is possible to concatenate the string and play with a parameter
-p
or--pasword
in theos.system()
, for example:os.system("comando -p "+password)
, but I don’t know if that’s what you want.– Felipe Avelar
@Alexandremarcondes Postgres. Command: pg_dump -U user_name -h host -C -f file_name.dump databasename. I want the script to be automated, without interaction with the user.
– Thiago Silva
@Felipe.Velar, the command does not have the password parameter.
– Thiago Silva
For what I gave a read, only changing the same system variable. Because, in fact, the command is not running "inside" Python, Python makes a process request to the OS and whoever executes this command is the OS itself and not Python. Did you understand what I meant or did you get a little confused?
– Felipe Avelar
@Felipe.Wake up, I understood yes. I had already seen this but wanted to avoid. Creating a system variable a simple echo would display the password. Via script would have some alternatives to avoid this problem.
– Thiago Silva