Skip to main content

===== libnl

A port of libnl <http://www.infradead.org/~tgr/libnl/>_, a collection of libraries providing APIs to the Netlink protocol based Linux kernel interfaces. This library is API-equivalent to the original C library, and should make it relatively easy to convert C programs into pure Python without having to call external binaries.

As Netlink is a Linux-specific protocol, this library will only work on Linux hosts. All communication is done using sockets between the Python process and the Linux kernel. The main driver for porting libnl was to use nl80211 <https://wireless.wiki.kernel.org/en/developers/documentation/nl80211>_ in Python to scan for wireless access points natively, without having to run an external program and parse its output.

  • Python 2.6, 2.7, PyPy, PyPy3, 3.3, and 3.4 supported on Linux.

.. image:: https://img.shields.io/wercker/ci/54f908261d0e8d4b221bfc9d.svg?style=flat-square&label=Wercker%20CI :target: https://app.wercker.com/#applications/54f908261d0e8d4b221bfc9d :alt: Build Status WiFi

.. image:: https://img.shields.io/travis/Robpol86/libnl/master.svg?style=flat-square&label=Travis%20CI :target: https://travis-ci.org/Robpol86/libnl :alt: Build Status

.. image:: https://img.shields.io/codecov/c/github/Robpol86/libnl/master.svg?style=flat-square&label=Codecov :target: https://codecov.io/github/Robpol86/libnl :alt: Coverage Status

.. image:: https://img.shields.io/pypi/v/libnl.svg?style=flat-square&label=Latest :target: https://pypi.python.org/pypi/libnl/ :alt: Latest Version

.. image:: https://img.shields.io/pypi/dm/libnl.svg?style=flat-square&label=PyPI%20Downloads :target: https://pypi.python.org/pypi/libnl/ :alt: Downloads

Quickstart

Install:

.. code:: bash

pip install libnl

Example Implementations

A simple Python program that merely lists network adapters on the host:

.. code:: python

import ctypes
import socket

from libnl.error import errmsg
from libnl.handlers import NL_CB_CUSTOM, NL_CB_VALID, NL_OK
from libnl.linux_private.if_link import IFLA_IFNAME, IFLA_RTA
from libnl.linux_private.netlink import NETLINK_ROUTE, NLMSG_LENGTH, NLM_F_DUMP, NLM_F_REQUEST
from libnl.linux_private.rtnetlink import RTA_DATA, RTA_NEXT, RTA_OK, RTM_GETLINK, ifinfomsg, rtgenmsg
from libnl.misc import get_string
from libnl.msg import nlmsg_data, nlmsg_hdr
from libnl.nl import nl_connect, nl_recvmsgs_default, nl_send_simple
from libnl.socket_ import nl_socket_alloc, nl_socket_modify_cb


def callback(msg, _):
    nlh = nlmsg_hdr(msg)
    iface = ifinfomsg(nlmsg_data(nlh))
    hdr = IFLA_RTA(iface)
    remaining = ctypes.c_int(nlh.nlmsg_len - NLMSG_LENGTH(iface.SIZEOF))
    while RTA_OK(hdr, remaining):
        if hdr.rta_type == IFLA_IFNAME:
            print('Found interface {0}: {1}'.format(iface.ifi_index, get_string(RTA_DATA(hdr)).decode('ascii')))
        hdr = RTA_NEXT(hdr, remaining)
    return NL_OK


sk = nl_socket_alloc()  # Creates an nl_sock instance.
ret = nl_connect(sk, NETLINK_ROUTE)  # Create file descriptor and bind socket.
if ret < 0:
    raise RuntimeError('nl_connect() returned {0} ({1})'.format(ret, errmsg[abs(ret)]))
rt_hdr = rtgenmsg(rtgen_family=socket.AF_PACKET)
ret = nl_send_simple(sk, RTM_GETLINK, NLM_F_REQUEST | NLM_F_DUMP, rt_hdr, rt_hdr.SIZEOF)
if ret < 0:
    raise RuntimeError('nl_send_simple() returned {0} ({1})'.format(ret, errmsg[abs(ret)]))
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, callback, None)  # Add callback to the nl_sock instance.
ret = nl_recvmsgs_default(sk)  # Get kernel's answer, and call attached callbacks.
if ret < 0:
    raise RuntimeError('nl_recvmsgs_default() returned {0} ({1})'.format(ret, errmsg[abs(ret)]))

Here are some more examples with their C equivalents in order from "easy" to "hard":

  • example_list_network_interfaces.py <https://github.com/Robpol86/libnl/blob/master/example_list_network_interfaces.py>_ (list_network_interfaces.c <https://github.com/Robpol86/libnl/blob/master/example_c/list_network_interfaces.c>_)
  • example_show_wifi_interface.py <https://github.com/Robpol86/libnl/blob/master/example_show_wifi_interface.py>_ (show_wifi_interface.c <https://github.com/Robpol86/libnl/blob/master/example_c/show_wifi_interface.c>_)
  • example_scan_access_points.py <https://github.com/Robpol86/libnl/blob/master/example_scan_access_points.py>_ (scan_access_points.c <https://github.com/Robpol86/libnl/blob/master/example_c/scan_access_points.c>_)

Changelog

This project adheres to Semantic Versioning <http://semver.org/>_.

0.3.0 - 2021-02-05

* Fix python 3.6 error
* Fix continue dump when no NL_CB_DUMP_INTR

0.2.0 - 2015-03-26

Added

* Python2.6, PyPy, and PyPy3 support.

0.1.1 - 2015-03-15

* Initial release.

Metadata

Release files for libnl3 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libnl3 0.3.0
File Size Uploaded Provenance Actions
libnl3-0.3.0.tar.gz 67.1 kB Without provenance Details for libnl3-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for libnl3 0.3.0
File Interpreter ABI Platform Actions
libnl3-0.3.0-py3-none-any.whl Python 3 none any Details for libnl3-0.3.0-py3-none-any.whl

Total release size: 156.7 kB

Release files / libnl3-0.3.0.tar.gz

Download URL libnl3-0.3.0.tar.gz
Size 67.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e5738811b1965e2a6bbd152d07b7cf2ffff145c5ef484551fcd8a7311350a17a
BLAKE2b-256 checksum
How to use checksums
65ea23c5597b800b09d32f80b60771b6f24f284721c46793cfebff6aa1dec219
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

Release files / libnl3-0.3.0-py3-none-any.whl

Download URL libnl3-0.3.0-py3-none-any.whl
Size 89.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd7965dea043c12fc370cf6c045d29615b2e3a778e04a8f29bdb0d67780141b7
BLAKE2b-256 checksum
How to use checksums
cc8d094f1125ee42d8cc7d94b4f680be3c25989bcbf962cb75811590044e4134
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

Security for libnl3 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page