israel-lugo / netcalc

Advanced network calculator and address planning helper
GNU General Public License v3.0
21 stars 0 forks source link

Autodetect IP addresses from a routing table output #6

Open israel-lugo opened 7 years ago

israel-lugo commented 7 years ago

The add-file command reads network addresses from files. Right now, it only supports clean lists of addresses, e.g.:

10.0.0.0/24
192.0.2.0/27
10.5.7.128/25

It would be nice, however, to be able to feed add-file directly with the output of a "show route" command from some router, e.g.:

B        10.20.0.0/15 [20/40000] via 198.51.100.134, 3d14h
O        10.34.1.0/24 [110/200] via 198.51.100.142, 7w0d, Vlan0021
O E2     10.35.0.0/16 [110/20] via 198.51.100.142, 7w0d, Vlan0021
B        10.98.0.0/14 [20/40000] via 198.51.100.134, 3d14h
O E2     10.76.0.0/14 [110/20] via 198.51.100.70, 1w0d, Vlan0017
O E2     10.84.0.0/16 [110/20] via 198.51.100.38, 7w0d, Vlan0017
O E2     10.85.0.0/16 [110/20] via 198.51.100.49, 00:44:15, Vlan0013

Of course, we'd need to support any kind of format:

10.0.0.0/8         unreachable [sink_private 18:49:35] * (1)
10.16.64.0/24      via 198.51.100.129 on eth0 [igp_backbone 2016-11-16] * E1 (150/20) [198.51.100.5]
10.68.16.0/24      via 198.51.100.139 on eth0 [igp_backbone 2016-11-14] * IA (150/20) [198.51.100.139]
10.64.1.0/24       via 198.51.100.129 on eth1 [isp1 10:26:59] * (100) [AS65302?]
10.1.0.0/16        via 198.51.100.136 on eth0 [igp_backbone 2016-11-14] * IA (150/20) [198.51.100.136]

These can be dealt with simply by looking for the first thing in the line that looks like an address. Implementation can be either with a regexp (nightmare for IPv6) or by splitting the line into tokens and checking with netaddr.valid_ipv4 and netaddr.valid_ipv6.

The problem comes when we have output like this:

10.5.110.0/23      *[OSPF/10] 2d 01:03:59, metric 20
                    > to 198.51.100.130 via irb.2
10.0.0.0/23        *[OSPF/10] 2d 01:03:59, metric 20
                    > to 198.51.100.141 via irb.2
10.8.123.0/24      *[OSPF/10] 17:01:09, metric 20
                    > to 198.51.100.130 via irb.2
                      to 198.51.100.143 via irb.2
10.8.32.0/24       *[OSPF/10] 2d 01:03:59, metric 20
                    > to 198.51.100.139 via irb.2
10.20.0.0/15       *[OSPF/150] 00:53:42, metric 40000, tag 6666
                    > to 198.51.100.158 via irb.2
10.20.8.0/24       *[OSPF/150] 2d 01:03:59, metric 20, tag 0
                    > to 198.51.100.151 via irb.2

Some of these lines are interesting, some aren't. The problem is some lines have IP addresses that we don't care about (e.g. nexthops). For this kind of output, we'd need to be configurable somehow. Have some kind of way for the user to tell us "ignore stuff that looks like an IP address if it's on a line like this".

For now we'll just let the user worry about filtering invalid lines. They can use grep or similar and only give us what they want.