Post

linux端口转发脚本

一般来说,路由器都自带有端口转发功能,但部分路由器的配置条数会有限制(比如40条)。于是产生此脚本使用场景:将一批端口指到网关机器,然后使用iptables进行封包转发,一样实现了端口转发功能,而且没有条数限制!

此脚本使用iptables进行tcp封包转发,适用于有公网IP而内网很多机器都需要使用公网端口的情况。

将源码保存为iptab.sh,修改gate地址即可食用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash

gate=192.168.0.179

if [ "$1" = "" ]||[ "$1" = "-h" ];then

	echo -e "\n本脚本使用选项和参数如下:(用户请使用外网端口20000-20100)\
	\n  查看映射请使用选项“-chk”,参数可指定:外网端口(不指定参数会查询所有映射)\
	\n  添加映射请使用选项“-add”,参数需指定:外网端口、目标内网IP、目标内网端口、映射描述(以空格分隔)\
	\n  删除映射请使用选项“-del”,参数需指定:外网端口 \n注意:描述不要以“-”等特殊符号开始"


elif [ "$1" = -chk ];then
	
	if [ "$2" != ""  ];then
		
		numchk=`iptables -t nat -nL --line-number|grep DNAT|awk '{print $8}'|grep -w "$2"|wc -l`

		if [ "$numchk" -ne 0 ];then
			iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|awk '{for (i=7;i<=NF;i++)printf("%s",$i);print ""}'|awk -F "*" '{print $1,$3,"--"$2}'
		else
			echo -e "\n外网端口 $2 未使用"
		fi
	else
		echo -e "\n已经使用的外网端口\"dpt\" 和对应目标信息\"to\" 如下:"
        	iptables -t nat -nL --line-number|grep DNAT|awk '{for (i=7;i<=NF;i++)printf("%s",$i);print ""}'|awk -F "*" '{print $1,$3,"--"$2}'
	fi

elif [ "$1" = -add ];then
	if [ $2 -lt 20000 ] || [ $2 -gt 20120 ];then
		echo "外网端口超出范围!"
		exit
	fi
	chkdnat=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|grep -w "$3"|grep -w "$4"|wc -l`
	usedport=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|wc -l`
	
	if [ "$usedport" -eq 0 ];then
		if [ "$5" != "" ];then


			iptables -t nat -A PREROUTING -d "$gate" -p tcp --dport "$2" -j DNAT --to-destination "$3":"$4" -m comment --comment "$5" 2> /dev/null
			iptables -t nat -A POSTROUTING --dst "$3" -p tcp --dport "$4" -j SNAT --to-source "$gate" 2> /dev/null
			if [ "$?" = 0 ];then
				
				iptables-save > /etc/sysconfig/iptables
				disdnat=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|grep -w "$3"|grep -w "$4"|awk '{for (i=7;i<=NF;i++)printf("%s",$i);print ""}'|awk -F "*" '{print $1,$3,"--"$2}'`
				echo -e "\n映射 $disdnat 添加成功!"
			else
				echo -e "\n映射添加失败!"
			fi
		else
			echo "映射添加失败,请添加描述信息!"
		fi
			
	elif [ "$chkdnat" -ne 0 ];then
		echo -e "\n此映射已存在!请检查!"
		
	else
		echo -e "\n此外网端口已被使用!添加失败!"
	fi


elif [ "$1" = -del ];then

	chkdnat=`iptables -t nat -nL --line-number|grep DNAT|awk '{print $8}'|grep -w "$2"|wc -l`
	if [ "$chkdnat" -eq 1 ];then

		inip=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|awk '{print $NF}'|awk -F":" '{print $2}'`
		inport=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|awk '{print $NF}'|awk -F":" '{print $3}'`
		numdnat=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|grep -w "$inip":"$inport"|awk '{print $1}'`
		disdnat=`iptables -t nat -nL --line-number|grep DNAT|grep -w "$2"|grep -w "$inip":"$inport"|awk '{for (i=7;i<=NF;i++)printf("%s",$i);print ""}'|awk -F "*" '{print $1,$3,"--"$2}'`

		iptables -t nat -D PREROUTING "$numdnat"
			
		delsnat=`iptables -t nat -nL --line-number|grep SNAT|awk '{print $1,$6,$8}'|grep -w "$inip"|grep -w "$inport"| wc -l`
                if [ "$delsnat" -eq 1 ];then
			numsnat=`iptables -t nat -nL --line-number|grep SNAT|awk '{print $1,$6,$8}'|grep -w "$inip"|grep -w "$inport"|awk '{print $1}'`
                        iptables -t nat -D POSTROUTING "$numsnat"
		fi
		iptables-save > /etc/sysconfig/iptables
		echo -e "\n映射 $disdnat 已删除!"
		
	else
		echo -e "\n删除映射不存在!"
	fi

else
	echo "参数不正确!请使用 -h 参数查看"

fi

This post is licensed under CC BY 4.0 by the author.