Roberto Fernandez Diaz
Roberto Fernandez Diaz

Reputation: 804

How can I programmatically write expected input on terminal command?

I am trying to execute this command programmatically with python 2.7:

aws configure --profile name_profile

The thing is that this command as soon as you executed it, it ask the user to input some parameters:

$ aws configure
AWS Access Key ID [****]: access_key_input
AWS Secret Access Key [****]: secret__key_input
Default region name [us-west-1]: region_optional_input

I know how to execute commands in python, usually with these lines you are done:

def execute_command(command):
    try:
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        return output, error
    except Exception as error:
        return None, error

But in this case, I don't know how to simulate this process. I want to write something like

def add_profile(access_key, access_secret, name_profile, region=None)

And that should execute the command of before and answer the questions with the parameters.

Thanks in advance to all.

Upvotes: 0

Views: 703

Answers (2)

Roberto Fernandez Diaz
Roberto Fernandez Diaz

Reputation: 804

Even though the use of Pexpect (thanks @natn2323) solve the issue, I found out that if you don't need to check for dynamic questions and the amount and type of questions are always the same. This will work also:

try:
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    output, error = process.communicate(input=b"answer1\nanswer2\n\n")
    return output, error
except Exception as error:
    return None, error

Notice that the last 2 answers (the last part of the input "\n\n") are empty for my case (aws configure).

I have some errors with Pycharm and the use of Pexpect so at the end I will use this solution.

Upvotes: 1

natn2323
natn2323

Reputation: 2061

This question has more to do with the AWS CLI than with Python. The easy, non-interactive way to configure you AWS credentials is to use the AWS shared credentials file.

Instead of having to interact with the command line, you could have your function create such a shared credentials file, making sure to also clean up or delete the file after your done with it.

If you're adamant on using Python to interact with the AWS CLI, there a couple of approaches. From a quick Google search, I've found this SO post. From my own experiences of interacting with the command line programatically with Python, I've used Pexpect. This library basically lets you expect that certain text will appear in the CLI, after which your program can output new data to the CLI.

Upvotes: 1

Related Questions