nuno 修订了这个 Gist 1 month ago. 转到此修订
1 file changed, 27 insertions
gistfile1.txt(文件已创建)
| @@ -0,0 +1,27 @@ | |||
| 1 | + | def 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 | |
上一页
下一页