Τρίτη 5 Αυγούστου 2008

Debian : Bittorrent Ports Script

Oρίστε λοιπόν ο πιο κατάλληλος τρόπος προκειμένου να ελέγξετε τις πόρτες για το πρόγραμμα bittorrent !!

Controlling bittorrent ports

You are running linux, so you are probably running a firewall based on iptables. This script will allow you to open / close incoming ports for the widely used bittorrent program. I am running the console and curses version of bittorrent on one my machines to download torrents (and yes, even for torrents, command line rules!) and I have written this little script to open the appropriate ports in the firewall and close them again when the download ends. (Actually, they are closed automatically by a cron job).

The script will have to be modified for your network setup. An explanation follows.
Usage

Copy the script that follows to a file and save it as bit-ports. An appropriate location would be /sbin or /usr/sbin

Examples:

bit-ports open

bit-ports close

Script

#! /bin/sh
#
# Bittorrent firewall ports control
# (C) by CyberToxic pigi apo Manolis Kiagias
#
case "$1" in
'open')
if [ -z "$(/sbin/iptables-save |grep 6881:6889)" ]; then
/sbin/iptables -I INPUT 3 -d 192.168.0.21 -p tcp -m tcp --dport 6881:6889 -j ACCEPT
echo "Bittorrent ports now OPEN in system firewall."
else
echo "Bittorrent ports already open!!!"
fi
;;
'close')
if [ -n "$(/sbin/iptables-save |grep 6881:6889)" ]; then
if [ -z "$(ps aux |grep bittorrent |grep -v grep)" ]; then
/sbin/iptables -D INPUT -d 192.168.0.21 -p tcp -m tcp --dport 6881:6889 -j ACCEPT
echo "Bittorrent ports now CLOSED in system firewall."
else
echo "Bittorrent is running. Ports will NOT close!"
fi
else
echo "Bittorrent ports already closed!!!"
fi
;;
*)
echo "Usage: bit-ports [open|close]"
exit 1
;;
esac
exit 0

Bittorrent console uses tcp ports 6881 to 6889 for incoming connections. The script opens these ports when called with the 'on' switch and closes them when called with the 'close' switch. It will not close the ports if bittorrent is running!
Assumptions

The script assumes the following:

* Your linux box is behind a router and has an internal address of 192.168.0.21. Modify this to your own IP address.
* The iptables command is located in /sbin. This is true for most distros (debian included).
* You have su privileges. You need to either sudo or su to execute this script since it modifies iptables rules.
* You do really understand what you are doing! (This is a prerequisite for ALL my scripts...)