ip route命令获取json结果
方法一(CentOS7需要自编译iproute5版本):
[root@device shells]# ip --json route list type unicast table all
[{"dst":"default","gateway":"192.168.0.1","dev":"team0","table":"10","flags":[]},{"dst":"192.168.0.0/24","dev":"team0","table":"10","scope":"link","flags":[]},{"dst":"default","gateway":"192.168.0.1","dev":"team0","protocol":"static","metric":350,"flags":[]},{"dst":"192.168.0.0/24","dev":"team0","protocol":"kernel","scope":"link","prefsrc":"192.168.0.188","metric":350,"flags":[]},{"dst":"fe80::/64","dev":"team0","protocol":"kernel","metric":350,"flags":[],"pref":"medium"},{"dst":"ff00::/8","dev":"eth2","table":"local","metric":256,"flags":[],"pref":"medium"},{"dst":"ff00::/8","dev":"team0","table":"local","metric":256,"flags":[],"pref":"medium"},{"dst":"ff00::/8","dev":"eth0","table":"local","metric":256,"flags":[],"pref":"medium"},{"dst":"ff00::/8","dev":"eth1","table":"local","metric":256,"flags":[],"pref":"medium"}]
方法二(华为欧拉jq命令报错,CentOS8和阿里龙蜥正常):
[root@device shells]#ip route list type unicast table all|grep -v local|grep -v table|jq -R -s -c -M 'split("\n") | map(capture("^(?: ?(?<network>.*?) )(?:from (?<from>\\S+)|0.0.0.0 ?)?(?:via (?<via>\\S+) ?)?(?:dev (?<dev>\\S+) ?)?(?:table (?<table>\\S+) ?)?(?:proto (?<proto>\\S+) ?)?(?:scope (?<scope>\\S+) ?)?(?:src (?<src>\\S+) ?)?(?:metric (?<metric>\\d+) ?)?(?<linkdown>linkdown)?(?<unresolved>unresolved)?"; "g"))'|sed s/null/\"\"/g
[{"network":"default","from":"","via":"192.168.0.1","dev":"team0","table":"","proto":"static","scope":"","src":"","metric":"350","linkdown":"","unresolved":""},{"network":"192.168.0.0/24","from":"","via":"","dev":"team0","table":"","proto":"kernel","scope":"link","src":"192.168.0.188","metric":"350","linkdown":"","unresolved":""},{"network":"fe80::/64","from":"","via":"","dev":"team0","table":"","proto":"kernel","scope":"","src":"","metric":"350","linkdown":"","unresolved":""}]