如何檢索 JSON 格式的路由表 (linux)?

如何檢索 JSON 格式的路由表 (linux)?

ip命令能夠以 JSON 格式傳回一些結果,但似乎無法格式化路由表。
以下是一些例子來說明我的意思。

所有地址清單:ip --json address show

[{ ... },{
    "ifindex": 2,
    "ifname": "eth0",
    "flags": ["BROADCAST","MULTICAST","UP","LOWER_UP"],
    "mtu": 1500,
    "qdisc": "fq_codel",
    "master": "lan0",
    "operstate": "UP",
    "group": "default",
    "txqlen": 1000,
    "link_type": "ether",
    "address": "44:89:3f:e9:a8:08",
    "broadcast": "ff:ff:ff:ff:ff:ff",
    "addr_info": []
},{ ... }]



介面列表:ip --json link show

[{ ... },{
    "ifindex": 2,
    "ifname": "eth0",
    "flags": ["BROADCAST","MULTICAST","UP","LOWER_UP"],
    "mtu": 1500,
    "qdisc": "fq_codel",
    "master": "lan0",
    "operstate": "UP",
    "linkmode": "DEFAULT",
    "group": "default",
    "txqlen": 1000,
    "link_type": "ether",
    "address": "44:89:3f:e9:a8:08",
    "broadcast": "ff:ff:ff:ff:ff:ff"
},{ ... }]



我本來希望也能檢索 JSON 中的路由列表,但我只能檢索其明文版本:
路由列表:(ip --json route list它仍然是明文形式)

1.1.1.2 via 192.168.255.11 dev lan0 table hopper src 192.168.254.1 metric 10
default via 10.19.1.4 dev wg0 metric 5
default via 192.168.255.11 dev lan0 metric 6
10.19.1.0/24 dev wg0 scope link
124.214.110.113 via 192.168.255.11 dev lan0 metric 4
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
172.18.0.0/16 dev br-fea43fdf31f1 proto kernel scope link src 172.18.0.1
172.19.0.0/16 dev br-6f9e681d15b0 proto kernel scope link src 172.19.0.1 linkdown
192.168.0.0/16 dev lan0 proto kernel scope link src 192.168.254.1
192.168.1.0/24 dev wan0 proto kernel scope link src 192.168.1.9
192.168.1.0/24 via 192.168.255.11 dev lan0 metric 3
broadcast 127.0.0.0 dev lo table local proto kernel scope link src 127.0.0.1
local 127.0.0.1 dev lo table local proto kernel scope host src 127.0.0.1
multicast 239.255.255.250/32 from 192.168.1.109/32 table default proto 17 unresolved

由於我在任何地方都找不到解決方案,因此我將回答我自己的問題並留下我所做的事情。
誰知道呢,也許有一天我會再次需要它,或者也許其他人也會遇到同樣的問題! :-)

答案1

所以,這就是我所做的。
我使用該命令jq解析使用正規表示式的結果ip route list table all,將輸出轉換為 JSON。
隨意玩弄jq, 和按一下此處使用線上沙箱已經載入了這個解決方案。
這是完整的命令:

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"))'

這是輸出:

