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

控制台命令结果转json

满纸空言3年前 (2021-11-18)尘凡21070

jq:

So, here's what I did.
I used the command jq to parse the result of ip route list table all using a regex, transforming the output to JSON.
Feel free to play around with jq, and click here to use an online sandbox already loaded with this solution.

ip route list table all | jq --raw-input --slurp 'split("\n") | map(capture("^(?:(?<broadcast>broadcast) ?)?(?:(?<local>local) ?)?(?:(?<multicast>multicast) ?)?(?: ?(?<network>.*?) )(?:from (?<from>\\S+) ?)?(?: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"))'

ip route list type unicast table all|grep -v local|grep -v table|jq --raw-input --slurp 'split("\n") | map(capture("^(?: ?(?<network>.*?) )(?:from (?<from>\\S+) ?)?(?:via (?<via>\\S+) ?)?(?:dev (?<dev>\\S+) ?)?(?:src (?<src>\\S+) ?)?"; "g"))'

ip route list type unicast table all|grep -v local|grep -v table|jq -R -s -c 'split("\n") | map(capture("^(?: ?(?<network>.*?) )(?:from (?<from>\\S+) ?)?(?:via (?<via>\\S+) ?)?(?:dev (?<dev>\\S+) ?)?(?:src (?<src>\\S+) ?)?"; "g"))'

 

jc:

https://github.com/kellyjonbrazil/jc

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

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

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

分享给朋友:

“控制台命令结果转json” 的相关文章

nmcli参数的各种缩写3年前 (2021-04-21)
红旗11系统下载3年前 (2021-07-09)
Git 保存用户名和密码3年前 (2021-08-16)
Golong发送HTTP、HTTPS请求3年前 (2021-08-19)

发表评论

访客

看不清,换一张

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