shell代码,统计所有iptables,ip6tables规则,按五链分别保存到对应.md文件

1 篇文章 0 订阅

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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值