#!/bin/sh CHAIN=$CHAIN COMMENT=$COMMENT PROTOCOL=$TYPE ############################## echo 1 > /proc/sys/net/ipv4/ip_forward ############################## # if /sbin/iptables-legacy --list $CHAIN |grep ESTABLISHED |grep RELATED|grep ACCEPT ; then : ; else /sbin/iptables-legacy -I $CHAIN -m state --state established,related -j ACCEPT; fi ############################### COUNT_SOURCE_IP=$(set |grep SOURCE_IP |wc -l) COUNT_TARGET_IP=$(set |grep TARGET_IP |wc -l) COUNT_TARGET_PORT=$(set |grep TARGET_PORT |wc -l) for source_ip_index in $(seq 1 $COUNT_SOURCE_IP) ; do if set |grep SOURCE_IP_ ; then SOURCE_IP=$(eval "echo \${"SOURCE_IP_$source_ip_index"}") fi for target_ip_index in $(seq 1 $COUNT_TARGET_IP) ; do if set |grep TARGET_IP_ ; then TARGET_IP=$(eval "echo \${"TARGET_IP_$target_ip_index"}") fi for target_port_index in $(seq 1 $COUNT_TARGET_PORT) ; do if set |grep TARGET_PORT_ ; then TARGET_PORT=$(eval "echo \${"TARGET_PORT_$target_port_index"}") fi # DELETE UNECESSARY LINES FROM PREVIOUS RULES LINES=$(/sbin/iptables-legacy --line-number -n --list $CHAIN | grep $SOURCE_IP |grep $TARGET_IP |grep $PROTOCOL |grep $TARGET_PORT | awk '{print $1}'| tac) if [ -n "$LINES" ] ; then for i in $LINES; do /sbin/iptables-legacy -D $CHAIN $i sleep 0.1 done fi /sbin/iptables-legacy -I $CHAIN -s $SOURCE_IP -d $TARGET_IP -p $PROTOCOL --dport $TARGET_PORT -m comment --comment "$COMMENT" -j ACCEPT done done done