9

I'd like to use more of the iproute2 (ip command) utility instead of the deprecated net-tools (ifconfig, route, ...).

The main reason I keep going back to net-tools is the output of ip route which in my humble opinion is lacking in clarity compared to the old route that notably provides column titles:

  • ip route:

    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 
    
  • route:

    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
    

Question: Is there a way to have a clear/pretty display of the routes using the ip command?

3
  • 6
    ip route | column -t makes it a bit more pretty, but titles are still missing.
    – Thomas
    Sep 11, 2017 at 13:50
  • 2
    BTW, the ip command output was designed that way to allow easier parsing.
    – dirkt
    Sep 11, 2017 at 14:59
  • You could also output all the information in json format with ip -j -d route show table all, and then pretty print it in the format you like with your favourite programming language. Aug 4, 2021 at 8:15

3 Answers 3

5

This awk script assumes, perhaps wrongly, that the output values are in pairs of keyword value, e.g. scope link, with some exceptions like the first column, and the linkdown keyword. It accumulates the columns and data and prints the result:

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]
    }
}'

The result is wider than 80 columns:

 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                 

The script uses associative array hdr to hold the keywords as they are found, and the two-dimensional col array is indexed by this keyword and the line number to hold the value. The first column is treated specially with an invented ip keyword which has a leading space to ensure it gets sorted to the first column. The extra array notes the linkdown lone keyword.

At the end of data, the headers are sorted into an indexing array x, and we go through all the values finding the maximum column width. The column headers are then printed, and then the saved data.

0
4

Answering my own question a few years later...

I just learned that a pretty display with colors was added in Feb 2018. This makes the output more readable to me:

enter image description here

From man 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.
1
  • 2
    It's sill abysmal, but I appreciate you adding this information.
    – chb
    Feb 12 at 8:08
3

The iproute2 package (Debian, Ubuntu) and the iproute package (CentOS, RedHat) both include a utility specifically designed for this: routel

Sample of output:

[2022-02-12 04:21:05]» routel
         target            gateway          source    proto    scope    dev tbl
        default        192.168.1.1                                   ob-top 
   10.171.32.0/ 24                     10.171.32.1   kernel     link lxdbr0 
   169.254.0.0/ 16                                              link ob-top 
    172.17.0.0/ 16                      172.17.0.1   kernel     linkdocker0 
   192.168.1.0/ 24                   192.168.1.101   kernel     link ob-top 
 192.168.122.0/ 24                   192.168.122.1   kernel     link virbr0  
    10.171.32.0          broadcast     10.171.32.1   kernel     link lxdbr0 local
    10.171.32.1              local     10.171.32.1   kernel     host lxdbr0 local
  10.171.32.255          broadcast     10.171.32.1   kernel     link lxdbr0 local
      127.0.0.0          broadcast       127.0.0.1   kernel     link     lo local
     127.0.0.0/ 8            local       127.0.0.1   kernel     host     lo local
      127.0.0.1              local       127.0.0.1   kernel     host     lo local
127.255.255.255          broadcast       127.0.0.1   kernel     link     lo local
     172.17.0.0          broadcast      172.17.0.1   kernel     linkdocker0 local
     172.17.0.1              local      172.17.0.1   kernel     hostdocker0 local
 172.17.255.255          broadcast      172.17.0.1   kernel     linkdocker0 local
    192.168.1.0          broadcast   192.168.1.101   kernel     link ob-top local
  192.168.1.101              local   192.168.1.101   kernel     host ob-top local
  192.168.1.255          broadcast   192.168.1.101   kernel     link ob-top local
  192.168.122.0          broadcast   192.168.122.1   kernel     link virbr0 local
  192.168.122.1              local   192.168.122.1   kernel     host virbr0 local
192.168.122.255          broadcast   192.168.122.1   kernel     link virbr0 local

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.