Server IP : 85.214.239.14 / Your IP : 3.133.149.244 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /bin/ |
Upload File : |
#! /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()