1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-10 22:41:51 +02:00
Files
.github
completions
docs
parsers
acpi.md
airport.md
airport_s.md
arp.md
asciitable.md
asciitable_m.md
blkid.md
cef.md
cef_s.md
chage.md
cksum.md
crontab.md
crontab_u.md
csv.md
csv_s.md
date.md
datetime_iso.md
df.md
dig.md
dir.md
dmidecode.md
dpkg_l.md
du.md
email_address.md
env.md
file.md
findmnt.md
finger.md
free.md
fstab.md
git_log.md
git_log_s.md
gpg.md
group.md
gshadow.md
hash.md
hashsum.md
hciconfig.md
history.md
hosts.md
id.md
ifconfig.md
ini.md
iostat.md
iostat_s.md
ip_address.md
iptables.md
iso_datetime.md
iw_scan.md
jar_manifest.md
jobs.md
jwt.md
kv.md
last.md
ls.md
ls_s.md
lsblk.md
lsmod.md
lsof.md
lspci.md
lsusb.md
m3u.md
mdadm.md
mount.md
mpstat.md
mpstat_s.md
netstat.md
nmcli.md
ntpq.md
passwd.md
pci_ids.md
pidstat.md
pidstat_s.md
ping.md
ping_s.md
pip_list.md
pip_show.md
plist.md
postconf.md
proc.md
proc_buddyinfo.md
proc_consoles.md
proc_cpuinfo.md
proc_crypto.md
proc_devices.md
proc_diskstats.md
proc_driver_rtc.md
proc_filesystems.md
proc_interrupts.md
proc_iomem.md
proc_ioports.md
proc_loadavg.md
proc_locks.md
proc_meminfo.md
proc_modules.md
proc_mtrr.md
proc_net_arp.md
proc_net_dev.md
proc_net_dev_mcast.md
proc_net_if_inet6.md
proc_net_igmp.md
proc_net_igmp6.md
proc_net_ipv6_route.md
proc_net_netlink.md
proc_net_netstat.md
proc_net_packet.md
proc_net_protocols.md
proc_net_route.md
proc_net_unix.md
proc_pagetypeinfo.md
proc_partitions.md
proc_pid_fdinfo.md
proc_pid_io.md
proc_pid_maps.md
proc_pid_mountinfo.md
proc_pid_numa_maps.md
proc_pid_smaps.md
proc_pid_stat.md
proc_pid_statm.md
proc_pid_status.md
proc_slabinfo.md
proc_softirqs.md
proc_stat.md
proc_swaps.md
proc_uptime.md
proc_version.md
proc_vmallocinfo.md
proc_vmstat.md
proc_zoneinfo.md
ps.md
route.md
rpm_qi.md
rsync.md
rsync_s.md
semver.md
sfdisk.md
shadow.md
ss.md
sshd_conf.md
stat.md
stat_s.md
sysctl.md
syslog.md
syslog_bsd.md
syslog_bsd_s.md
syslog_s.md
systemctl.md
systemctl_lj.md
systemctl_ls.md
systemctl_luf.md
systeminfo.md
time.md
timedatectl.md
timestamp.md
top.md
top_s.md
tracepath.md
traceroute.md
udevadm.md
ufw.md
ufw_appinfo.md
uname.md
universal.md
update_alt_gs.md
update_alt_q.md
upower.md
uptime.md
url.md
vmstat.md
vmstat_s.md
w.md
wc.md
who.md
x509_cert.md
xml.md
xrandr.md
yaml.md
zipinfo.md
lib.md
readme.md
streaming.md
utils.md
jc
man
templates
tests
.gitignore
CHANGELOG
CONTRIBUTING.md
EXAMPLES.md
LICENSE.md
MANIFEST.in
README.md
_config.yml
build-completions.py
build-package.sh
docgen.sh
install.sh
mangen.py
pypi-upload.sh
readmegen.py
requirements.txt
runtests.sh
setup.cfg
setup.py
updatedocs.sh
jc/docs/parsers/iptables.md
Kelly Brazil 3daf90c5fe doc formatting
2022-08-15 13:51:48 -07:00

4.4 KiB

Home

jc.parsers.iptables

jc - JSON Convert iptables command output parser

Supports -vLn and --line-numbers for all tables.

Usage (cli):

$ sudo iptables -L -t nat | jc --iptables

or

$ jc iptables -L -t nat

Usage (module):

import jc
result = jc.parse('iptables', iptables_command_output)

Schema:

[
  {
    "chain":                string,
    "rules": [
      {
        "num"               integer,
        "pkts":             integer,
        "bytes":            integer,  # converted based on suffix
        "target":           string,
        "prot":             string,
        "opt":              string,   # "--" = Null
        "in":               string,
        "out":              string,
        "source":           string,
        "destination":      string,
        "options":          string
      }
    ]
  }
]

Examples:

$ sudo iptables --line-numbers -v -L -t nat | jc --iptables -p
[
  {
    "chain": "PREROUTING",
    "rules": [
      {
        "num": 1,
        "pkts": 2183,
        "bytes": 186000,
        "target": "PREROUTING_direct",
        "prot": "all",
        "opt": null,
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere"
      },
      {
        "num": 2,
        "pkts": 2183,
        "bytes": 186000,
        "target": "PREROUTING_ZONES_SOURCE",
        "prot": "all",
        "opt": null,
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere"
      },
      {
        "num": 3,
        "pkts": 2183,
        "bytes": 186000,
        "target": "PREROUTING_ZONES",
        "prot": "all",
        "opt": null,
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere"
      },
      {
        "num": 4,
        "pkts": 0,
        "bytes": 0,
        "target": "DOCKER",
        "prot": "all",
        "opt": null,
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere",
        "options": "ADDRTYPE match dst-type LOCAL"
      }
    ]
  },
  ...
]

$ sudo iptables --line-numbers -v -L -t nat | jc --iptables -p -r
[
  {
    "chain": "PREROUTING",
    "rules": [
      {
        "num": "1",
        "pkts": "2183",
        "bytes": "186K",
        "target": "PREROUTING_direct",
        "prot": "all",
        "opt": "--",
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere"
      },
      {
        "num": "2",
        "pkts": "2183",
        "bytes": "186K",
        "target": "PREROUTING_ZONES_SOURCE",
        "prot": "all",
        "opt": "--",
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere"
      },
      {
        "num": "3",
        "pkts": "2183",
        "bytes": "186K",
        "target": "PREROUTING_ZONES",
        "prot": "all",
        "opt": "--",
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere"
      },
      {
        "num": "4",
        "pkts": "0",
        "bytes": "0",
        "target": "DOCKER",
        "prot": "all",
        "opt": "--",
        "in": "any",
        "out": "any",
        "source": "anywhere",
        "destination": "anywhere",
        "options": "ADDRTYPE match dst-type LOCAL"
      }
    ]
  },
  ...
]

parse

def parse(data, raw=False, quiet=False)

Main text parsing function

Parameters:

data:        (string)  text data to parse
raw:         (boolean) unprocessed output if True
quiet:       (boolean) suppress warning messages if True

Returns:

List of Dictionaries. Raw or processed structured data.

Parser Information

Compatibility: linux

Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)