[
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "1.1.1.2",
    "from": null,
    "via": "192.168.255.11",
    "dev": "lan0",
    "table": "hopper",
    "proto": null,
    "scope": null,
    "src": "192.168.254.1",
    "metric": "10",
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "default",
    "from": null,
    "via": "10.19.1.4",
    "dev": "wg0",
    "table": null,
    "proto": null,
    "scope": null,
    "src": null,
    "metric": "5",
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "default",
    "from": null,
    "via": "192.168.255.11",
    "dev": "lan0",
    "table": null,
    "proto": null,
    "scope": null,
    "src": null,
    "metric": "6",
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "10.19.1.0/24",
    "from": null,
    "via": null,
    "dev": "wg0",
    "table": null,
    "proto": null,
    "scope": "link",
    "src": null,
    "metric": null,
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "124.214.110.113",
    "from": null,
    "via": "192.168.255.11",
    "dev": "lan0",
    "table": null,
    "proto": null,
    "scope": null,
    "src": null,
    "metric": "4",
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "172.17.0.0/16",
    "from": null,
    "via": null,
    "dev": "docker0",
    "table": null,
    "proto": "kernel",
    "scope": "link",
    "src": "172.17.0.1",
    "metric": null,
    "linkdown": "linkdown",
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "172.18.0.0/16",
    "from": null,
    "via": null,
    "dev": "br-fea43fdf31f1",
    "table": null,
    "proto": "kernel",
    "scope": "link",
    "src": "172.18.0.1",
    "metric": null,
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "172.19.0.0/16",
    "from": null,
    "via": null,
    "dev": "br-6f9e681d15b0",
    "table": null,
    "proto": "kernel",
    "scope": "link",
    "src": "172.19.0.1",
    "metric": null,
    "linkdown": "linkdown",
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "192.168.0.0/16",
    "from": null,
    "via": null,
    "dev": "lan0",
    "table": null,
    "proto": "kernel",
    "scope": "link",
    "src": "192.168.254.1",
    "metric": null,
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "192.168.1.0/24",
    "from": null,
    "via": null,
    "dev": "wan0",
    "table": null,
    "proto": "kernel",
    "scope": "link",
    "src": "192.168.1.9",
    "metric": null,
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": null,
    "network": "192.168.1.0/24",
    "from": null,
    "via": "192.168.255.11",
    "dev": "lan0",
    "table": null,
    "proto": null,
    "scope": null,
    "src": null,
    "metric": "3",
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": "broadcast",
    "local": null,
    "multicast": null,
    "network": "127.0.0.0",
    "from": null,
    "via": null,
    "dev": "lo",
    "table": "local",
    "proto": "kernel",
    "scope": "link",
    "src": "127.0.0.1",
    "metric": null,
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": "local",
    "multicast": null,
    "network": "127.0.0.1",
    "from": null,
    "via": null,
    "dev": "lo",
    "table": "local",
    "proto": "kernel",
    "scope": "host",
    "src": "127.0.0.1",
    "metric": null,
    "linkdown": null,
    "unresolved": null
  },
  {
    "broadcast": null,
    "local": null,
    "multicast": "multicast",
    "network": "239.255.255.250/32",
    "from": "192.168.1.109/32",
    "via": null,
    "dev": null,
    "table": "default",
    "proto": "17",
    "scope": null,
    "src": null,
    "metric": null,
    "linkdown": null,
    "unresolved": "unresolved"
  }
]

這已經足夠滿足我的需要了,但是,如果您有空閒時間,仍然有改進的空間:可以刪除所有空值,可以將“metric”值轉換為數字,以及“broadcast”、“local” 、“multicast 」、「linkdown」和「unresolved」可以是布林值。

答案2

我寫了一個名為 cli 的工具,jc它將命令輸出轉換netstat -rrouteJSON。

$ jc netstat -rn | jq
[
  {
    "destination": "0.0.0.0",
    "gateway": "192.168.71.2",
    "genmask": "0.0.0.0",
    "route_flags": "UG",
    "mss": 0,
    "window": 0,
    "irtt": 0,
    "iface": "ens33",
    "kind": "route",
    "route_flags_pretty": [
      "UP",
      "GATEWAY"
    ]
  },
  {
    "destination": "172.17.0.0",
    "gateway": "0.0.0.0",
    "genmask": "255.255.0.0",
    "route_flags": "U",
    "mss": 0,
    "window": 0,
    "irtt": 0,
    "iface": "docker0",
    "kind": "route",
    "route_flags_pretty": [
      "UP"
    ]
  },
  {
    "destination": "192.168.71.0",
    "gateway": "0.0.0.0",
    "genmask": "255.255.255.0",
    "route_flags": "U",
    "mss": 0,
    "window": 0,
    "irtt": 0,
    "iface": "ens33",
    "kind": "route",
    "route_flags_pretty": [
      "UP"
    ]
  }
]
$ jc route | jq
[
  {
    "destination": "default",
    "gateway": "gateway",
    "genmask": "0.0.0.0",
    "flags": "UG",
    "metric": 100,
    "ref": 0,
    "use": 0,
    "iface": "ens33",
    "flags_pretty": [
      "UP",
      "GATEWAY"
    ]
  },
  {
    "destination": "172.17.0.0",
    "gateway": "0.0.0.0",
    "genmask": "255.255.0.0",
    "flags": "U",
    "metric": 0,
    "ref": 0,
    "use": 0,
    "iface": "docker0",
    "flags_pretty": [
      "UP"
    ]
  },
  {
    "destination": "192.168.71.0",
    "gateway": "0.0.0.0",
    "genmask": "255.255.255.0",
    "flags": "U",
    "metric": 100,
    "ref": 0,
    "use": 0,
    "iface": "ens33",
    "flags_pretty": [
      "UP"
    ]
  }
]

也支援許多其他命令。

https://github.com/kellyjonbrazil/jc

答案3

預設情況下,iproute2 實用程式支援 --json 選項,例如

ip --json address show
ip --json link show

etc..

它以 JSON 格式列出所有配置/統計資料。

但在某些情況下,例如路由,它只列出特定的路由表。要列出所有表,您可以明確使用以下命令:

ip --json route show table all

您也可以使用它們的名稱來取得特定的表,例如:

ip --json route show table default

[我不知道你是否已經得到答案,如果沒有,我希望這對你有幫助......再見:) ]

相關內容