Skip to content

Commit add95fc

Browse files
author
ldx
committed
Update documentation.
1 parent 714d89d commit add95fc

File tree

2 files changed

+124
-101
lines changed

2 files changed

+124
-101
lines changed

doc/examples.rst

Lines changed: 110 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Examples
22
========
33

4-
Introduction
5-
------------
4+
Rules
5+
-----
66

77
In ``python-iptables``, you usually first create a rule, and set any
88
source/destination address, in/out interface and protocol specifiers, for
9-
example:
9+
example::
1010

1111
>>> import iptc
1212
>>> rule = iptc.Rule()
@@ -20,7 +20,7 @@ source IP address of 192.168.1.0/255.255.255.0.
2020
A rule may contain matches and a target. A match is like a filter matching
2121
certain packet attributes, while a target tells what to do with the packet
2222
(drop it, accept it, transform it somehow, etc). One can create a match or
23-
target via a Rule:
23+
target via a Rule::
2424

2525
>>> rule = iptc.Rule()
2626
>>> m = rule.create_match("tcp")
@@ -29,7 +29,7 @@ target via a Rule:
2929
Match and target parameters can be changed after creating them. It is also
3030
perfectly valid to create a match or target via instantiating them with
3131
their constructor, but you still need a rule and you have to add the matches
32-
and the target to their rule manually:
32+
and the target to their rule manually::
3333

3434
>>> rule = iptc.Rule()
3535
>>> match = iptc.Match(rule, "tcp")
@@ -38,14 +38,14 @@ and the target to their rule manually:
3838
>>> rule.target = target
3939

4040
Any parameters a match or target might take can be set via the attributes of
41-
the object. To set the destination port for a TCP match:
41+
the object. To set the destination port for a TCP match::
4242

4343
>>> rule = iptc.Rule()
4444
>>> rule.protocol = "tcp"
4545
>>> match = rule.create_match("tcp")
4646
>>> match.dport = "80"
4747

48-
To set up a rule that matches packets marked with 0xff:
48+
To set up a rule that matches packets marked with 0xff::
4949

5050
>>> rule = iptc.Rule()
5151
>>> rule.protocol = "tcp"
@@ -55,18 +55,84 @@ To set up a rule that matches packets marked with 0xff:
5555
Parameters are always strings.
5656

5757
When you are ready constructing your rule, add them to the chain you want it
58-
to show up in:
58+
to show up in::
5959

6060
>>> chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
6161
>>> chain.insert_rule(rule)
6262

6363
This will put your rule into the INPUT chain in the filter table.
6464

65-
Simple rule with standard target
66-
--------------------------------
65+
Chains and tables
66+
-----------------
6767

68-
Reject packets with source address ``127.0.0.1/255.0.0.0`` coming in on any of
69-
the eth interfaces:
68+
You can of course also check what a rule's source/destination address,
69+
in/out inteface etc is. To print out all rules in the FILTER table::
70+
71+
>>> import iptc
72+
>>> table = iptc.Table(iptc.Table.FILTER)
73+
>>> for chain in table.chains:
74+
>>> print "======================="
75+
>>> print "Chain ", chain.name
76+
>>> for rule in chain.rules:
77+
>>> print "Rule", "proto:", rule.protocol, "src:", rule.src, "dst:", \
78+
>>> rule.dst, "in:", rule.in_interface, "out:", rule.out_interface,
79+
>>> print "Matches:",
80+
>>> for match in rule.matches:
81+
>>> print match.name,
82+
>>> print "Target:",
83+
>>> print rule.target.name
84+
>>> print "======================="
85+
86+
As you see in the code snippet above, rules are organized into chains, and
87+
chains are in tables. You have a fixed set of tables; for IPv4::
88+
89+
* FILTER,
90+
* NAT,
91+
* MANGLE and
92+
* RAW.
93+
94+
For IPv6 the tables are::
95+
96+
* FILTER,
97+
* MANGLE,
98+
* RAW and
99+
* SECURITY.
100+
101+
To access a table::
102+
103+
>>> import iptc
104+
>>> table = iptc.Table(iptc.Table.FILTER)
105+
>>> print table.name
106+
filter
107+
108+
To create a new chain in the FILTER table::
109+
110+
>>> import iptc
111+
>>> table = iptc.Table(iptc.Table.FILTER)
112+
>>> chain = table.create_chain("testchain")
113+
114+
$ sudo iptables -L -n
115+
[...]
116+
Chain testchain (0 references)
117+
target prot opt source destination
118+
119+
To access an existing chain::
120+
121+
>>> import iptc
122+
>>> table = iptc.Table(iptc.Table.FILTER)
123+
>>> chain = iptc.Chain(table, "INPUT")
124+
>>> chain.name
125+
'INPUT'
126+
>>> len(chain.rules)
127+
10
128+
>>>
129+
130+
More about matches and targets
131+
------------------------------
132+
133+
There are basic targets, such as ``DROP`` and ``ACCEPT``. E.g. to reject
134+
packets with source address ``127.0.0.1/255.0.0.0`` coming in on any of the
135+
``eth`` interfaces::
70136

