Routes management — pyroute2 0.9.4 documentation

# Routes management

## Simple routes

Ordinary routes management is really simple:

    # create a route
    ndb.routes.create(
        dst='10.0.0.0/24',
        gateway='192.168.122.1'
    ).commit()

    # retrieve a route and change it
    with ndb.routes['10.0.0.0/24'] as route:
        route.set(gateway='192.168.122.10')

    # remove a route
    with ndb.routes['10.0.0.0/24'] as route:
        route.remove()

## Multiple routing tables

But Linux systems have more than one routing table:

    >>> set((x.table for x in ndb.routes.summary()))
    {101, 5001, 5002, 254, 255}

The main routing table is 254. All the routes people mostly work with are in that table. To address routes in other routing tables, you can use dict specs:

    ndb.routes.create(
        dst='10.0.0.0/24',
        gateway='192.168.122.1',
        table=101
    ).commit()

    with ndb.routes[{'table': 101, 'dst': '10.0.0.0/24'}] as route:
        route.set('gateway', '192.168.122.10')
        route.set('priority', 500)

    with ndb.routes[{'table': 101, 'dst': '10.0.0.0/24'}] as route:
        route.remove()

## Route metrics

route['metrics'] attribute provides a dictionary-like object that reflects route metrics like hop limit, mtu etc:

    # set up all metrics from a dictionary
    with ndb.routes['10.0.0.0/24'] as route:
        route.set('metrics', {'mtu': 1500, 'hoplimit': 20})

    # fix individual metrics
    with ndb.routes['10.0.0.0/24']['metrics'] as metrics:
        metrics.set('mtu', 1500)
        metrics.set('hoplimit', 20)

## MPLS routes

See here: MPLS howto