最後活躍 1 month ago

修訂 9a7f7f60b36898abfa4f27a619f9f7ca9e931204

gistfile1.txt 原始檔案
1def audit_fw_counts(clients_dir):
2 """Return peer_name:fw_count pairs from iptables and client configs"""
3 import glob, subprocess
4
5 # Get iptables output once
6 try:
7 result = subprocess.run(
8 ['iptables', '-L', 'FORWARD', '-n'],
9 capture_output=True, text=True
10 )
11 fw_output = result.stdout
12 except:
13 fw_output = ""
14
15 # Build ip->name and count rules
16 for conf in glob.glob(f"{clients_dir}/*.conf"):
17 name = os.path.basename(conf).replace('.conf', '')
18 try:
19 with open(conf) as f:
20 for line in f:
21 if line.startswith('Address'):
22 ip = line.split('=')[1].strip().split('/')[0]
23 count = fw_output.count(ip)
24 print(f"{name}:{count}")
25 break
26 except:
27 pass