71137
>>> import iptc
72138
>>> chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
@@ -77,25 +143,22 @@ the eth interfaces:
77143
>>> rule.target = target
78144
>>> chain.insert_rule(rule)
79145

80-
Simple rule not using any match extensions
81-
------------------------------------------
146+
To instantiate a target or match, we can either create an object like above,
147+
or use the ``rule.create_target(target_name)`` and
148+
``rule.create_match(match_name)`` methods. For example, in the code above
149+
target could have been created as::
82150

83-
Inserting a rule to NAT TCP packets going out via ``eth0``:
151+
>>> target = rule.create_target("DROP")
84152

85-
>>> import iptc
86-
>>> chain = iptc.Chain(iptc.Table(iptc.Table.NAT), "POSTROUTING")
87-
>>> rule = iptc.Rule()
88-
>>> rule.protocol = "tcp"
89-
>>> rule.out_interface = "eth0"
90-
>>> target = iptc.Target(rule, "MASQUERADE")
91-
>>> target.to_ports = "1234"
153+
instead of::
154+
155+
>>> target = iptc.Target(rule, "DROP")
92156
>>> rule.target = target
93-
>>> chain.insert_rule(rule)
94157

95-
Rule using the udp match extension
96-
----------------------------------
158+
The former also adds the match or target to the rule, saving a call.
97159

98-
Mark packets going to ``192.168.1.2`` UDP port ``1234`` with ``0xffff``:
160+
Another example, using a target which takes parameters. Let's mark packets
161+
going to ``192.168.1.2`` UDP port ``1234`` with ``0xffff``::
99162

100163
>>> import iptc
101164
>>> chain = iptc.Chain(iptc.Table(iptc.Table.MANGLE), "PREROUTING")
@@ -110,13 +173,25 @@ Mark packets going to ``192.168.1.2`` UDP port ``1234`` with ``0xffff``:
110173
>>> rule.target = target
111174
>>> chain.insert_rule(rule)
112175

113-
Multiple matches with iprange
114-
-----------------------------
176+
Matches are optional (specifying a target is mandatory). E.g. to insert a rule
177+
to NAT TCP packets going out via ``eth0``::
115178

116-
Now we will add multiple matches to a rule. This one is the
117-
``python-iptables`` equivalent of the following iptables command:
179+
>>> import iptc
180+
>>> chain = iptc.Chain(iptc.Table(iptc.Table.NAT), "POSTROUTING")
181+
>>> rule = iptc.Rule()
182+
>>> rule.protocol = "tcp"
183+
>>> rule.out_interface = "eth0"
184+
>>> target = iptc.Target(rule, "MASQUERADE")
185+
>>> target.to_ports = "1234"
186+
>>> rule.target = target
187+
>>> chain.insert_rule(rule)
188+
189+
Here only the properties of the rule decide whether the rule will be applied
190+
to a packet.
118191

119-
# iptables -A INPUT -p tcp –destination-port 22 -m iprange –src-range 192.168.1.100-192.168.1.200 –dst-range 172.22.33.106 -j DROP
192+
Matches are optional, but we can add multiple matches to a rule. In the
193+
following example we will do that, using the ``iprange`` and the ``tcp``
194+
matches::
120195

121196
>>> import iptc
122197
>>> rule = iptc.Rule()
@@ -131,3 +206,7 @@ Now we will add multiple matches to a rule. This one is the
131206
>>> rule.target = iptc.Target(rule, "DROP")
132207
>>> chain = iptc.Chain(iptc.Table.(iptc.Table.FILTER), "INPUT")
133208
>>> chain.insert_rule(rule)
209+
210+
This is the ``python-iptables`` equivalent of the following iptables command::
211+
212+
# iptables -A INPUT -p tcp –destination-port 22 -m iprange –src-range 192.168.1.100-192.168.1.200 –dst-range 172.22.33.106 -j DROP

doc/intro.rst

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ manpage puts it:
1919
rule specifies what to do with a packet that matches. This is called a
2020
`target`, which may be a jump to a user-defined chain in the same table.
2121

22-
``Python-iptables`` provides python bindings to iptables under Linux.
23-
Interoperability with iptables is achieved via using the iptables C libraries
24-
(``libiptc``, ``libxtables``, and the iptables extensions), not calling the
25-
iptables binary and parsing its output.
22+
``Python-iptables`` provides a pythonesque wrapper via python bindings to
23+
iptables under Linux. Interoperability with iptables is achieved via using
24+
the iptables C libraries (``libiptc``, ``libxtables``, and the iptables
25+
extensions), not calling the iptables binary and parsing its output. It is
26+
meant primarily for dynamic and/or complex routers and firewalls, where rules
27+
are often updated or changed, or Python programs wish to interface with the
28+
Linux iptables framework..
29+
30+
|buildstatus|
31+
32+
.. |buildstatus| image:: https://travis-ci.org/ldx/python-iptables.png?branch=master
2633

