I have a key value pair that I want to export from the bash script which the following python is embedded in. Excerpt from bash script:
output_test="ip:192.168.1.150,status:up"
export output_test
Embedded python within above bash script
python -c "$(cat << 'EOF'
import socket
import os
import json
bash_output=string(os.environ["output_test"])
sd = dict(u.split(":") for u in output_test.split(","))
print json.dumps(sd)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 15000))
s.send(bytes(json.dumps(s)))
result = json.loads(s.recv(1024))
print result
s.close()
EOF
)"
I'm quite new to Python I will admit I've hashed this together after intense speed research, and it's all I am going to need it to do in my scripts. Send name value pairs to a socket as a json array. Also, will the name I give the JSON object in the python, sd in the example above, become the name of the JSON array? - I will want to give it a meaningful name in the final version.
The error I'm getting is string not defined, which I think it means the string bash_output
dictionaryin standard python. You never assign a value tooutput_test, presumably you are missingoutput_test = os.environ['output_test']. No the name you assign the json will have no affect on the json ouput.