From: Sapan Bhatia Date: Fri, 30 Apr 2010 18:58:50 +0000 (+0000) Subject: Make sure only one instance of the port-forwarding rule is inserted. X-Git-Tag: vsys-scripts-0.95-17~2 X-Git-Url: http://git.onelab.eu/?p=vsys-scripts.git;a=commitdiff_plain;h=c57a1628286a502b45358326d551b94596455fda Make sure only one instance of the port-forwarding rule is inserted. --- diff --git a/exec/if_port_forward b/exec/if_port_forward index 36451c9..400591b 100755 --- a/exec/if_port_forward +++ b/exec/if_port_forward @@ -12,7 +12,14 @@ chomp($dst_port); die ("Src and dst ports need to be numbers") if (($src_port=~/[^0-9]/) || ($src_port=~/[^0-9]/)); -$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"; -print "Invoking port forwarding command: $IPTABLES_CMD\n"; -system($IPTABLES_CMD); +$IPTABLES_CMD_1="/sbin/iptables -t nat -D PREROUTING -m tcp -p tcp -d $slice_ip --dport $src_port -j DNAT --to-destination $slice_ip:$dst_port"; + +$IPTABLES_CMD_2="/sbin/iptables -t nat -A PREROUTING -m tcp -p tcp -d $slice_ip --dport $src_port -j DNAT --to-destination $slice_ip:$dst_port"; + +print "Removing any previous instances of this rule: $IPTABLES_CMD_1\n"; +system($IPTABLES_CMD_1); + +print "Adding forwarding rule: $IPTABLES_CMD_2\n"; +system($IPTABLES_CMD_2); + print "Done.\n";