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

14

I have a Python script that runs a particular command on the system. This command waits for a password to be entered immediately, the only way to do this is to pass the password via the argument.

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

    
asked by anonymous 28.01.2014 / 18:00

4 answers

13

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

maquina:porta:bancodedados:usuario:senha

In this way the standard tools will not prompt you for the password for the settings defined in the file.

    
28.01.2014 / 18:48
6

I've been reading a article about the Python Subprocess Module, specifically about subprocess.Popen , which explains how the Python allows you to communicate with the executed process.

I made a small example for Windows (sorry, I do not 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 code above was tested in the default implementation of Python (CPython) version 3.3. Note that the processo.communicate(b'01-01-01') line sends the 01-01-01 value to the date command.

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 Popen command to run dump and then send the password using the communicate method.

    
28.01.2014 / 18:31
2

On * nix systems you can send data as input to another command using pipes | . The idea is that when you type cmd1 | cmd2 the output of command 1 will be the input of command 2, that is, they stay connected. At the end all you will see will be the entry of command 1 and the output of command 2. You can execute the following:

echo "minhasenha" | seu_comando

The best alternative, however, is to use Python's subprocess to have access to the incoming and outgoing streams of the process created, giving you more control over what happens. See the utluiz's answer for more details.

    
28.01.2014 / 18:37
1

Use pExpect , which provides, among other things, something that looks like expect of 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 >

    
28.01.2014 / 21:05