5

How can I use a NETLINK socket to data from kernel space in PYTHON?

Viccari
  • 9,029
  • 4
  • 43
  • 77
1.618
  • 847
  • 2
  • 11
  • 19
  • 1
    There are netlink libraries. You might need to be more specific in your question. You ought to go through your previous questions and accept some answers too. – MattH Jan 13 '11 at 11:36

2 Answers2

4
import socket
sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW)
sock.bind((0,0))
sock.send(<nlmsghdr>)
sock.recv()
tMC
  • 18,105
  • 14
  • 62
  • 98
  • Do you maybe know an answer to the question over [here](https://stackoverflow.com/questions/35876323/netlink-multicast-in-kernels-above-4)? – user1252280 Mar 10 '16 at 18:38
  • 1
    Use sock.bind((0,-1)). The second argument (-1) is a bitmask of groups to monitor. Setting to -1 sets all bits in the mask. – David Poole Feb 23 '17 at 16:08
3

I believe that the socket Python module has had support for AF_NETLINK sockets since Python 2.5 or so, although I have never used it. There are a few projects out there that use it and can serve as an example:

Searching Google for /usr/bin/python af_netlink comes up with a few more hits, although I will admit that most are not really useful.

thkala
  • 84,049
  • 23
  • 157
  • 201