Skip to content

Directing JC output into new Python variable? #165

Discussion options

You must be logged in to vote

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 the subprocess library to run a command and capture the output. You also do not need to pipe the output through jc within the subprocess call because jc is also a Python library and can be used directly in the program. Here's how you can grab the output and parse it:

import subprocess
from jc.parsers.iptables import parse as parse_iptables

cmd_output = subprocess.check_output(['sudo', 'iptables', '-L'], text=True)
iptables_data = parse_iptables(cmd_output, quiet=True, raw=True)

Because you are using the jc parse…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kellyjonbrazil
Comment options

Answer selected by kellyjonbrazil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants