当前位置:首页 > 尘凡 > 正文内容

ip route命令获取json结果

满纸空言3年前 (2022-03-28)尘凡20220

方法一(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":""}]

扫描二维码推送至手机访问。

版权声明:本文由满纸空言发布,如需转载请注明出处。

本文链接:https://mzky.cc/post/89.html

分享给朋友:

“ip route命令获取json结果” 的相关文章

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。