Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-04-13 19:37:51

#! Member
Registered: 2011-11-29
Posts: 69

[Script] Generate an IP blocklist for P2P

I created a simple script today that generates an IP blocklist using the blocklists provided from
This is useful when using BitTorrent because it prevents you from connecting to some known "bad" IPs if your client supports it.
I believe you can also use this with iptables, though it probably requires some modification.

Checks if the commands used in the script exist
Backups up the last blocklist you generated before generating a new blocklist
Merges all blocklists into one large blocklist
Easy to add/remove blocklists
Generated blocklist is in the .p2p format
Beautifies the blocklist after it's generated (this option can be disabled through a variable that acts as a pseudo boolean)

I suggest changing the blocklist and tmpfldr paths to whatever you prefer before running the script.
Everything is stored inside functions because it's easier for me/you/everyone to debug and play with the script.
I attempted to look into implementing error handling, then realized that bash and error handling simply don't go together. It wouldn't be worth the effort for such a simple script.

Q: What is an IP blocklist?
A: An IP blocklist is a list of IP addresses that you don't want to connect to that can be used with P2P software, though not limited to P2P software

Q: How do I use the blocklist for P2P?
A: Check your BitTorrent client's documentation. The blocklist generated using this script is in the .p2p format [Dropbox]
Feedback is welcome. I don't have any intention/motivation to add new features, but I don't mind small, quick changes or fixes.
Feedback is more than welcome on the syntax. This is my first bash script that's more than a few lines. (likely my last)

# Title:
# Description: Script for downloading and generating an IP blocklist
# Author: Ragnarok (
# Date: 2013-04-13
# Version: 0.1
# Usage: $ bash
# Dependencies: bash, coreutils, gzip, wget

# Save path for the blocklist
# Directory for storing temporary lists

# Beautify the blocklist after it is generated

# Disabled. This doesn't work as expected for some reason
#check_for_paths() {
#  if [[ ! -d "$tmpfldr" ]] && [[ ! -r "$tmpfldr" ]] && [[ ! -w "$tmpfldr" ]]; then
#    echo -e "Please make sure " "$tmpfldr" " exists and you have read/write access\nExiting"
#    exit 1
#  fi

check_for_depends() {
  my_needed_commands="cat gunzip mv rm sed sort wget"

  for needed_command in $my_needed_commands; do
    if ! hash "$needed_command" >/dev/null 2>&1; then
      printf "Command not found in PATH: %s\n" "$needed_command" >&2

  if ((missing_counter > 0)); then
    printf "Minimum %d commands are missing in PATH, aborting\n" "$missing_counter" >&2
    exit 1

backup_blocklist() {
  # Check for and backup blocklist
  echo "Checking for blocklist..."
  if [[ -f "$blocklist" ]]; then
    echo "Backing up blocklist and overwriting old backup if exists..."
    mv -f $blocklist ${blocklist}.old

downloads_lists() {
  # Download blocklists
  # This is where you add/remove blocklists
  # Downloaded lists must follow the following naming format: bl-[zero or more characters].gz
  echo "Downloading lists..."
  wget -q -O $tmpfldr/bl-iana-reserved.gz
  wget -q -O $tmpfldr/bl-ads.gz
  wget -q -O $tmpfldr/bl-bogon.gz
  wget -q -O $tmpfldr/bl-dshield.gz
  wget -q -O $tmpfldr/bl-hijacked.gz
  wget -q -O $tmpfldr/bl-level1.gz
  wget -q -O $tmpfldr/bl-level2.gz
  wget -q -O $tmpfldr/bl-microsoft.gz
  wget -q -O $tmpfldr/bl-spyware.gz
  wget -q -O $tmpfldr/bl-badpeers.gz
  wget -q -O $tmpfldr/bl-primary-threats.gz
  wget -q -O $tmpfldr/bl-iana-multicast.gz
  echo "Download complete"

merge_lists() {
  # Merge blocklists
  echo "Merging lists..."
  cat ${tmpfldr}/bl-*.gz > ${blocklist}.gz

decompress_blocklist() {
  # Decompress the gzip archive
  if [[ -f "${blocklist}.gz" ]]; then
    echo "Decompressing..."
    gunzip ${blocklist}.gz
    echo "Blocklist successfully generated"
    echo -e "Unable to find ${blocklist}.gz\nExiting"
    exit 1

beautify_blocklist () {
  # Cleanup the blocklist
  # This will remove comments, empty lines and sort the list alphabetically
  if $beautify; then
    echo -e "Beautification started\nRemoving comments and blank lines..."
    sed -i -e '/^\#/d' -e '/^$/d' $blocklist
    echo "Sorting alphabetically..."
    sort $blocklist > ${tmpfldr}/blocklist.p2p.tmp && mv -f ${tmpfldr}/blocklist.p2p.tmp $blocklist
    echo "Beautification complete"

remove_temp() {
  # Remove temporary blocklists
  echo "Removing temporary files..."
  rm -f ${tmpfldr}/bl-*.gz


echo "Done!"

exit 0

Last edited by Ragnarok (2013-04-13 19:39:44)


Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo