“ip route”的漂亮显示

Goh*_*ohu 5 iproute

我想使用更多的iproute2( ipcommand) 实用程序而不是已弃用的net-tools( ifconfig, route, ...)。

我一直回顾的主要原因net-toolsip route,在我看来,与route特别提供列标题的旧版本相比,其输出不够清晰:

  • ip路由:

    default via 192.168.134.254 dev enp1s0  proto static  metric 100 
    10.42.0.0/24 dev wlp2s0  proto kernel  scope link  src 10.42.0.1  metric 600 
    10.56.30.0/24 dev enx00133b0402c2  proto kernel  scope link  src 10.56.30.143 
    169.254.0.0/16 dev wlp2s0  scope link  metric 1000 
    192.168.57.0/24 dev vboxnet1  proto kernel  scope link  src 192.168.57.1 linkdown 
    192.168.134.0/24 dev enp1s0  proto kernel  scope link  src 192.168.134.142  metric 100 
    
    Run Code Online (Sandbox Code Playgroud)
  • 路线:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         192.168.134.254 0.0.0.0         UG    100    0        0 enp1s0
    10.42.0.0       *               255.255.255.0   U     600    0        0 wlp2s0
    10.56.30.0      *               255.255.255.0   U     0      0        0 enx00133b0402c2
    link-local      *               255.255.0.0     U     1000   0        0 wlp2s0
    192.168.57.0    *               255.255.255.0   U     0      0        0 vboxnet1
    192.168.134.0   *               255.255.255.0   U     100    0        0 enp1s0
    
    Run Code Online (Sandbox Code Playgroud)

问题:有没有办法使用ip命令清晰/漂亮地显示路线?

chb*_*chb 10

软件包iproute2(Debian、Ubuntu)和iproute软件包(CentOS、RedHat)都包含一个专门为此设计的实用程序:routel漂亮的输出格式列出路由

\n

输出示例:

\n
[2022-02-12 04:21:05]\xc2\xbb routel\n         target            gateway          source    proto    scope    dev tbl\n        default        192.168.1.1                                   ob-top \n   10.171.32.0/ 24                     10.171.32.1   kernel     link lxdbr0 \n   169.254.0.0/ 16                                              link ob-top \n    172.17.0.0/ 16                      172.17.0.1   kernel     linkdocker0 \n   192.168.1.0/ 24                   192.168.1.101   kernel     link ob-top \n 192.168.122.0/ 24                   192.168.122.1   kernel     link virbr0  \n    10.171.32.0          broadcast     10.171.32.1   kernel     link lxdbr0 local\n    10.171.32.1              local     10.171.32.1   kernel     host lxdbr0 local\n  10.171.32.255          broadcast     10.171.32.1   kernel     link lxdbr0 local\n      127.0.0.0          broadcast       127.0.0.1   kernel     link     lo local\n     127.0.0.0/ 8            local       127.0.0.1   kernel     host     lo local\n      127.0.0.1              local       127.0.0.1   kernel     host     lo local\n127.255.255.255          broadcast       127.0.0.1   kernel     link     lo local\n     172.17.0.0          broadcast      172.17.0.1   kernel     linkdocker0 local\n     172.17.0.1              local      172.17.0.1   kernel     hostdocker0 local\n 172.17.255.255          broadcast      172.17.0.1   kernel     linkdocker0 local\n    192.168.1.0          broadcast   192.168.1.101   kernel     link ob-top local\n  192.168.1.101              local   192.168.1.101   kernel     host ob-top local\n  192.168.1.255          broadcast   192.168.1.101   kernel     link ob-top local\n  192.168.122.0          broadcast   192.168.122.1   kernel     link virbr0 local\n  192.168.122.1              local   192.168.122.1   kernel     host virbr0 local\n192.168.122.255          broadcast   192.168.122.1   kernel     link virbr0 local\n\n
Run Code Online (Sandbox Code Playgroud)\n


Goh*_*ohu 9

几年后回答我自己的问题......

我刚刚了解到 2018 年 2 月添加了漂亮的颜色显示。这使输出对我来说更容易阅读:

在此输入图像描述

人的IP

   -c[color][={always|auto|never}
          Configure color output. If parameter is omitted or always, color
          output is enabled regardless of stdout state. If parameter is auto,
          stdout is checked to be a terminal before enabling color output. If
          parameter is never, color output is disabled. If specified multiple
          times, the last one takes precedence. This flag is ignored if -json
          is also given.
Run Code Online (Sandbox Code Playgroud)

  • 这仍然很糟糕,但我很感谢您添加这些信息。 (5认同)

meu*_*euh 5

这个 awk 脚本假设输出值是成对的关键字 value,例如scope link,除了第一列和linkdown关键字等一些例外,这可能是错误的。它累积列和数据并打印结果:

awk '
{   i = 1; h = " ip"
    hdr[h] = 1
    col[h,NR] = $i
    for(i=2;i<=NF;){
        if($i=="linkdown"){extra[NR] = $i; i++; continue}
        hdr[$i] = 1
        col[$i,NR] = $(i+1)
        i += 2
    }
}
END{     #PROCINFO[sorted_in] = "@ind_str_asc"
    n = asorti(hdr,x)
    for(i=1;i<=n;i++){ h = x[i]; max[h] = length(h) }
    for(j = 1;j<=NR;j++){
        for(i=1;i<=n;i++){
            h = x[i]
            l = length(col[h,j])
            if(l>max[h])max[h] = l
        }
    }
    for(i=1;i<=n;i++){ h = x[i]; printf "%-*s ",max[h],h }
    printf "\n"
    for(j = 1;j<=NR;j++){
        for(i=1;i<=n;i++){ h = x[i]; printf "%-*s ",max[h],col[h,j] }
        printf "%s\n",extra[j]
    }
}'
Run Code Online (Sandbox Code Playgroud)

结果超过 80 列:

 ip              dev             metric proto  scope src             via             
default          enp1s0          100    static                       192.168.134.254 
10.42.0.0/24     wlp2s0          600    kernel link  10.42.0.1                       
10.56.30.0/24    enx00133b0402c2        kernel link  10.56.30.143                    
169.254.0.0/16   wlp2s0          1000          link                                  
192.168.57.0/24  vboxnet1               kernel link  192.168.57.1                    linkdown
192.168.134.0/24 enp1s0          100    kernel link  192.168.134.142                 
Run Code Online (Sandbox Code Playgroud)

该脚本使用关联数组hdr来保存找到的关键字,二维col数组由此关键字和行号索引以保存值。第一列用一个发明的ip关键字特别处理,该关键字有一个前导空格,以确保它被排序到第一列。该extra数组记录了linkdownlone 关键字。

在数据的末尾,标题被排序到一个索引数组中x,我们遍历所有值找到最大列宽。然后打印列标题,然后打印保存的数据。