More nerd stuff

Since I have way to much on my hands, I wrote a simple little bash script for updating an ip blocklist for all you bittorrent users out there. Not that I use bittorrent for anything illegal. I only download Linux ISOs. A lot. Really.

Script follows below, you pirating bastards.


#!/bin/bash

# Change these variables as necessary
BLOCKLISTURL="http://lists1.phoenixlabs.org.nyud.net:8080/level1.7z"
BLOCKLISTDIR="/path/to/store/blocklist/in"
MAXNUMBACKUPS=5
APPS="wget p7zip"

# Function that check for required programs
check_apps() {
for app in $APPS; do
if ! eval which $app &> /dev/null; then
echo "This script needs $app!"
echo "Install it and run the script again."
exit -1
fi
done
}

# Make sure needed applications are installed
check_apps

# Enter directory where blocklist should be saved
cd ${BLOCKLISTDIR}

# Count number of backups
count=0
for file in $(ls -1); do
(( count += 1))
done

# Try to fetch updated blocklist. If success, backup old and unpack new.
# If number of backups exceed the maximum number you want to have, delete
# the oldest one.
if wget ${BLOCKLISTURL}; then
echo "Creating backup of old list"
if [ $count -gt $MAXNUMBACKUPS ]; then
rm $(ls -ltr | head -2 | grep -v total | awk '{print $8}')
fi
mv level1.txt "level1.old.$(date +%F)"
echo "Unpacking new list"
p7zip -d level1.7z
echo "List updated"
else
echo "Could not update list"
fi

I’ve only used this on my Gentoo Linux system, but I suppose it should work on any Linux or *BSD system with some minor modifications. Oh, and a good idea is to add a cron job that updates your blocklist every once in a while.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: