0

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
    ip route | column -t makes it a bit more pretty, but titles are still missing. – Thomas Sep 11 '17 at 13:50
  • 2
    BTW, the ip command output was designed that way to allow easier parsing. – dirkt Sep 11 '17 at 14:59
4

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.

  • That is some nice awk scripting! Also, I did not see the keyword value pairing you pointed out. Thanks. – Gohu Sep 12 '17 at 14:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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