iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 10.10.10.10 anywhere tcp dpt:6379
ACCEPT tcp -- 10.10.10.10 anywhere tcp dpt:6379
I know iptables-save | awk ' !x[$0]++' | iptables-restore will do the work in shell, but I want to eliminate duplicate IP table entries through python script which runs every 30 seconds.
Something like this?
The middle line is slightly compact; it could be rephrased more readably as
The
setoperation is what removes duplicates here; a Pythonsetis defined as a collection of items where no duplicates are possible.The final newline is important for some applications, while others will happily read input which lacks the final newline (though it is required by POSIX for text files and streams).
If keeping the original order is a requirement,
set()in recent versions of Python should do that, but you might want to explore e.g. Does Python have an ordered set? for a discussion.