I got tired of looking up the command syntax for port forwarding
every time I wanted to use it, so I wrote this short script to
make the process a little easier.  It assumes a PPP connection on
interface ppp0 (in order to grab the external IP) but you can
change this in the code if you have a different setup (cable
modem or something else).  This has been especially useful in my
home network because our ISP allocates dynamic IP's and I've
gotten really tired of looking up the IP manually every time
I want to set up forwarding.

It's useful because if you want to use port forwarding for any
non-standard services (such as playing games) you can just edit
the script (or /etc/services) and add in the port number for the
game.

Just type ./fwd with no arguments to find out the syntax and
the assumptions I made about certain files.  Make sure you install
ipmasqadm first.

Chris
[EMAIL PROTECTED]


--- begin fwd.pl ---

#!/usr/bin/perl

use Socket;

# non-standard applications/ports (global)
#example:
#$ports{ftp} = '21/tcp';

# what to print (if different from what's listed in /etc/services)
$output{ftp} = 'FTP';

sub getstatus {
    $status = `/usr/sbin/ipmasqadm portfw -l`;
    if (!(($status =~ /TCP/) || ($status =~ /UDP/))) {
 print "\nNo forwarding active.\n\n";
    } else {
 print("\n$status\n");
    }
}

if ($#ARGV < 0) {
    print "\nUsage:\n\n";
    print "  $0\t\t\tPrints this screen\n";
    print "  $0 status\t\tGet current port forwarding status\n";
    print "  $0 app [+/-]host\tEnable/disable forwarding for selected
app\n";
    print "\nExamples:\n\n";
    print "  $0 ftp +tigger\tEnable FTP forwarding to host tigger\n";
    print "  $0 telnet -pooh\tDisable Telnet forwarding to host roo\n";
    print "\nCaveats:\n\n";
    print "  This script depends on the port numbers and transport layer
protocols being\n";
    print "  defined either in /etc/services or in the global variables
section of the\n";
    print "  script.  It also assumes that all internal hostnames used as
parameters\n";
    print "  will be defined in /etc/hosts and that the external IP is
already\n";
    print "  established on interface ppp0.\n\n";
    exit(1);
}

$app = shift(@ARGV);
$app =~ tr/A-Z/a-z/;

if ($app eq 'status') {
    &getstatus;
    exit(1);
} elsif (!($ports{$app})) {
    $ports{$app} = `grep $app /etc/services`;
    if ($ports{$app}) {
 $ports{$app} =~ /.*^$app\s+([0-9]+\/...).*/;
 $ports{$app} = $1;
 if (!($output{$app})) {
     $output{$app} = ucfirst($app);
 }
    } else {
 print "\nInvalid application selected.\n\n";
 exit(1);
    }
}

if ($#ARGV < 0) {
    print "\nNeed to specify the target host.\n\n";
    exit(1);
}

$ARGV[0] =~ /([\+\-])(.*)/;
$addremove = $1;
$host = $2;

$ip = `grep $host /etc/hosts`;
if (!($ip)) {
    print "\nInvalid hostname selected.\n\n";
    exit(1);
}

$ip =~ /([0-9\.]*)\s.*/;
$intip = $1;
print "\nInternal IP is $intip\n";

$ip = `ifconfig ppp0`;
$ip =~ /.*inet addr:([0-9\.]*)\s.*/;
$extip = $1;
print "External IP is $extip\n";

$ports{$app} =~ /(.*)\/(.*)/;
$portnum = $1;
$protocol = $2;

if ($addremove eq '+') {
    system("/usr/sbin/ipmasqadm portfw -a -P $protocol -L $extip $portnum -R
$intip $portnum");
    print "$output{$app} forwarding (port $portnum) to $host is enabled.\n";
} else {
    system("/usr/sbin/ipmasqadm portfw -d -P $protocol -L $extip $portnum -R
$intip $portnum");
    print "$output{$app} forwarding (port $portnum) to $host is
disabled.\n";
}

&getstatus;

--- end fwd.pl ---



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For daily digest info, email [EMAIL PROTECTED]

Reply via email to