This file is indexed.

/usr/share/doc/python-iptables-doc/html/examples.html is in python-iptables-doc 0.12.0-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Examples &#8212; python-iptables 0.4.0-dev documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.4.0-dev',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="prev" title="Usage" href="usage.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="usage.html" title="Usage"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">python-iptables 0.4.0-dev documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="examples">
<h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline"></a></h1>
<div class="section" id="rules">
<h2>Rules<a class="headerlink" href="#rules" title="Permalink to this headline"></a></h2>
<p>In <code class="docutils literal"><span class="pre">python-iptables</span></code>, you usually first create a rule, and set any
source/destination address, in/out interface and protocol specifiers, for
example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">in_interface</span> <span class="o">=</span> <span class="s2">&quot;eth0&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">src</span> <span class="o">=</span> <span class="s2">&quot;192.168.1.0/255.255.255.0&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
</pre></div>
</div>
<p>This creates a rule that will match TCP packets coming in on eth0, with a
source IP address of 192.168.1.0/255.255.255.0.</p>
<p>A rule may contain matches and a target. A match is like a filter matching
certain packet attributes, while a target tells what to do with the packet
(drop it, accept it, transform it somehow, etc). One can create a match or
target via a Rule:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_match</span><span class="p">(</span><span class="s2">&quot;tcp&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_target</span><span class="p">(</span><span class="s2">&quot;DROP&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Match and target parameters can be changed after creating them. It is also
perfectly valid to create a match or target via instantiating them with
their constructor, but you still need a rule and you have to add the matches
and the target to their rule manually:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;tcp&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;DROP&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span>
</pre></div>
</div>
<p>Any parameters a match or target might take can be set via the attributes of
the object. To set the destination port for a TCP match:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_match</span><span class="p">(</span><span class="s2">&quot;tcp&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">dport</span> <span class="o">=</span> <span class="s2">&quot;80&quot;</span>
</pre></div>
</div>
<p>To set up a rule that matches packets marked with 0xff:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_match</span><span class="p">(</span><span class="s2">&quot;mark&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">mark</span> <span class="o">=</span> <span class="s2">&quot;0xff&quot;</span>
</pre></div>
</div>
<p>Parameters are always strings. You can supply any string as the parameter
value, but note that most extensions validate their parameters. For example
this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;ACCEPT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;state&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;RELATED,ESTABLISHED&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>will work. However, if you change the <cite>state</cite> parameter:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;ACCEPT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;state&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;RELATED,ESTABLISHED,FOOBAR&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">python-iptables</span></code> will throw an exception:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;state.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">7</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
    <span class="n">match</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;RELATED,ESTABLISHED,FOOBAR&quot;</span>
  <span class="n">File</span> <span class="s2">&quot;/home/user/Projects/python-iptables/iptc/ip4tc.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">369</span><span class="p">,</span> <span class="ow">in</span> <span class="fm">__setattr__</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">,</span> <span class="s2">&quot;-&quot;</span><span class="p">),</span> <span class="n">value</span><span class="p">)</span>
  <span class="n">File</span> <span class="s2">&quot;/home/user/Projects/python-iptables/iptc/ip4tc.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">286</span><span class="p">,</span> <span class="ow">in</span> <span class="n">parse</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_parse</span><span class="p">(</span><span class="n">argv</span><span class="p">,</span> <span class="n">inv</span><span class="p">,</span> <span class="n">entry</span><span class="p">)</span>
  <span class="n">File</span> <span class="s2">&quot;/home/user/Projects/python-iptables/iptc/ip4tc.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">516</span><span class="p">,</span> <span class="ow">in</span> <span class="n">_parse</span>
    <span class="n">ct</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ptrptr</span><span class="p">,</span> <span class="n">ct</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ct</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">)))</span>
  <span class="n">File</span> <span class="s2">&quot;/home/user/Projects/python-iptables/iptc/xtables.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">736</span><span class="p">,</span> <span class="ow">in</span> <span class="n">new</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">fn</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  <span class="n">File</span> <span class="s2">&quot;/home/user/Projects/python-iptables/iptc/xtables.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1031</span><span class="p">,</span> <span class="ow">in</span> <span class="n">parse_match</span>
    <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">iptc</span><span class="o">.</span><span class="n">xtables</span><span class="o">.</span><span class="n">XTablesError</span><span class="p">:</span> <span class="n">state</span><span class="p">:</span> <span class="n">parameter</span> <span class="n">error</span> <span class="o">-</span><span class="mi">2</span> <span class="p">(</span><span class="n">RELATED</span><span class="p">,</span><span class="n">ESTABLISHED</span><span class="p">,</span><span class="n">FOOBAR</span><span class="p">)</span>
</pre></div>
</div>
<p>Certain parameters take a string that optionally consists of multiple words.
The comment match is a good example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">src</span> <span class="o">=</span> <span class="s2">&quot;127.0.0.1&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;udp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_target</span><span class="p">(</span><span class="s2">&quot;ACCEPT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_match</span><span class="p">(</span><span class="s2">&quot;comment&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">comment</span> <span class="o">=</span> <span class="s2">&quot;this is a test comment&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that this is still just one parameter value.</p>
<p>However, when a match or a target takes multiple parameter values, that needs
to be passed in as a list. Let&#8217;s assume you have created and set up an
<code class="docutils literal"><span class="pre">ipset</span></code> called <code class="docutils literal"><span class="pre">blacklist</span></code> via the <code class="docutils literal"><span class="pre">ipset</span></code> command. To create a rule
with a match for this set:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_match</span><span class="p">(</span><span class="s2">&quot;set&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">match_set</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;blacklist&#39;</span><span class="p">,</span> <span class="s1">&#39;src&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>Note how this time a list was used for the parameter value, since the <code class="docutils literal"><span class="pre">set</span></code>
match <code class="docutils literal"><span class="pre">match_set</span></code> parameter expects two values. See the <code class="docutils literal"><span class="pre">iptables</span></code>
manpages to find out what the extensions you use expect. See <a class="reference external" href="http://ipset.netfilter.org/">ipset</a> for more
information.</p>
<p>When you are ready constructing your rule, add them to the chain you want it
to show up in:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>This will put your rule into the INPUT chain in the filter table.</p>
</div>
<div class="section" id="chains-and-tables">
<h2>Chains and tables<a class="headerlink" href="#chains-and-tables" title="Permalink to this headline"></a></h2>
<p>You can of course also check what a rule&#8217;s source/destination address,
in/out inteface etc is. To print out all rules in the FILTER table:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">chain</span> <span class="ow">in</span> <span class="n">table</span><span class="o">.</span><span class="n">chains</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="nb">print</span> <span class="s2">&quot;=======================&quot;</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="nb">print</span> <span class="s2">&quot;Chain &quot;</span><span class="p">,</span> <span class="n">chain</span><span class="o">.</span><span class="n">name</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="s2">&quot;Rule&quot;</span><span class="p">,</span> <span class="s2">&quot;proto:&quot;</span><span class="p">,</span> <span class="n">rule</span><span class="o">.</span><span class="n">protocol</span><span class="p">,</span> <span class="s2">&quot;src:&quot;</span><span class="p">,</span> <span class="n">rule</span><span class="o">.</span><span class="n">src</span><span class="p">,</span> <span class="s2">&quot;dst:&quot;</span><span class="p">,</span> \
<span class="gp">&gt;&gt;&gt; </span>              <span class="n">rule</span><span class="o">.</span><span class="n">dst</span><span class="p">,</span> <span class="s2">&quot;in:&quot;</span><span class="p">,</span> <span class="n">rule</span><span class="o">.</span><span class="n">in_interface</span><span class="p">,</span> <span class="s2">&quot;out:&quot;</span><span class="p">,</span> <span class="n">rule</span><span class="o">.</span><span class="n">out_interface</span><span class="p">,</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="s2">&quot;Matches:&quot;</span><span class="p">,</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">for</span> <span class="n">match</span> <span class="ow">in</span> <span class="n">rule</span><span class="o">.</span><span class="n">matches</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="nb">print</span> <span class="n">match</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="s2">&quot;Target:&quot;</span><span class="p">,</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="n">rule</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">name</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="s2">&quot;=======================&quot;</span>
</pre></div>
</div>
<p>As you see in the code snippet above, rules are organized into chains, and
chains are in tables. You have a fixed set of tables; for IPv4:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">*</span> <span class="n">FILTER</span><span class="p">,</span>
<span class="o">*</span> <span class="n">NAT</span><span class="p">,</span>
<span class="o">*</span> <span class="n">MANGLE</span> <span class="ow">and</span>
<span class="o">*</span> <span class="n">RAW</span><span class="o">.</span>
</pre></div>
</div>
<p>For IPv6 the tables are:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">*</span> <span class="n">FILTER</span><span class="p">,</span>
<span class="o">*</span> <span class="n">MANGLE</span><span class="p">,</span>
<span class="o">*</span> <span class="n">RAW</span> <span class="ow">and</span>
<span class="o">*</span> <span class="n">SECURITY</span><span class="o">.</span>
</pre></div>
</div>
<p>To access a table:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">table</span><span class="o">.</span><span class="n">name</span>
<span class="go">filter</span>
</pre></div>
</div>
<p>To create a new chain in the FILTER table:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">create_chain</span><span class="p">(</span><span class="s2">&quot;testchain&quot;</span><span class="p">)</span>

<span class="go">$ sudo iptables -L -n</span>
<span class="go">[...]</span>
<span class="go">Chain testchain (0 references)</span>
<span class="go">target     prot opt source               destination</span>
</pre></div>
</div>
<p>To access an existing chain:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;INPUT&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">)</span>
<span class="go">10</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="more-about-matches-and-targets">
<h2>More about matches and targets<a class="headerlink" href="#more-about-matches-and-targets" title="Permalink to this headline"></a></h2>
<p>There are basic targets, such as <code class="docutils literal"><span class="pre">DROP</span></code> and <code class="docutils literal"><span class="pre">ACCEPT</span></code>. E.g. to reject
packets with source address <code class="docutils literal"><span class="pre">127.0.0.1/255.0.0.0</span></code> coming in on any of the
<code class="docutils literal"><span class="pre">eth</span></code> interfaces:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">in_interface</span> <span class="o">=</span> <span class="s2">&quot;eth+&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">src</span> <span class="o">=</span> <span class="s2">&quot;127.0.0.1/255.0.0.0&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;DROP&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>To instantiate a target or match, we can either create an object like above,
or use the <code class="docutils literal"><span class="pre">rule.create_target(target_name)</span></code> and
<code class="docutils literal"><span class="pre">rule.create_match(match_name)</span></code> methods. For example, in the code above
target could have been created as:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">create_target</span><span class="p">(</span><span class="s2">&quot;DROP&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>instead of:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;DROP&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span>
</pre></div>
</div>
<p>The former also adds the match or target to the rule, saving a call.</p>
<p>Another example, using a target which takes parameters. Let&#8217;s mark packets
going to <code class="docutils literal"><span class="pre">192.168.1.2</span></code> UDP port <code class="docutils literal"><span class="pre">1234</span></code> with <code class="docutils literal"><span class="pre">0xffff</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">MANGLE</span><span class="p">),</span> <span class="s2">&quot;PREROUTING&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">dst</span> <span class="o">=</span> <span class="s2">&quot;192.168.1.2&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;udp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;udp&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">dport</span> <span class="o">=</span> <span class="s2">&quot;1234&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;MARK&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span><span class="o">.</span><span class="n">set_mark</span> <span class="o">=</span> <span class="s2">&quot;0xffff&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>Matches are optional (specifying a target is mandatory). E.g. to insert a rule
to NAT TCP packets going out via <code class="docutils literal"><span class="pre">eth0</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">NAT</span><span class="p">),</span> <span class="s2">&quot;POSTROUTING&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">out_interface</span> <span class="o">=</span> <span class="s2">&quot;eth0&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;MASQUERADE&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span><span class="o">.</span><span class="n">to_ports</span> <span class="o">=</span> <span class="s2">&quot;1234&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>Here only the properties of the rule decide whether the rule will be applied
to a packet.</p>
<p>Matches are optional, but we can add multiple matches to a rule. In the
following example we will do that, using the <code class="docutils literal"><span class="pre">iprange</span></code> and the <code class="docutils literal"><span class="pre">tcp</span></code>
matches:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="s2">&quot;tcp&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;tcp&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">dport</span> <span class="o">=</span> <span class="s2">&quot;22&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;iprange&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">src_range</span> <span class="o">=</span> <span class="s2">&quot;192.168.1.100-192.168.1.200&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">dst_range</span> <span class="o">=</span> <span class="s2">&quot;172.22.33.106&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;DROP&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>This is the <code class="docutils literal"><span class="pre">python-iptables</span></code> equivalent of the following iptables command:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># 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</span>
</pre></div>
</div>
<p>You can of course negate matches, just like when you use <code class="docutils literal"><span class="pre">!</span></code> in front of a
match with iptables. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Rule</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Match</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;mac&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="o">.</span><span class="n">mac_source</span> <span class="o">=</span> <span class="s2">&quot;!00:11:22:33:44:55&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">add_match</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rule</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span><span class="n">rule</span><span class="p">,</span> <span class="s2">&quot;ACCEPT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">),</span> <span class="s2">&quot;INPUT&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span><span class="o">.</span><span class="n">insert_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
</pre></div>
</div>
<p>This results in:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ sudo iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            MAC ! 00:11:22:33:44:55

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
</pre></div>
</div>
</div>
<div class="section" id="counters">
<h2>Counters<a class="headerlink" href="#counters" title="Permalink to this headline"></a></h2>
<p>You can query rule and chain counters, e.g.:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s1">&#39;OUTPUT&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="p">(</span><span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">get_counters</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span>
</pre></div>
</div>
<p>However, the counters are only refreshed when the underlying low-level
iptables connection is refreshed in <code class="docutils literal"><span class="pre">Table</span></code> via <code class="docutils literal"><span class="pre">table.refresh()</span></code>. For
example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s1">&#39;OUTPUT&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="p">(</span><span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">get_counters</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="s2">&quot;Please send some traffic&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="c1"># Here you will get back the same counter values as above</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="p">(</span><span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">get_counters</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span>
</pre></div>
</div>
<p>This will show you the same counter values even if there was traffic hitting
your rules. You have to refresh your table to get update your counters:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s1">&#39;OUTPUT&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="p">(</span><span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">get_counters</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="s2">&quot;Please send some traffic&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>  <span class="c1"># Here: refresh table to update rule counters</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="p">(</span><span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">get_counters</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="nb">print</span> <span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span>
</pre></div>
</div>
<p>What is more, if you add:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">iptables</span> <span class="o">-</span><span class="n">A</span> <span class="n">OUTPUT</span> <span class="o">-</span><span class="n">p</span> <span class="n">tcp</span> <span class="o">--</span><span class="n">sport</span> <span class="mi">80</span>
<span class="n">iptables</span> <span class="o">-</span><span class="n">A</span> <span class="n">OUTPUT</span> <span class="o">-</span><span class="n">p</span> <span class="n">tcp</span> <span class="o">--</span><span class="n">sport</span> <span class="mi">22</span>
</pre></div>
</div>
<p>you can query rule and chain counters together with the protocol and sport(or
dport), e.g.:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s1">&#39;OUTPUT&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">for</span> <span class="n">match</span> <span class="ow">in</span> <span class="n">rule</span><span class="o">.</span><span class="n">matches</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="p">(</span><span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="n">get_counters</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="nb">print</span> <span class="n">packets</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">,</span> <span class="n">match</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">match</span><span class="o">.</span><span class="n">sport</span>
</pre></div>
</div>
</div>
<div class="section" id="autocommit">
<h2>Autocommit<a class="headerlink" href="#autocommit" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal"><span class="pre">Python-iptables</span></code> by default automatically performs an iptables commit after
each operation. That is, after you add a rule in <code class="docutils literal"><span class="pre">python-iptables</span></code>, that
will take effect immediately.</p>
<p>It may happen that you want to batch together certain operations. A typical
use case is traversing a chain and removing rules matching a specific
criteria. If you do this with autocommit enabled, after the first delete
operation, your chain&#8217;s state will change and you have to restart the
traversal. You can do something like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">removed</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s2">&quot;FORWARD&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">removed</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="n">removed</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="k">if</span> <span class="n">rule</span><span class="o">.</span><span class="n">out_interface</span> <span class="ow">and</span> <span class="s2">&quot;eth0&quot;</span> <span class="ow">in</span> <span class="n">rule</span><span class="o">.</span><span class="n">out_interface</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">chain</span><span class="o">.</span><span class="n">delete_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="n">removed</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span>            <span class="k">break</span>
</pre></div>
</div>
<p>This is clearly not ideal and the code is not very readable. An alternative is
to disable autocommits, traverse the chain, removing one or more rules, than
commit it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">iptc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="n">iptc</span><span class="o">.</span><span class="n">Table</span><span class="o">.</span><span class="n">FILTER</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span><span class="o">.</span><span class="n">autocommit</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chain</span> <span class="o">=</span> <span class="n">iptc</span><span class="o">.</span><span class="n">Chain</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="s2">&quot;FORWARD&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">rules</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">if</span> <span class="n">rule</span><span class="o">.</span><span class="n">out_interface</span> <span class="ow">and</span> <span class="s2">&quot;eth0&quot;</span> <span class="ow">in</span> <span class="n">rule</span><span class="o">.</span><span class="n">out_interface</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">chain</span><span class="o">.</span><span class="n">delete_rule</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span><span class="o">.</span><span class="n">autocommit</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p>The drawback is that <cite>Table</cite> is a singleton, and if you disable autocommit, it
will be disabled for all instances of that <cite>Table</cite>.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Examples</a><ul>
<li><a class="reference internal" href="#rules">Rules</a></li>
<li><a class="reference internal" href="#chains-and-tables">Chains and tables</a></li>
<li><a class="reference internal" href="#more-about-matches-and-targets">More about matches and targets</a></li>
<li><a class="reference internal" href="#counters">Counters</a></li>
<li><a class="reference internal" href="#autocommit">Autocommit</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="usage.html"
                        title="previous chapter">Usage</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/examples.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="usage.html" title="Usage"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">python-iptables 0.4.0-dev documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2010-2017, Vilmos Nebehaj.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
    </div>
  </body>
</html>