106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
|
|
"""Database verkenning: PostgreSQL + Neo4j"""
|
||
|
|
import os, sys
|
||
|
|
os.environ["PG_HOST"] = "192.168.1.211"
|
||
|
|
os.environ["PG_PORT"] = "5433"
|
||
|
|
os.environ["PG_USER"] = "mo"
|
||
|
|
os.environ["PG_PASSWORD"] = "WaQTUw2t"
|
||
|
|
os.environ["PG_DATABASE"] = "homelab"
|
||
|
|
os.environ["NEO4J_URI"] = "neo4j://192.168.1.211:49153"
|
||
|
|
os.environ["NEO4J_USER"] = "neo4j"
|
||
|
|
os.environ["NEO4J_PASSWORD"] = "WaQTUw2t"
|
||
|
|
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
|
||
|
|
print("=" * 70)
|
||
|
|
print("POSTGRESQL VERKENNING")
|
||
|
|
print("=" * 70)
|
||
|
|
|
||
|
|
try:
|
||
|
|
from src.pg_client import query
|
||
|
|
|
||
|
|
# Alle schemas
|
||
|
|
print("\n--- Schemas ---")
|
||
|
|
schemas = query("SELECT schema_name FROM information_schema.schemata ORDER BY schema_name")
|
||
|
|
for s in schemas:
|
||
|
|
print(f" {s['schema_name']}")
|
||
|
|
|
||
|
|
# Alle tabellen
|
||
|
|
print("\n--- Tabellen ---")
|
||
|
|
tables = query("""
|
||
|
|
SELECT table_schema, table_name
|
||
|
|
FROM information_schema.tables
|
||
|
|
WHERE table_type = 'BASE TABLE'
|
||
|
|
ORDER BY table_schema, table_name
|
||
|
|
""")
|
||
|
|
for t in tables:
|
||
|
|
print(f" {t['table_schema']}.{t['table_name']}")
|
||
|
|
|
||
|
|
# Per tabel details + sample data
|
||
|
|
for t in tables:
|
||
|
|
schema = t['table_schema']
|
||
|
|
name = t['table_name']
|
||
|
|
full = f"{schema}.{name}"
|
||
|
|
|
||
|
|
cols = query("""
|
||
|
|
SELECT column_name, data_type
|
||
|
|
FROM information_schema.columns
|
||
|
|
WHERE table_schema = %s AND table_name = %s
|
||
|
|
ORDER BY ordinal_position
|
||
|
|
""", (schema, name))
|
||
|
|
|
||
|
|
try:
|
||
|
|
count = query(f"SELECT count(*) as cnt FROM {full}", fetch="one")
|
||
|
|
cnt = count['cnt']
|
||
|
|
except Exception as e:
|
||
|
|
cnt = f"ERR: {e}"
|
||
|
|
|
||
|
|
print(f"\n [{cnt} rows] {full}")
|
||
|
|
col_names = [c['column_name'] for c in cols]
|
||
|
|
print(f" Kolommen: {', '.join(col_names)}")
|
||
|
|
|
||
|
|
# Sample data (max 5 rijen)
|
||
|
|
if isinstance(cnt, int) and cnt > 0:
|
||
|
|
try:
|
||
|
|
rows = query(f"SELECT * FROM {full} LIMIT 5")
|
||
|
|
for i, row in enumerate(rows):
|
||
|
|
print(f" Row {i+1}: {dict(row)}")
|
||
|
|
except Exception as e:
|
||
|
|
print(f" Sample fout: {e}")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"PostgreSQL FOUT: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
|
||
|
|
print("\n" + "=" * 70)
|
||
|
|
print("NEO4J VERKENNING")
|
||
|
|
print("=" * 70)
|
||
|
|
|
||
|
|
try:
|
||
|
|
from src.neo4j_client import get_driver, get_all_devices, get_scan_history, get_network_summary, close
|
||
|
|
|
||
|
|
devices = get_all_devices()
|
||
|
|
print(f"\nDevices: {len(devices)}")
|
||
|
|
for d in devices:
|
||
|
|
ports = [p.get('port') for p in (d.get('ports') or []) if p and p.get('port')]
|
||
|
|
print(f" {d['ip']:<16} {d.get('hostname','')[:30]:<30} {d.get('os_guess','')[:25]:<25} ports={ports}")
|
||
|
|
|
||
|
|
print(f"\n--- Scan History ---")
|
||
|
|
scans = get_scan_history()
|
||
|
|
for s in scans:
|
||
|
|
print(f" {s}")
|
||
|
|
|
||
|
|
print(f"\n--- Summary ---")
|
||
|
|
summary = get_network_summary()
|
||
|
|
print(f" {summary}")
|
||
|
|
|
||
|
|
close()
|
||
|
|
print("\nNeo4j: OK")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Neo4j FOUT: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
|
||
|
|
print("\nDONE.")
|