portforward.sh 练习一下

1317阅读 0评论2006-01-15 ruiqingzheng
分类:LINUX

#!/bin/bash

if [ ! $# -eq 2 ];then
        echo "Usage:`basename $0` IP PORT" && exit 1
else
        echo $1 | grep -oq "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$"
        if [ ! $? -eq 0 ]; then
                echo "Illegal IP $1"
                exit 2
        else
                echo "Correct  IP format $1"
        fi

        echo $2 | grep -oq "[0-9]\{1,5\}$"
        if [ ! $? -eq 0 ]; then
                echo "Illegal PORT $2"
                exit 3
        else
                echo "Correct PORT format $2"
        fi
fi
IP=$1
PORT=$2

# set all rules default if you want
read -p "Do you want to restor the firewall first?"   -t 10 an
[ $an = "y" -o $an = "yes" ] && echo "Reatoring..." && iptables-restore < /etc/rc.d/firewall.rule

# set portforward rules
read -p "${IP}:${PORT} Is it right ? [y/n]"  -t 10 ans
ans=${ans:-"n"}
case "$ans" in
        y|Y|yes|YES)
        iptables -t nat -I PREROUTING -d 219.140.A.B -p tcp \
                 --dport $PORT -j DNAT --to $IP
        iptables -t nat -I POSTROUTING -d $IP -p tcp \
                 --dport $PORT -j SNAT --to 192.168.0.1
        echo "Have done , please check your iptables"
        ;;
        n|N|no|NO)
        echo "You canceled . Exit now"
        exit
        ;;
        *)
        echo "Only can enter y or n!"
        exit
esac

exit 0

上一篇:script : check_conn.sh
下一篇:FC1里面使用VNC