Skip to main content

Python Netlink library

Project description

Pyroute2 is a pure Python netlink library. The core requires only Python stdlib, no 3rd party libraries. The library was started as an RTNL protocol implementation, so the name is pyroute2, but now it supports many netlink protocols. Some supported netlink families and protocols:

  • rtnl, network settings — addresses, routes, traffic controls

  • nfnetlink — netfilter API:

    • ipset — IP sets

    • nftables — packet filtering

    • nfct — connection tracking

  • ipq — simplest userspace packet filtering, iptables QUEUE target

  • devlink — manage and monitor devlink-enabled hardware

  • generic — generic netlink families:

    • ethtool — low-level network interface setup

    • wireguard — VPN setup

    • nl80211 — wireless functions API (basic support)

    • taskstats — extended process statistics

    • acpi_events — ACPI events monitoring

    • thermal_events — thermal events monitoring

    • VFS_DQUOT — disk quota events monitoring

Supported systems

Pyroute2 runs natively on Linux and emulates some limited subset of RTNL netlink API on BSD systems on top of PF_ROUTE notifications and standard system tools.

Other platforms are not supported.

The simplest usecase

The objects, provided by the library, are socket objects with an extended API. The additional functionality aims to:

  • Help to open/bind netlink sockets

  • Discover generic netlink protocols and multicast groups

  • Construct, encode and decode netlink and PF_ROUTE messages

Maybe the simplest usecase is to monitor events. Disk quota events:

from pyroute2 import DQuotSocket
# DQuotSocket automatically performs discovery and binding,
# since it has no other functionality beside of the monitoring
with DQuotSocket() as ds:
    for message in ds.get():

Get notifications about network settings changes with IPRoute:

from pyroute2 import IPRoute
with IPRoute() as ipr:
    # With IPRoute objects you have to call bind() manually
    for message in ipr.get():

Network namespace examples

Network namespace manipulation:

from pyroute2 import netns
# create netns
# list
# remove netns

Create veth interfaces pair and move to netns:

from pyroute2 import IPRoute

with IPRoute() as ipr:

    # create interface pair'add',

    # lookup the peer index
    idx = ipr.link_lookup(ifname='v0p1')[0]

    # move the peer to the 'test' netns:'set',

List interfaces in some netns:

from pyroute2 import NetNS
from pprint import pprint

ns = NetNS('test')

More details and samples see in the documentation.


make install or pip install pyroute2


Python >= 2.7

The pyroute2 testing and documentaion framework requirements:

Optional dependencies:

  • mitogen – for distributed rtnl

  • psutil – for ss2 tool

Project details

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyroute2-0.5.11.tar.gz (767.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page