shell代码实现,统计所有iptables,ip6tables规则,按五链分别保存到对应.md文件
#!/bin/sh
generate_chain_diagram() {
local chain="$1"
local version="$2"
local command="$3"
echo "graph TD"
echo " A[$chain 链 $version] --> B[filter 表]"
echo " A --> C[nat 表]"
echo " A --> D[mangle 表]"
echo
for table in filter nat mangle; do
local table_prefix=$(echo $table | cut -c1 | tr '[:lower:]' '[:upper:]')
$command -t $table -L $chain -v -n 2>/dev/null | awk -v table="$table" -v prefix="$table_prefix" '
NR>2 {
rule = substr($0, 1, 50);
gsub(/"/, "\\\"", rule);
print " " prefix " --> " prefix "R" NR-3 "[" table ": " rule "...]"
}'
done
echo
}
for chain in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
for version in "IPv4" "IPv6"; do
if [ "$version" = "IPv4" ]; then
command="iptables"
else
command="ip6tables"
fi
filename="${chain}_${version}.md"
generate_chain_diagram "$chain" "$version" "$command" > "$filename"
echo "Generated $filename"
done
done
echo "all charts are created"