if_port_forward lets a slice forward traffic from one port to another
[vsys-scripts.git] / exec / if_port_forward
1 #!/usr/bin/perl
2
3 $|=1;
4 $slice=$ARGV[0];
5 $slice_ip=`cat /etc/vservers/$slice/interfaces/0/ip`;
6 $src_port = <STDIN>;
7 $dst_port = <STDIN>;
8
9 chomp($slice_ip);
10 chomp($src_port);
11 chomp($dst_port);
12
13 die ("Src and dst ports need to be numbers") if (($src_port=~/[^0-9]/) || ($src_port=~/[^0-9]/));
14
15 $IPTABLES_CMD="/sbin/iptables -t nat -A PREROUTING -m tcp -p tcp -d $slice_ip --dport $src_port -j DNAT --to-destination $slice_ip:$dst_port";
16 print "Invoking port forwarding command: $IPTABLES_CMD\n";
17 system($IPTABLES_CMD);
18 print "Done.\n";