2734
Compiling and installing
2835
------------------------
@@ -54,67 +61,7 @@ Now you can run the tests::
5461
WARNING: this test will manipulate iptables rules.
5562
Don't do this on a production machine.
5663
Would you like to continue? y/n y
57-
test_table6 (iptc.test.test_iptc.TestTable6) ... ok
58-
test_refresh (iptc.test.test_iptc.TestTable) ... ok
59-
test_table (iptc.test.test_iptc.TestTable) ... ok
60-
test_builtin_chain (iptc.test.test_iptc.TestChain) ... ok
61-
test_chain (iptc.test.test_iptc.TestChain) ... ok
62-
test_chain_counters (iptc.test.test_iptc.TestChain) ... ok
63-
test_chain_policy (iptc.test.test_iptc.TestChain) ... ok
64-
test_chains (iptc.test.test_iptc.TestChain) ... ok
65-
test_create_chain (iptc.test.test_iptc.TestChain) ... ok
66-
test_is_chain (iptc.test.test_iptc.TestChain) ... ok
67-
test_rule_address (iptc.test.test_iptc.TestRule6) ... ok
68-
test_rule_compare (iptc.test.test_iptc.TestRule6) ... ok
69-
test_rule_interface (iptc.test.test_iptc.TestRule6) ... ok
70-
test_rule_iterate (iptc.test.test_iptc.TestRule6) ... ok
71-
test_rule_protocol (iptc.test.test_iptc.TestRule6) ... ok
72-
test_rule_standard_target (iptc.test.test_iptc.TestRule6) ... ok
73-
test_rule_address (iptc.test.test_iptc.TestRule) ... ok
74-
test_rule_compare (iptc.test.test_iptc.TestRule) ... ok
75-
test_rule_fragment (iptc.test.test_iptc.TestRule) ... ok
76-
test_rule_interface (iptc.test.test_iptc.TestRule) ... ok
77-
test_rule_iterate (iptc.test.test_iptc.TestRule) ... ok
78-
test_rule_protocol (iptc.test.test_iptc.TestRule) ... ok
79-
test_rule_standard_target (iptc.test.test_iptc.TestRule) ... ok
80-
81-
----------------------------------------------------------------------
82-
Ran 23 tests in 0.013s
83-
84-
OK
85-
test_match_compare (iptc.test.test_matches.TestMatch) ... ok
86-
test_match_create (iptc.test.test_matches.TestMatch) ... ok
87-
test_match_parameters (iptc.test.test_matches.TestMatch) ... ok
88-
test_udp_insert (iptc.test.test_matches.TestXTUdpMatch) ... ok
89-
test_udp_port (iptc.test.test_matches.TestXTUdpMatch) ... ok
90-
test_mark (iptc.test.test_matches.TestXTMarkMatch) ... ok
91-
test_mark_insert (iptc.test.test_matches.TestXTMarkMatch) ... ok
92-
test_limit (iptc.test.test_matches.TestXTLimitMatch) ... ok
93-
test_limit_insert (iptc.test.test_matches.TestXTLimitMatch) ... ok
94-
test_comment (iptc.test.test_matches.TestCommentMatch) ... ok
95-
test_iprange (iptc.test.test_matches.TestIprangeMatch) ... ok
96-
test_iprange_tcpdport (iptc.test.test_matches.TestIprangeMatch) ... ok
97-
98-
----------------------------------------------------------------------
99-
Ran 12 tests in 0.024s
100-
101-
OK
102-
test_target_compare (iptc.test.test_targets.TestTarget) ... ok
103-
test_target_create (iptc.test.test_targets.TestTarget) ... ok
104-
test_target_parameters (iptc.test.test_targets.TestTarget) ... ok
105-
test_insert (iptc.test.test_targets.TestXTClusteripTarget) ... ok
106-
test_mode (iptc.test.test_targets.TestXTClusteripTarget) ... ok
107-
test_insert (iptc.test.test_targets.TestIPTRedirectTarget) ... ok
108-
test_mode (iptc.test.test_targets.TestIPTRedirectTarget) ... ok
109-
test_insert (iptc.test.test_targets.TestXTTosTarget) ... ok
110-
test_mode (iptc.test.test_targets.TestXTTosTarget) ... ok
111-
test_insert (iptc.test.test_targets.TestIPTMasqueradeTarget) ... ok
112-
test_mode (iptc.test.test_targets.TestIPTMasqueradeTarget) ... ok
113-
114-
----------------------------------------------------------------------
115-
Ran 11 tests in 0.015s
116-
117-
OK
64+
[...]
11865

11966
The ``PATH=$PATH`` part is necessary after ``sudo`` if you have installed into
12067
a ``virtualenv``, since ``sudo`` will reset your environment to a system
@@ -136,9 +83,6 @@ The basic iptables framework and all the match/target extensions are supported
13683
by ``python-iptables``, including IPv4 and IPv6 ones. All IPv4 and IPv6 tables
13784
are supported as well.
13885

139-
Contact
140-
-------
141-
142-
ldx (at) nilvec.com
86+
Full documentation with API reference is available here_.
14387

144-
http://nilvec.com
88+
.. _here: http://ldx.github.com/python-iptables/

0 commit comments

Comments
 (0)