新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
分享一個好用的iptables一鍵配置端口轉(zhuǎn)發(fā)腳本,支持TCP和UDP
一鍵配置iptables端口轉(zhuǎn)發(fā)腳本

簡介
本腳本用于一鍵配置iptables的TCP和UDP端口轉(zhuǎn)發(fā),方便用戶快速設置端口映射。
使用方法
1、將以下腳本保存為port_forwarding.sh文件;
2、賦予腳本執(zhí)行權(quán)限:chmod +x port_forwarding.sh;
3、以root權(quán)限運行腳本:sudo ./port_forwarding.sh;
4、根據(jù)提示輸入相關信息,如源IP、目的IP、協(xié)議類型(TCP或UDP)以及端口號等;
5、腳本會自動配置iptables規(guī)則并重啟服務。
腳本內(nèi)容
#!/bin/bash
獲取用戶輸入信息
read p "請輸入源IP地址:" src_ip
read p "請輸入目的IP地址:" dst_ip
read p "請選擇協(xié)議類型(TCP/UDP):" protocol
read p "請輸入源端口號:" src_port
read p "請輸入目的端口號:" dst_port
檢查協(xié)議類型
if [ "$protocol" != "TCP" ] && [ "$protocol" != "UDP" ]; then
echo "協(xié)議類型錯誤,請輸入TCP或UDP"
exit 1
fi
清空已有規(guī)則
iptables t nat F PREROUTING
iptables t nat F POSTROUTING
iptables t nat F OUTPUT
添加端口轉(zhuǎn)發(fā)規(guī)則
iptables t nat A PREROUTING p $protocol dport $src_port j DNAT todestination $dst_ip:$dst_port
iptables t nat A POSTROUTING p $protocol j MASQUERADE
保存規(guī)則
iptablessave > /etc/iptables.rules
重啟iptables服務
service iptables restart
echo "端口轉(zhuǎn)發(fā)配置完成!"
相關問題與解答
Q1:如何刪除已配置的端口轉(zhuǎn)發(fā)規(guī)則?
A1:可以使用以下命令刪除已配置的端口轉(zhuǎn)發(fā)規(guī)則:
iptables t nat D PREROUTING p $protocol dport $src_port j DNAT todestination $dst_ip:$dst_port iptables t nat D POSTROUTING p $protocol j MASQUERADE iptablessave > /etc/iptables.rules service iptables restart
Q2:如何查看當前iptables規(guī)則?
A2:可以使用以下命令查看當前iptables規(guī)則:
iptables L n v
網(wǎng)站題目:分享一個好用的iptables一鍵配置端口轉(zhuǎn)發(fā)腳本,支持TCP和UDP
網(wǎng)站網(wǎng)址:http://www.dlmjj.cn/article/djgccdc.html


咨詢
建站咨詢
