Quick start — pyroute2 0.5.9 documentation

NDB is a high level network management module. IT allows to manage interfaces, routes, addresses etc. of connected systems, containers and network namespaces.

NDB work with remote systems via ssh, in that case mitogen module is required. It is possible to connect also OpenBSD and FreeBSD systems, but in read-only mode for now.

# Quick start

Print the routing infotmation in the CSV format:

    with NDB() as ndb:
        for record in ndb.routes.summary(format='csv'):
            print(record)

Print all the interface names on the system:

    with NDB() as ndb:
        print([x.ifname for x in ndb.interfaces.summary()])

Print IP addresses of interfaces in several network namespaces:

    nslist = ['netns01',
              'netns02',
              'netns03']

    with NDB() as ndb:
        for nsname in nslist:
            ndb.sources.add(netns=nsname)
        for record in ndb.interfaces.summary(format='json'):
            print(record)

Add an IP address on an interface:

    with NDB() as ndb:
        with ndb.interfaces['eth0'] as i:
            i.ipaddr.create(address='10.0.0.1', prefixlen=24).commit()
            # ---> <---  NDB waits until the address actually
            #            becomes available

Change an interface property:

    with NDB() as ndb:
        with ndb.interfaces['eth0'] as i:
            i['state'] = 'up'
            i['address'] = '00:11:22:33:44:55'
        # ---> <---  the commit() is called authomatically by
        #            the context manager's __exit__()

Key NDB features:
  - Asynchronously updated database of RTNL objects
  - Data integrity
  - Multiple data sources – local, netns, remote
  - Fault tolerance and memory consumtion limits
  - Transactions