Skip to content

Commit

Permalink
Create 2023-12-27-linux端口转发脚本.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterChangRay authored Dec 27, 2023
1 parent 20ba1ad commit a875700
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions _posts/2023-12-27-linux端口转发脚本.md
Original file line number Diff line number Diff line change
@@ -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

```

0 comments on commit a875700

Please sign in to comment.