0

I want to use python to send a command to a terminal and I am doing it with:

subprocess.call('ask util generate-lwa-tokens')

Normally in the terminal it will ask me to input my client ID next. How can I send the client ID to the subprocess?

This is a picture of how it is with the terminal picture of what normally works

Community
  • 1
  • 1
bhalgalix
  • 29
  • 1
  • 9

1 Answers1

1

Try the subprocess manual. You have options with subprocess to work with stdin, stdout, and stderr of the process you're calling.

from subprocess import Popen, PIPE, STDOUT

p = Popen(['ask', 'util', 'generate-lwa-tokens'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, universal_newlines=True)

# Storing the values that should be passed

values = ["client_id", "client_secret"]

# Interacting with the shell 
output, err = p.communicate(input=f'{values[0]}\n{values[1]}\n')
# Displaying outputs
print(output)

Try this and comment if there are any errors.

Community
  • 1
  • 1
codeslord
  • 2,172
  • 14
  • 20
  • what this do is generate a token after i give the client id and i need to read this token to use it, but now i am not getting any response when i execute the file – bhalgalix Feb 12 '19 at 16:54
  • i added an image to help explain what i want to do, hope it helps – bhalgalix Feb 12 '19 at 18:55
  • like that it answer the client ID but when the terminal ask for client secret it stops – bhalgalix Feb 12 '19 at 20:45
  • print(err) and see what is the issue, communicate() reads data from stdout and stderr until end-of-file is reached. - It waits until your program quits. – codeslord Feb 12 '19 at 21:27
  • 1
    at the end i will use pexpect but thankyou for the help – bhalgalix Feb 12 '19 at 22:59
  • Kudos, you may edit the answer to add your solution. Also please be aware that pexpect is not continuously maintained and not completely platform independent. – codeslord Feb 13 '19 at 06:40
  • Thank you for the advice, i will take that into consideration. – bhalgalix Feb 13 '19 at 20:35