#!/usr/bin/env python3
"""
Анализ подсети 74.208.53.0/24
"""
import json
from pathlib import Path

ANALYSIS_DIR = Path('/root/ir-assessment/redteam/irelydata/analysis')

# Читаем результаты nmap
hosts = []
with open('/tmp/irely_subnet.scan', 'r') as f:
    for line in f:
        if 'Status: Up' in line:
            # Формат: Host: 74.208.53.2 (hostname) Status: Up
            parts = line.split()
            if len(parts) >= 2:
                ip = parts[1]
                hostname = parts[2] if len(parts) > 2 and parts[2].startswith('(') else None
                if hostname and hostname.startswith('('):
                    hostname = hostname[1:-1]
                
                hosts.append({
                    'ip': ip,
                    'hostname': hostname
                })

# Фильтруем интересные хосты
interesting_keywords = ['irely', 'i21', 'jenkins', 'uap', 'sql']
interesting_hosts = [
    h for h in hosts 
    if h['hostname'] and any(kw in h['hostname'].lower() for kw in interesting_keywords)
]

results = {
    'total_hosts': len(hosts),
    'interesting_hosts': interesting_hosts,
    'all_hosts': hosts[:20]  # Первые 20 для примера
}

with open(ANALYSIS_DIR / 'subnet_analysis.json', 'w') as f:
    json.dump(results, f, indent=2)

print(f"Всего активных хостов: {len(hosts)}")
print(f"Интересных хостов: {len(interesting_hosts)}")

if interesting_hosts:
    print("\nИнтересные хосты:")
    for h in interesting_hosts:
        print(f"  {h['ip']} - {h['hostname']}")
else:
    print("\nИнтересные хосты не найдены (нет irely/i21/jenkins в именах)")
