简体   繁体   中英

Run shell script with additional parameters from python and get output as python variable (API authentication)

Could someone help me with the following:

I have this line that I normally run in terminal:

 path/to/mysh/script.sh PROD generate-ticket username

and it gives me the following output in terminal:

{"user":{"Userid":123,"firstName":"Joe","SecondName":"Doe","loginName":"jdoe"},"accessToken":"1235df6543cggf432sa24"}

I need the 'accessToken' value assigned to python variable.

My code:

import subprocess
import json 

username ='jdoe'

def get_Access_Token(username):
    s = json.loads(subprocess.check_output(['path/to/mysh/script.sh', 'PROD', 'generate-ticket', username]))


print (s['accessToken'])

but it gives me the following error when trying to print my variable:

NameError: name 's' is not defined

Thanks in advance!

Return the value from the function.

Ex:

import subprocess
import json 

username ='jdoe'

def get_Access_Token(username):
    return json.loads(subprocess.check_output(['path/to/mysh/script.sh', 'PROD', 'generate-ticket', username]))['accessToken']

print (get_Access_Token(username))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM