Skip to content

Commit 2cbd38d

Browse files
author
ldx
committed
Add section about counters to README.
1 parent 5537404 commit 2cbd38d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,48 @@ This is the `python-iptables` equivalent of the following iptables
305305
command:
306306

307307
# 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
308+
309+
Counters
310+
--------
311+
You can query rule and chain counters, e.g.:
312+
313+
>>> import iptc
314+
>>> table = iptc.Table(iptc.Table.FILTER)
315+
>>> chain = iptc.Chain(table, 'OUTPUT')
316+
>>> for rule in chain.rules:
317+
>>> (packets, bytes) = rule.get_counters()
318+
>>> print packets, bytes
319+
320+
However, the counters are only refreshed when the underlying low-level iptables connection is refreshed in `Table` via `table.refresh()`. For example:
321+
322+
import time, sys
323+
import iptc
324+
table = iptc.Table(iptc.Table.FILTER)
325+
chain = iptc.Chain(table, 'OUTPUT')
326+
for rule in chain.rules:
327+
(packets, bytes) = rule.get_counters()
328+
print packets, bytes
329+
print "Please send some traffic"
330+
sys.stdout.flush()
331+
time.sleep(3)
332+
for rule in chain.rules:
333+
# Here you will get back the same counter values as above
334+
(packets, bytes) = rule.get_counters()
335+
print packets, bytes
336+
337+
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:
338+
339+
import time, sys
340+
import iptc
341+
table = iptc.Table(iptc.Table.FILTER)
342+
chain = iptc.Chain(table, 'OUTPUT')
343+
for rule in chain.rules:
344+
(packets, bytes) = rule.get_counters()
345+
print packets, bytes
346+
print "Please send some traffic"
347+
sys.stdout.flush()
348+
time.sleep(3)
349+
table.refresh() # Here: refresh table to update rule counters
350+
for rule in chain.rules:
351+
(packets, bytes) = rule.get_counters()
352+
print packets, bytes

0 commit comments

Comments
 (0)