How do I programmatically respond to a command on the Linux terminal?

Asked

Viewed 1,418 times

15

I have a Python script that runs a certain command on the system. This command expects a password to be typed right away, the only way it works is this, it is not possible to pass the password via argument.

I wanted to know if there is any way to execute the command and password without needing user interaction.

  • 1

    I could go into more detail, what would be the command?

  • 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.

  • 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?

  • If the command allows, it is possible to concatenate the string and play with a parameter -p or --pasword in the os.system(), for example: os.system("comando -p "+password), but I don’t know if that’s what you want.

  • @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.

  • @Felipe.Velar, the command does not have the password parameter.

  • 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.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.

Show 3 more comments

4 answers

14


You don’t need all this, all you have to do is create a . pgpass file in the user’s home directory that will run the command. The file must have read/write permission only for this user for security reasons.
The file syntax is as follows:

maquina:porta:bancodedados:usuario:senha

This way the default tools will not ask for the password for the settings set in the file.

6

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.

  • great! It will suit me for other tasks for sure.

2

On *Nix systems you can input data to another command using Pipes |. The idea is that when you write cmd1 | cmd2 The output of Command 1 will be the input of Command 2, that is, they are connected. At the end all you see will be the input of command 1 and the output of command 2. You can run the following:

echo "minhasenha" | seu_comando

The best alternative, however, is to use the subprocess Python to gain access to input and output streams from the created process, giving you more control over what happens. See utluiz response for more details.

1

Use the pExpect, that provides among other features, something that looks like the expect linux.

See an example of how to interact with a session TELNET

import pexpect
import sys,time
ipaddr = "192.168.0.81"
username = "usuario"
password = "minhasenha"
telconn = pexpect.spawn("telnet " + ipaddr)
telconn.expect(":")
telconn.logfile=sys.stdout
time.sleep(15)
telconn.sendline(username + "\r")
telconn.expect(":")
telconn.sendline(password + "\r")
time.sleep(30)
telconn.expect(">")
print "Logado com Sucesso"

In the above example, the variable is sent only when the "expected" statement is reached, in the telnet case, it was expected by : sometimes, and printed Logado com Sucesso after waiting for >

Browser other questions tagged

You are not signed in. Login or sign up in order to post.