-
-
Save intijk/61a72d4445152048abb2802a87ec06bc to your computer and use it in GitHub Desktop.
Pretty print iptables output. Align columns and strip out comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import re | |
import sys | |
from tabulate import tabulate | |
comments_re = re.compile(r'/\*.*\/') | |
in_chain, eof = False, False | |
headers, table = [], [] | |
while True: | |
line = sys.stdin.readline() | |
if len(line) == 0: | |
eof = True | |
line = line.strip() | |
if line == '': | |
if in_chain: | |
headers.append('extra') | |
print(tabulate(table, headers=headers)) | |
headers, table = [], [] | |
in_chain = False | |
if eof: | |
break | |
continue | |
if line.startswith('Chain'): | |
print('\n{}'.format(line)) | |
continue | |
if line.startswith('pkts') or line.startswith('target') or line.startswith('num'): | |
headers = line.split() | |
in_chain = True | |
continue | |
if in_chain: | |
parts = line.split() | |
begin = parts[:len(headers)] | |
extra = ' '.join(parts[len(headers):]) | |
# comments are too wide and usually redundant - strip out | |
extra = comments_re.sub('', extra) | |
table.append(begin + [extra]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo iptables -t nat -nL KUBE-NODEPORTS | |
Chain KUBE-NODEPORTS (1 references) | |
target prot opt source destination | |
KUBE-MARK-MASQ tcp -- 0.0.0.0/0 0.0.0.0/0 /* kube-system/traefik:http */ tcp dpt:31922 | |
KUBE-SVC-X3WUOHPTYIG7AA3Q tcp -- 0.0.0.0/0 0.0.0.0/0 /* kube-system/traefik:http */ tcp dpt:31922 | |
KUBE-MARK-MASQ tcp -- 0.0.0.0/0 0.0.0.0/0 /* kube-system/traefik:https */ tcp dpt:31783 | |
KUBE-SVC-IKNZCF5XJQBTG3KZ tcp -- 0.0.0.0/0 0.0.0.0/0 /* kube-system/traefik:https */ tcp dpt:31783 | |
$ sudo iptables -t nat -nL KUBE-NODEPORTS | ./pp-iptables.py | |
Chain KUBE-NODEPORTS (1 references) | |
target prot opt source destination extra | |
------------------------- ------ ----- --------- ------------- ------------- | |
KUBE-MARK-MASQ tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:31922 | |
KUBE-SVC-X3WUOHPTYIG7AA3Q tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:31922 | |
KUBE-MARK-MASQ tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:31783 | |
KUBE-SVC-IKNZCF5XJQBTG3KZ tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:31783 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment