#!/bin/bash# A Shell Script for editing iptables# Written by Xiao Pan 2021# Clear iptables# Delete all User-defined roles,chains and zonesiptables-Fiptables-Xiptables-Z# Set default policyiptables-PINPUTDROPiptables-POUTPUTACCEPTiptables-PFORWARDACCEPT# Set Rulesiptables-AINPUT-ilo-jACCEPTiptables-AINPUT-iens33-mstate--stateRELATED,ESTABLISHED-jACCEPT# 我知道你们这些中文玩家大概率记不住这单词,如果忘掉请 "man iptables-extensions"# Uncommunt this if you want to block ping# iptables -A INPUT -i ens33 -p icmp --icmp-type 8 -j DROPiptables-AINPUT-iens33-picmp-jACCEPT# Add Accept rules# iptables -A INPUT -i ens33 -s 192.168.1.0/24 -j ACCEPT# Set NATEXTIF="ens192"INIFONE="ens224"INNETONE="192.168.100.0/24"INITTWO="ens256"INNETTWO="192.168.200.0/24"# Clear NAT table rulesiptables-F-tnatiptables-X-tnatiptables-Z-tnatiptables-tnat-PPREROUTINGACCEPTiptables-tnat-PPOSTROUTINGACCEPTiptables-tnat-POUTPUTACCEPT# Enable Source NATif ["$INIFONE"!="" ]; theniptables-tnat-APOSTROUTING-s $INNETONE -o $EXTIF -jMASQUERADEfi# Like this# 我知道这个你们大概率还是记不住,请 "man iptables-extensions" 后搜索 “SNAT” 全大写,或者搜索 “TARGET” 在 TARGET EXTENSIONS 中你们也会找得到的。# 注意:这个只适合用在动态 IP 地址的场合,其他情况依然推荐使用传统的 SNAT 即:iptables-tnat-APOSTROUTING-s $INNETONE -o $EXTIF -jSNAT--to-source123.186.228.222# Add DNAT Rules# Such as Web service on 192.168.100.10:80iptables-tnat-APREROUTING-ptcp-i $EXTIF --dport80-jDNAT--to-destination192.168.100.10:80# Like this# Save this, and run "bash ./firewall.sh"# or "chmod +x ./firewall.sh && ./firewall.sh# Run As ROOT!