summaryrefslogtreecommitdiffstats
path: root/ip/routel
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 14:18:53 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 14:18:53 +0000
commita0e0018c9a7ef5ce7f6d2c3ae16aecbbd16a8f67 (patch)
tree8feaf1a1932871b139b3b30be4c09c66489918be /ip/routel
parentInitial commit. (diff)
downloadiproute2-a0e0018c9a7ef5ce7f6d2c3ae16aecbbd16a8f67.tar.xz
iproute2-a0e0018c9a7ef5ce7f6d2c3ae16aecbbd16a8f67.zip
Adding upstream version 6.1.0.upstream/6.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ip/routel')
-rwxr-xr-xip/routel62
1 files changed, 62 insertions, 0 deletions
diff --git a/ip/routel b/ip/routel
new file mode 100755
index 0000000..09a9012
--- /dev/null
+++ b/ip/routel
@@ -0,0 +1,62 @@
+#! /usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# This is simple script to process JSON output from ip route
+# command and format it. Based on earlier shell script version.
+"""Script to parse ip route output into more readable text."""
+
+import sys
+import json
+import getopt
+import subprocess
+
+
+def usage():
+ '''Print usage and exit'''
+ print("Usage: {} [tablenr [raw ip args...]]".format(sys.argv[0]))
+ sys.exit(64)
+
+
+def main():
+ '''Process the arguments'''
+ family = 'inet'
+ try:
+ opts, args = getopt.getopt(sys.argv[1:], "h46f:", ["help", "family="])
+ except getopt.GetoptError as err:
+ print(err)
+ usage()
+
+ for opt, arg in opts:
+ if opt in ["-h", "--help"]:
+ usage()
+ elif opt == '-6':
+ family = 'inet6'
+ elif opt == "-4":
+ family = 'inet'
+ elif opt in ["-f", "--family"]:
+ family = arg
+ else:
+ assert False, "unhandled option"
+
+ if not args:
+ args = ['0']
+
+ cmd = ['ip', '-f', family, '-j', 'route', 'list', 'table'] + args
+ process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+ tbl = json.load(process.stdout)
+ if family == 'inet':
+ fmt = '{:15} {:15} {:15} {:8} {:8}{:<16} {}'
+ else:
+ fmt = '{:32} {:32} {:32} {:8} {:8}{:<16} {}'
+
+ # ip route json keys
+ keys = ['dst', 'gateway', 'prefsrc', 'protocol', 'scope', 'dev', 'table']
+ print(fmt.format(*map(lambda x: x.capitalize(), keys)))
+
+ for record in tbl:
+ fields = [record[k] if k in record else '' for k in keys]
+ print(fmt.format(*fields))
+
+
+if __name__ == "__main__":
+ main()