Chinaunix首页 | 论坛 | 博客
  • 博客访问: 282452
  • 博文数量: 176
  • 博客积分: 2516
  • 博客等级: 少校
  • 技术积分: 1350
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-01 11:18
文章分类
文章存档

2011年(1)

2010年(18)

2009年(157)

我的朋友

分类: LINUX

2009-07-05 20:41:17

iptable 应用脚本
这个脚本是我在某某公司工作2个月以后,偶然间误删了前一位工程师的在公司网关服务器上的防火墙设置,所以迫不得以,在网上恶补了一下防火墙,然后找了个脚本的模版,结合公司内的需求,编写了个防火墙的脚本.
要求:
1.通过IP转发,允许公司内所有服务器通过网关服务器访问外网
2.允许公司外网访问公司内网关服务器Eth0的922端口
3.允许公司内网访问外网的FTP
4.允许对外进行DNS查询
5.允许公司内服务器访问网关服务器的631(打印机)端口
6.在该服务器上设置NAT地址影射.影射地址:
211.160.8.116--192.168.1.113
211.160.8.115--192.168.1.63
 
[root@china tools]# cat iprules.sh
#!/bin/bash
# This is a script
# Edit by liwei
# establish a static firewall
# define const here
Open_ports="25 110 922" # 自己机器对外开放的端口
Allow_ports="53 21 22" # internet的数据可以进入自己机器的端口
#init
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -P INPUT DROP #we can use another method to instead it
#iptables -P INPUT -i eth0 -j DROP
#iptables -P INPUT -i ppp0 -j DROP
#iptables -A INPUT -i ! ppp0 -j ACCEPT
iptables -A INPUT -i eth1  -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
# define ruler so that some data can come in.
echo "Allow_ports:"
for Port in $Allow_ports ; do
echo $Port
iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i eth0 -p udp --sport $Port -j ACCEPT
done
echo "Open_ports:"
for Port in $Open_ports ; do
echo $Port
iptables -A INPUT -i ppp0 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport $Port -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i eth0 -p udp --dport $Port -j ACCEPT
done
#To add printer access for 192.168.1.0/24 clients
iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --dport 631 -j ACCEPT
#iptables -A INPUT -i eth0 -s 192.168.1.1 -p tcp --dport 631 -j ACCEPT
iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --dport 139 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
#Add default route comes from 211.160.8.114
#fw from internet to 192.168.1.113 though by 211.160.8.116
iptables -t nat -A PREROUTING -d 211.160.8.116 -p tcp  -j DNAT --to 192.168.1.113
#fw from internet to 192.168.1.63 though by 211.160.8.115
iptables -t nat -A PREROUTING -d 211.160.8.115 -p tcp --dport 22 -j DNAT --to 192.168.1.63
 
 
### 以下是供TC脚本使用的预参数设置
#------Upload Limitation----
IPS=50
IPE=245
INET=192.168.1.
COUNTER=$IPS
while [ $COUNTER -lt $IPE ]
do
    iptables -t mangle -A PREROUTING -i eth1 -s $INET$COUNTER  -j MARK --set-mark $COUNTER
    COUNTER=`expr $COUNTER + 1`
done
阅读(705) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~