0

I would like to know how can i use my variables in output of another command. For example if i try to generate some keys with "openssl" i'll get the question about the country, state, organizations etc.

I would like to use my variables in the script that i have to fill this information. I'll have variable "Country"; variable "State" etc. and to be parsed/set in to this questions from the openssl command when is executed.

I'm trying this in bash but also would like to know how will be the same think done in python.

Kind regards

IvanAK
  • 203
  • 2
  • 11

1 Answers1

1

You have multiple ways to do so. 1. If you have your script launched before the python script and the result set in an enviroment variable you can read the environment variable from your python script as follows:

import os 
os.environ.get('MYVARIABLE', 'Default val')

Otherwise you can try to launch the other application from your python script and read the result by using os.popen():

import os
tmp = os.popen("ls").read()

or better (if you have a python newer than 2.6)

import subprocess
proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
tmp = proc.stdout.read()
Stefano
  • 3,981
  • 8
  • 36
  • 66
  • and what about Bash ? Also the other command is openssl and what i want to achieve is to fill those questions that openssl is asking me using the variables in the script that is lunching openssl – IvanAK Apr 11 '16 at 15:19
  • ok... so what you want to open an interactive shell. have a look at this: https://pymotw.com/2/subprocess/ – Stefano Apr 13 '16 at 07:18
  • and this: http://stackoverflow.com/questions/9673730/interacting-with-bash-from-python – Stefano Apr 13 '16 at 07:18
  • I have found some solution for the openssl. There is a man openssl that can fill up the required questions. But the main questions is how to interact with some shell that show questions that need to be filled with some input from the user. And i want to autmatcly fill up that questions with some variables that i will have in the script. The same for example with **ssh-keygen** command, when you are prompted with questions, the user need to fill this questions. path where to store the keys, passwords names etc. – IvanAK Apr 13 '16 at 08:21