Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Converter between P2P lists from to IPSet

Project description


|Build Status| |Coverage Status| |Health Status|

iblocklist2ipset is a small utility (really small, check out sources)
to manage your favourite `iblocklists <>`__
with `IPSets <>`__.

All it does, it converts lists from P2P format to the format compatible with
``ipset restore``. Basic usage is following:

.. code-block:: bash

$ iblocklist2ipset generate \
--ipset idiots
"" \
> ~/.ipset
$ sudo ipset restore -f ~/.ipset

And you are done.

Now let's try to figure out why do you need it (and you need it).

Why do you need it

Let's imagine you are living in a country where DMCA makes sense and you
want to make your seedbox safe but you can't use PeerGuardian or PeerBlock
because it sucks on Linux so you decide to use iptables + ipset so...

Wait. It looks like a tool for... piracy? No, of course it is not. Let's start

Let's imagine you have some kind of server (bare metal or cloud VM) and you are
running some mission critical software on it. You do not like botnets or EVIL HACKERS
so want to block all of them to have some good feeling of safety. You heard about
blocklists supplied by `BlueTack <>`__ and wanna use them.

But pure iptables import sucks. iptables actually is a pretty dumb tool for shiny
netfilter and it sucks back and forth if you have a lot of rules (linear complexity
is WTF in 2014, right?). You googled and found amazing `ipset <>`__
tool which integrates with iptables perfectly. Lovely. Let's use it.

So this tool just converts gzipped P2P lists into something you can give to
ipset to restore, right? This is a missing tool and you definitely want it.

Why not to use PeerGuardian etc?

Good question. Actually they are pretty heavyweight and do almost nothing. It is just a
firewall after firewall you already have installed. Look, basically combination of iptables,
ipset and iblocklist2ipset with cron gives you the same features.

Moreover, I want to setup it once and do not worry about anything else. PeerGuardian is
great if you are running this tool on desktop but not on a remote machine. Basically it looks
as excess.


Well, to give you a feel of a tool, let's just execute it as is:

.. code-block:: bash

$ iblocklist2ipset -h
Small utility to convert p2p format of IP Blocklists to IPSet format.

iblocklist2ipset generate [options] BLOCKLIST_URL...
iblocklist2ipset example_restore_ipset_job [options] IPTABLES_NAME IPSET_PATH
iblocklist2ipset example_update_ipset_job [options] IPSET_PATH BLOCKLIST_URL...
iblocklist2ipset -h | --help
iblocklist2ipset --version

-h --help Shows this screen.
--version Shows version and exits.
-i IPSET_NAME --ipset=IPSET_NAME The name of IPSet set [default: blocklist]

To get IP blocklists please visit

So as you can see, usage is pretty straightforward. You can give it multiple blocklist
URLs and it will generate you correct file you can restore with ``ipset restore``. Let's do
it with *hijacked* list:

.. code-block:: bash

$ iblocklist2ipset generate \
"" \
> ipset_to_restore
$ head ipset_to_restore
create blocklist hash:net family inet hashsize 512 maxelem 536
add blocklist
add blocklist
add blocklist
add blocklist
add blocklist
add blocklist
add blocklist
add blocklist
add blocklist

Quite nice, isn't it? It calculated proper sizes for our list also. Now you can just import it

.. code-block:: bash

$ sudo ipset -f ipset_to_restore

And you are safe.

Not really, you need to setup iptables etc to get it work. No problems, if you are using the same
configuration all the times, you can use ``example_restore_ipset_job`` and ``example_update_ipset_job``
commands to get some examples of the usage.

Real world example

I have my Raspberry Pi running some mission critical software (BTSync for example) and I want to use
`*hijacked* list from <>`__.

I have a Raspbian up to date and now I want to use this shiny tool. First, let's install it.

.. code-block:: bash

$ sudo pip install iblocklist2ipset
$ sudo apt-get install -y ipset

(ipset is not bundled by default so install it before).

I want to store an ipset blocklist into ``/etc/ipset.rules``.

.. code-block:: bash

$ iblocklist2ipset example_restore_ipset_job \
-i hijacked blocklist \
/etc/ipset.rules \
> ~/scripts/
$ chmod +x ~/scripts/

Now we created shell scripts. On execution it will restore iptables and ipset configuration. Please
be noticed that ``iblocklist2ipset`` understands virtualenv usage and script is generated with this
knowledge also.

.. code-block:: bash

$ iblocklist2ipset example_update_ipset_job \
-i hijacked \
/etc/ipset.rules "" \
> ~/scripts/
$ chmod +x ~/scripts/

Lovely. Now we have a script to update. Let's update crontab then

.. code-block:: bash

@reboot /home/user/scripts/
@midnight /home/user/scripts/

Why not to store this stuff into iptables permanently? Well this is mostly because of
ipset configuration. It loses it on reboot and it is not really trivial to restore it.

Probably one day I will do it but right now it has to be like this. At least it works
for me.


.. |Build Status| image::

.. |Coverage Status| image::

.. |Health Status| image::
:alt: Code Health

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for iblocklist2ipset, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size iblocklist2ipset-0.0.1-py2.py3-none-any.whl (11.8 kB) File type Wheel Python version 2.7 Upload date Hashes View hashes
Filename, size iblocklist2ipset-0.0.1.tar.gz (7.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page