From a875700b616a6e45d7f67dbee33244d358c2ce31 Mon Sep 17 00:00:00 2001 From: "ray.zh" Date: Wed, 27 Dec 2023 10:13:13 +0800 Subject: [PATCH] =?UTF-8?q?Create=202023-12-27-linux=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E8=BD=AC=E5=8F=91=E8=84=9A=E6=9C=AC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\217\221\350\204\232\346\234\254.md" | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 "_posts/2023-12-27-linux\347\253\257\345\217\243\350\275\254\345\217\221\350\204\232\346\234\254.md" diff --git "a/_posts/2023-12-27-linux\347\253\257\345\217\243\350\275\254\345\217\221\350\204\232\346\234\254.md" "b/_posts/2023-12-27-linux\347\253\257\345\217\243\350\275\254\345\217\221\350\204\232\346\234\254.md" new file mode 100644 index 0000000..fc1d20f --- /dev/null +++ "b/_posts/2023-12-27-linux\347\253\257\345\217\243\350\275\254\345\217\221\350\204\232\346\234\254.md" @@ -0,0 +1,110 @@ +--- +layout: post +title: "linux端口转发脚本" +date: 2023-12-27 10:29:20 +0800 +categories: + - 命令脚本 +tags: + - 端口转发 + - centos +--- + +一般来说,路由器都自带有端口转发功能,但部分路由器的配置条数会有限制(比如40条)。于是产生此脚本使用场景:将一批端口指到网关机器,然后使用iptables进行封包转发,一样实现了端口转发功能,而且没有条数限制! + +此脚本使用iptables进行tcp封包转发,适用于有公网IP而内网很多机器都需要使用公网端口的情况。 + +将源码保存为`iptab.sh`,修改gate地址即可食用。 + +```sh +#!/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 + +```