文章目录


#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATHGreen_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
Tip="${Green_font_prefix}[注意]${Font_color_suffix}"check_iptables(){iptables_exist=$(iptables -V)[[ ${iptables_exist} = "" ]] && echo -e "${Error} 没有安装iptables,请检查 !" && exit 1
}
check_sys(){if [[ -f /etc/redhat-release ]]; thenrelease="centos"elif cat /etc/issue | grep -q -E -i "debian"; thenrelease="debian"elif cat /etc/issue | grep -q -E -i "ubuntu"; thenrelease="ubuntu"elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; thenrelease="centos"elif cat /proc/version | grep -q -E -i "debian"; thenrelease="debian"elif cat /proc/version | grep -q -E -i "ubuntu"; thenrelease="ubuntu"elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; thenrelease="centos"fi#bit=`uname -m`
}
install_iptables(){iptables_exist=$(iptables -V)if [[ ${iptables_exist} != "" ]]; thenecho -e "${Info} 已经安装iptables,继续..."elseecho -e "${Info} 检测到未安装 iptables,开始安装..."if [[ ${release}  == "centos" ]]; thenyum updateyum install -y iptableselseapt-get updateapt-get install -y iptablesfiiptables_exist=$(iptables -V)if [[ ${iptables_exist} = "" ]]; thenecho -e "${Error} 安装iptables失败,请检查 !" && exit 1elseecho -e "${Info} iptables 安装完成 !"fifiecho -e "${Info} 开始配置 iptables !"Set_iptablesecho -e "${Info} iptables 配置完毕 !"
}
Set_forwarding_port(){read -e -p "请输入 iptables 欲转发至的 远程端口 [1-65535] (支持端口段 如 2333-6666, 被转发服务器):" forwarding_port[[ -z "${forwarding_port}" ]] && echo "取消..." && exit 1echo && echo -e "    欲转发端口 : ${Red_font_prefix}${forwarding_port}${Font_color_suffix}" && echo
}
Set_forwarding_ip(){read -e -p "请输入 iptables 欲转发至的 远程IP(被转发服务器):" forwarding_ip[[ -z "${forwarding_ip}" ]] && echo "取消..." && exit 1echo && echo -e "    欲转发服务器IP : ${Red_font_prefix}${forwarding_ip}${Font_color_suffix}" && echo
}
Set_local_port(){echo -e "请输入 iptables 本地监听端口 [1-65535] (支持端口段 如 2333-6666)"read -e -p "(默认端口: ${forwarding_port}):" local_port[[ -z "${local_port}" ]] && local_port="${forwarding_port}"echo && echo -e "   本地监听端口 : ${Red_font_prefix}${local_port}${Font_color_suffix}" && echo
}
Set_local_ip(){read -e -p "请输入 本服务器的 网卡IP(注意是网卡绑定的IP,而不仅仅是公网IP,回车自动检测外网IP):" local_ipif [[ -z "${local_ip}" ]]; thenlocal_ip=$(wget -qO- -t1 -T2 ipinfo.io/ip)if [[ -z "${local_ip}" ]]; thenecho "${Error} 无法检测到本服务器的公网IP,请手动输入"read -e -p "请输入 本服务器的 网卡IP(注意是网卡绑定的IP,而不仅仅是公网IP):" local_ip[[ -z "${local_ip}" ]] && echo "取消..." && exit 1fifiecho && echo -e "   本服务器IP : ${Red_font_prefix}${local_ip}${Font_color_suffix}" && echo
}
Set_forwarding_type(){echo -e "请输入数字 来选择 iptables 转发类型:1. TCP2. UDP3. TCP+UDP\n"read -e -p "(默认: TCP+UDP):" forwarding_type_num[[ -z "${forwarding_type_num}" ]] && forwarding_type_num="3"if [[ ${forwarding_type_num} == "1" ]]; thenforwarding_type="TCP"elif [[ ${forwarding_type_num} == "2" ]]; thenforwarding_type="UDP"elif [[ ${forwarding_type_num} == "3" ]]; thenforwarding_type="TCP+UDP"elseforwarding_type="TCP+UDP"fi
}
Set_Config(){Set_forwarding_portSet_forwarding_ipSet_local_portSet_local_ipSet_forwarding_typeecho && echo -e "——————————————————————————————请检查 iptables 端口转发规则配置是否有误 !\n本地监听端口    : ${Green_font_prefix}${local_port}${Font_color_suffix}服务器 IP\t: ${Green_font_prefix}${local_ip}${Font_color_suffix}\n欲转发的端口    : ${Green_font_prefix}${forwarding_port}${Font_color_suffix}欲转发 IP\t: ${Green_font_prefix}${forwarding_ip}${Font_color_suffix}转发类型\t: ${Green_font_prefix}${forwarding_type}${Font_color_suffix}
——————————————————————————————\n"read -e -p "请按任意键继续,如有配置错误请使用 Ctrl+C 退出。" var
}
Add_forwarding(){check_iptablesSet_Configlocal_port=$(echo ${local_port} | sed 's/-/:/g')forwarding_port_1=$(echo ${forwarding_port} | sed 's/-/:/g')if [[ ${forwarding_type} == "TCP" ]]; thenAdd_iptables "tcp"elif [[ ${forwarding_type} == "UDP" ]]; thenAdd_iptables "udp"elif [[ ${forwarding_type} == "TCP+UDP" ]]; thenAdd_iptables "tcp"Add_iptables "udp"fiSave_iptablesclear && echo && echo -e "——————————————————————————————iptables 端口转发规则配置完成 !\n本地监听端口    : ${Green_font_prefix}${local_port}${Font_color_suffix}服务器 IP\t: ${Green_font_prefix}${local_ip}${Font_color_suffix}\n欲转发的端口    : ${Green_font_prefix}${forwarding_port_1}${Font_color_suffix}欲转发 IP\t: ${Green_font_prefix}${forwarding_ip}${Font_color_suffix}转发类型\t: ${Green_font_prefix}${forwarding_type}${Font_color_suffix}
——————————————————————————————\n"
}
View_forwarding(){check_iptablesforwarding_text=$(iptables -t nat -vnL PREROUTING|tail -n +3)[[ -z ${forwarding_text} ]] && echo -e "${Error} 没有发现 iptables 端口转发规则,请检查 !" && exit 1forwarding_total=$(echo -e "${forwarding_text}"|wc -l)forwarding_list_all=""for((integer = 1; integer <= ${forwarding_total}; integer++))doforwarding_type=$(echo -e "${forwarding_text}"|awk '{print $4}'|sed -n "${integer}p")forwarding_listen=$(echo -e "${forwarding_text}"|awk '{print $11}'|sed -n "${integer}p"|awk -F "dpt:" '{print $2}')[[ -z ${forwarding_listen} ]] && forwarding_listen=$(echo -e "${forwarding_text}"| awk '{print $11}'|sed -n "${integer}p"|awk -F "dpts:" '{print $2}')forwarding_fork=$(echo -e "${forwarding_text}"| awk '{print $12}'|sed -n "${integer}p"|awk -F "to:" '{print $2}')forwarding_list_all=${forwarding_list_all}"${Green_font_prefix}"${integer}".${Font_color_suffix} 类型: ${Green_font_prefix}"${forwarding_type}"${Font_color_suffix} 监听端口: ${Red_font_prefix}"${forwarding_listen}"${Font_color_suffix} 转发IP和端口: ${Red_font_prefix}"${forwarding_fork}"${Font_color_suffix}\n"doneecho && echo -e "当前有 ${Green_background_prefix} "${forwarding_total}" ${Font_color_suffix} 个 iptables 端口转发规则。"echo -e ${forwarding_list_all}
}
Del_forwarding(){check_iptableswhile truedoView_forwardingread -e -p "请输入数字 来选择要删除的 iptables 端口转发规则(默认回车取消):" Del_forwarding_num[[ -z "${Del_forwarding_num}" ]] && Del_forwarding_num="0"echo $((${Del_forwarding_num}+0)) &>/dev/nullif [[ $? -eq 0 ]]; thenif [[ ${Del_forwarding_num} -ge 1 ]] && [[ ${Del_forwarding_num} -le ${forwarding_total} ]]; thenforwarding_type=$(echo -e "${forwarding_text}"| awk '{print $4}' | sed -n "${Del_forwarding_num}p")forwarding_listen=$(echo -e "${forwarding_text}"| awk '{print $11}' | sed -n "${Del_forwarding_num}p" | awk -F "dpt:" '{print $2}' | sed 's/-/:/g')[[ -z ${forwarding_listen} ]] && forwarding_listen=$(echo -e "${forwarding_text}"| awk '{print $11}' |sed -n "${Del_forwarding_num}p" | awk -F "dpts:" '{print $2}')Del_iptables "${forwarding_type}" "${Del_forwarding_num}"Save_iptablesecho && echo -e "${Info} iptables 端口转发规则删除完成 !" && echoelseecho -e "${Error} 请输入正确的数字 !"fielsebreak && echo "取消..."fidone
}
Uninstall_forwarding(){check_iptablesecho -e "确定要清空 iptables 所有端口转发规则 ? [y/N]"read -e -p "(默认: n):" unyn[[ -z ${unyn} ]] && unyn="n"if [[ ${unyn} == [Yy] ]]; thenforwarding_text=$(iptables -t nat -vnL PREROUTING|tail -n +3)[[ -z ${forwarding_text} ]] && echo -e "${Error} 没有发现 iptables 端口转发规则,请检查 !" && exit 1forwarding_total=$(echo -e "${forwarding_text}"|wc -l)for((integer = 1; integer <= ${forwarding_total}; integer++))doforwarding_type=$(echo -e "${forwarding_text}"|awk '{print $4}'|sed -n "${integer}p")forwarding_listen=$(echo -e "${forwarding_text}"|awk '{print $11}'|sed -n "${integer}p"|awk -F "dpt:" '{print $2}')[[ -z ${forwarding_listen} ]] && forwarding_listen=$(echo -e "${forwarding_text}"| awk '{print $11}'|sed -n "${integer}p"|awk -F "dpts:" '{print $2}')# echo -e "${forwarding_text} ${forwarding_type} ${forwarding_listen}"Del_iptables "${forwarding_type}" "${integer}"doneSave_iptablesecho && echo -e "${Info} iptables 已清空 所有端口转发规则 !" && echoelseecho && echo "清空已取消..." && echofi
}
Add_iptables(){iptables -t nat -A PREROUTING -p "$1" --dport "${local_port}" -j DNAT --to-destination "${forwarding_ip}":"${forwarding_port}"iptables -t nat -A POSTROUTING -p "$1" -d "${forwarding_ip}" --dport "${forwarding_port_1}" -j SNAT --to-source "${local_ip}"echo "iptables -t nat -A PREROUTING -p $1 --dport ${local_port} -j DNAT --to-destination ${forwarding_ip}:${forwarding_port}"echo "iptables -t nat -A POSTROUTING -p $1 -d ${forwarding_ip} --dport ${forwarding_port_1} -j SNAT --to-source ${local_ip}"echo "${local_port}"iptables -I INPUT -m state --state NEW -m "$1" -p "$1" --dport "${local_port}" -j ACCEPT
}
Del_iptables(){iptables -t nat -D POSTROUTING "$2"iptables -t nat -D PREROUTING "$2"iptables -D INPUT -m state --state NEW -m "$1" -p "$1" --dport "${forwarding_listen}" -j ACCEPT
}
Save_iptables(){if [[ ${release} == "centos" ]]; thenservice iptables saveelseiptables-save > /etc/iptables.up.rulesfi
}
Set_iptables(){echo -e "net.ipv4.ip_forward=1" >> /etc/sysctl.confsysctl -pif [[ ${release} == "centos" ]]; thenservice iptables savechkconfig --level 2345 iptables onelseiptables-save > /etc/iptables.up.rulesecho -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules' > /etc/network/if-pre-up.d/iptableschmod +x /etc/network/if-pre-up.d/iptablesfi
}
check_sys
echo && echo -e " iptables 端口转发一键管理脚本 ${Red_font_prefix}${Font_color_suffix}${Green_font_prefix}1.${Font_color_suffix} 安装 iptables${Green_font_prefix}2.${Font_color_suffix} 清空 iptables 端口转发
————————————${Green_font_prefix}3.${Font_color_suffix} 查看 iptables 端口转发${Green_font_prefix}4.${Font_color_suffix} 添加 iptables 端口转发${Green_font_prefix}5.${Font_color_suffix} 删除 iptables 端口转发
————————————
注意:初次使用前请请务必执行 ${Green_font_prefix}1. 安装 iptables${Font_color_suffix}(不仅仅是安装)" && echo
read -e -p " 请输入数字 [1-5]:" num
case "$num" in1)install_iptables;;2)Uninstall_forwarding;;3)View_forwarding;;4)Add_forwarding;;5)Del_forwarding;;*)echo "请输入正确数字 [1-5]";;
esac

