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 dev enp1s0  proto static  metric 100 dev wlp2s0  proto kernel  scope link  src  metric 600 dev enx00133b0402c2  proto kernel  scope link  src dev wlp2s0  scope link  metric 1000 dev vboxnet1  proto kernel  scope link  src linkdown dev enp1s0  proto kernel  scope link  src  metric 100 
  • route:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         UG    100    0        0 enp1s0       *        U     600    0        0 wlp2s0      *        U     0      0        0 enx00133b0402c2
    link-local      *          U     1000   0        0 wlp2s0    *        U     0      0        0 vboxnet1   *        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

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
        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++){
            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                  wlp2s0          600    kernel link                  enx00133b0402c2        kernel link              wlp2s0          1000          link                           vboxnet1               kernel link                    linkdown enp1s0          100    kernel link                 

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.