Gopi Gopi - 4 months ago 32
Python Question

how to use pynetfilter_conntrack library of python

I am trying to write a python program for listing established TCP connections. I found pynetfilter_conntrack module. I have installed using

pip install pynetfilter_conntrack
. while importing the library I got error as below

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pynetfilter_conntrack/__init__.py", line 4, in <module>
from pynetfilter_conntrack.func import *
File "/usr/local/lib/python2.7/dist-packages/pynetfilter_conntrack/func.py", line 6, in <module>
library = cdll.LoadLibrary("libnetfilter_conntrack.so.1")
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libnetfilter_conntrack.so.1: cannot open shared object file: No such file or directory


And fixed using command

sudo ln -s /usr/lib/libnetfilter_conntrack.so.3.1.3 /usr/lib/libnetfilter_conntrack.so.1


I searched, but I could not get document on how to use it with some example program. Also it's wiki site is currently down.

http://software.inl.fr/trac/trac.cgi/wiki/pynetfilter_conntrack.

Answer

Web page is down due the finishing of sponsored development. But there is a new maintainer on github trying to prevent the final death of the library.

As I see this repo (0.5) version is currently in development, but got an a example which will give you the key idea of using even 0.4.2 version.

from netifaces AF_INET
import pynetfilter_conntrack

# Print source ip, port and sent bytes
ct = pynetfilter_conntrack.Conntrack()
for item in ct.dump_table(AF_INET):
    print item.orig_ipv4_src, item.orig_port_src, item.repl_counter_bytes
Comments