iptables 一键设置/清除脚本相关推荐

  1. 通用文件清除脚本,可对多个指定目录按照磁盘空间、文件保存天数进行清理,并可设置目录扫描深度

    common_clear.sh #!/bin/bash######################################################################## ...

  2. Linux开启Docker远程访问并设置安全访问(证书密钥),附一份小白一键设置脚本哦!

    前言 喜欢折腾慢慢看,不喜欢折腾直接跳到小简下文的一键脚本那里,两分钟搞好. 我的博客:https://blog.ideaopen.cn 我的公众号:小简聊开发 开启远程访问 编辑docker.ser ...

  3. Linux iptables防火墙设置与NAT服务配置

    Linux iptables防火墙设置与NAT服务配置 - 摘要: linux教程,NAT服务器,iptables防火墙设置与NAT服务配置, 防火墙是指设置在不同网络或网络安全域之间的一系列部件的组 ...

  4. Centos Denyhosts 一键安装配置脚本

    Centos Denyhosts 一键安装配置脚本 一键安装denyhosts脚本并配置为常用配置.放置Linux服务器被暴力破解 由于不能上传tar文件.所以改为zip压缩. 将附件的zip压缩包解 ...

  5. Mongodb定时备份脚本和清除脚本

    Mongodb用的是可以热备份的mongodump和对应恢复的mongorestore,在linux下面使用shell脚本写的定时备份,代码如下 1.定时备份 #!/bin/bash sourcepa ...

  6. 4步搞定MySQL安装部署(附MySQL一键式部署脚本)

    墨墨导读:良好的开端是成功的一半,从MySQL安装开始. 学习数据库技术,实际动手的第一步是安装自己的MySQL.MySQL方面也提供多样式的安装方式rpm ,tar ,源码包.当安装完投入使用之后, ...

  7. linux卸载zabbix数据库,结合宝塔linux面板 centos6 7一键zabbix安装脚本!

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.铁网维就不做过多的介绍,详情可以百度了解! 本次铁网维结合宝塔linux面板的环境来安装zabbix3.0. ...

  8. Linux学习之一键建站脚本

    本文转自lnmp.lamp.lnmpa一键安装包和LAMP一键安装脚本使用说明 这个脚本是使用shell编写,为了快速在生产环境上部署lnmp/lamp/lnmpa(Linux.Nginx/Tengi ...

  9. MS PPT一键设置全部文本框字体和大小(VBA)

    适用于Microsoft PPT 2021版本. 作用:将PPT全部文本框进行字体和字体大小设置. 弊端:执行后会使全部字体被改变,建议在写完ppt后,先执行该脚本,再把局部字体手动调整. 配置参数修 ...

最新文章

  1. 秋色园QBlog技术原理解析:性能优化篇:数据库文章表分表及分库减压方案(十五)...
  2. C语言声明数组变量时,在什么情况下,可不指定数组大小
  3. java 标记_java的标记算法
  4. 【代码实现接口测试】Requests库
  5. 7.3图的遍历(深度优先)-理论
  6. SCONS如何集成工具
  7. 用Mediator Pattern + Queue 解决 订单处理流程
  8. 日常提醒2 (delphi源码)
  9. 利用Python进行股票交易分析(一):量化交易策略——买点与卖点的量化
  10. Java8中关于LocalDateTime转换方法总结
  11. 计算机考试当场出分,基金从业资格考试当场出成绩吗?
  12. acm快速输入法 有效解决超时
  13. item_search - 根据关键词取拼多多商品列表
  14. 图像操作(反差,混合,调整图像亮度与对比度,绘制文字)
  15. Spring 读取properties文件key+value方式
  16. sql 统计各班不同分数段的人数(案例v1)
  17. oracle 按天数 均值,oracle 按天数统计数据
  18. linuxci‘pang磁盘管理的实验报告
  19. Linux从入门到入狱-01
  20. 专著《Python与开源GIS:数据处理、空间分析》

热门文章

  1. 开关电源环路稳定性分析(09)——环路补偿六步法
  2. 网络开发——Unity中的消息分发器
  3. springcloud微服务国际化
  4. SAP CEWB 批量修改 工艺路线 和 BOM
  5. 佛祖保佑,永无bug——springboot项目启动图案的修改方法
  6. Android打开第三方应用方法总结
  7. LaTeX之tcolorbox宏包应用示例
  8. python机器视觉车牌识别_2车牌识别与深度学习
  9. Android英语词典案例
  10. java金蝶云单据查询_凭证查询