Directing JC output into new Python variable? #165
-
Hi there, I'm trying to integrate JC into a Flask-server that'll provide a REST endpoint from where I can get a JSON return of IPTables. I'm still finding my feet in Linux/BASH, so I thought I'd ask you directly in the repo here. The trouble is this line: iptables = os.system("sudo iptables -L | jc -r --iptables") This variable prints the output of my IPtables to the terminal, which is great. However, to make it work with my plans for it, I need it to return the JSONified output as JSON, insted of returning the exit-code 0 it produces right now. Is there some method for extracting the command output into a separate variable or a flag to return something other than the exit code on execution that I'm missing? Or should I try to pipe the output into some further tool, if I just want to work with the output JSON and not the exit code? Your help is appreciated and my thanks for a great tool! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi there - thanks for your question! There are a few ways to tackle this, but I think this is probably the cleanest approach. Instead of using the
Because you are using the Hope that helps! |
Beta Was this translation helpful? Give feedback.
Hi there - thanks for your question!
There are a few ways to tackle this, but I think this is probably the cleanest approach.
Instead of using the
os.system
library, you can use thesubprocess
library to run a command and capture the output. You also do not need to pipe the output throughjc
within thesubprocess
call becausejc
is also a Python library and can be used directly in the program. Here's how you can grab the output and parse it:Because you are using the
jc
parse…