Skip to main content

linuxnet-iptables

linuxnet-iptables provides programmatic access to the Linux iptables(8) (or ip6tables(8)) command. Using linuxnet-iptables one can view existing chains/rules, create new ones, or delete existing ones. The package documentation is available here.

For the following examples, Python3 (3.6 or later) is required.

>>> from linuxnet.iptables import IptablesPacketFilterTable
>>> table = IptablesPacketFilterTable('filter')
>>> table.read_system_config()
>>> input_chain = table.get_chain('INPUT')
>>> for rule in input_chain:
...    print(' '.join(rule.to_iptables_args()))
...
-j prod_bad_traffic
-m state --state RELATED,ESTABLISHED -j ACCEPT
-j prod_ingress
-j prod_INPUT_ldrop
>>>
>>> print(input_chain.get_packet_count())
183506560
>>>

The above code requires root access in order to successfully invoke the iptables command. If you are uncomfortable running it as root, you can extract the iptables output as root and then process it with linuxnet-iptables (note that the -xnv options must be specified):

# iptables -xnv -L > /tmp/iptables.output
#

Then, as a regular user:

>>> with open("/tmp/iptables.output") as f:
...    output = f.read()
...
>>> from linuxnet.iptables import IptablesPacketFilterTable
>>> table = IptablesPacketFilterTable('filter')
>>> table.init_from_output(output)
True
>>> input_chain = table.get_chain('INPUT')
>>> for rule in input_chain:
...     print(' '.join(rule.to_iptables_args()))
...
-j prod_bad_traffic
-m state --state RELATED,ESTABLISHED -j ACCEPT
-j prod_ingress
-j prod_INPUT_ldrop
>>>

Modifications to the chains are also supported as shown in the following (hereon, root permissions will be assumed).

Creating a new chain:

>>> from linuxnet.iptables import ChainRule, Targets
>>> newchain = table.create_chain('acceptall')
>>> newchain.append_rule(ChainRule(target=Targets.ACCEPT))
>>>
# iptables -nv -L acceptall
Chain acceptall (0 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0
#

Modifying the new chain to only accept TCP packets:

>>> newchain.flush()        # remove the existing rule
>>> from linuxnet.iptables import PacketMatch
>>> match_tcp = PacketMatch().protocol().equals('tcp')
>>> rule = ChainRule(match=match_tcp, target=Targets.ACCEPT)
>>> newchain.append_rule(rule)
>>> newchain.append_rule(ChainRule(target=Targets.DROP))
>>>
# iptables -L acceptall -nv
Chain acceptall (0 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0
#

Deleting the new chain:

>>> table.delete_chain(newchain)
>>>

Installation

Python3 is required.

Available Makefile targets can be listed by invoking make with no arguments.

make install will install the package.

make test runs the unit tests.

Release files for linuxnet-iptables 7.6.5

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

Source distribution (sdist)

Source distribution for linuxnet-iptables 7.6.5
File Size Uploaded
linuxnet_iptables-7.6.5.tar.gz 109.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for linuxnet-iptables 7.6.5
File Interpreter ABI Platform
linuxnet_iptables-7.6.5-py3-none-any.whl Python 3 none any Details

Total release size: 226.7 kB

Release files / linuxnet_iptables-7.6.5.tar.gz

Download URL linuxnet_iptables-7.6.5.tar.gz
Size 109.9 kB
Tags Source
SHA-256 checksum
How to use checksums
08f3464857cdf7efa2dbde5479e1c01231576f1620b553be5de7163d8dde4bf9
BLAKE2b-256 checksum
How to use checksums
79fae050de42ea8fea5a09f1e158fa33f0f0292e069b9bc1bff68b4b9b799006
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release files / linuxnet_iptables-7.6.5-py3-none-any.whl

Download URL linuxnet_iptables-7.6.5-py3-none-any.whl
Size 116.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6075bc0010fd155b16afb2e78e931a74678b9e272d5797c45f51385a528a5df1
BLAKE2b-256 checksum
How to use checksums
d70737bea96256ccc3161005880ab56a6e8d8d734753477c0958d3a994f89744
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release history Release notifications | RSS feed

This release

7.6.5 This release

2 release files

7.4.4

2 release files

7.4.1

2 release files

7.0.3

2 release files

7.0.0

2 release files

6.8.3

2 release files

6.7.0

2 release files

6.6.0

2 release files

6.5.0

2 release files

6.4.5

2 release files

6.3.1

2 release files

6.1.0

2 release files

5.4.0

2 release files

5.3.0

2 release files

5.2.0

2 release files

5.0.4

2 release files

5.0.2

2 release files

4.3.1

2 release files

4.2.1

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.1

2 release files

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