gistfile1.txt
· 901 B · Text
Неформатований
def audit_fw_counts(clients_dir):
"""Return peer_name:fw_count pairs from iptables and client configs"""
import glob, subprocess
# Get iptables output once
try:
result = subprocess.run(
['iptables', '-L', 'FORWARD', '-n'],
capture_output=True, text=True
)
fw_output = result.stdout
except:
fw_output = ""
# Build ip->name and count rules
for conf in glob.glob(f"{clients_dir}/*.conf"):
name = os.path.basename(conf).replace('.conf', '')
try:
with open(conf) as f:
for line in f:
if line.startswith('Address'):
ip = line.split('=')[1].strip().split('/')[0]
count = fw_output.count(ip)
print(f"{name}:{count}")
break
except:
pass
| 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 |