NDB module¶
NDB is a high level network management module. IT allows to manage interfaces, routes, addresses etc. of connected systems, containers and network namespaces.
In a nuthsell, NDB collects and aggregates netlink events in an SQL database, provides Python objects to reflect the system state, and applies changes back to the system. The database expects updates only from the sources, no manual SQL updates are expected normally.
NDB can 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 information in the CSV format:
with NDB() as ndb:
for record in ndb.routes.summary().format('csv'):
print(record)
Note
More on report filtering and formatting: Report filtering
Note
Since 0.5.11; versions 0.5.10 and earlier used syntax summary(format=’csv’, match={…})
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():
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__()
Reference¶
work in progress