.
This commit is contained in:
97
Dev/n8n/panda_n8n/n8n-scripts/get_sw_fw.py
Normal file
97
Dev/n8n/panda_n8n/n8n-scripts/get_sw_fw.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import re
|
||||
import json
|
||||
import sys
|
||||
from netmiko import ConnectHandler
|
||||
from netmiko.exceptions import NetmikoTimeoutException, NetmikoAuthenticationException
|
||||
|
||||
def get_aruba_clients(host, username, password):
|
||||
"""
|
||||
Connects to an Aruba switch via SSH, gets hostname and client info,
|
||||
and returns a list of dictionaries, each representing a client.
|
||||
"""
|
||||
device = {
|
||||
'device_type': 'aruba_aoscx',
|
||||
'host': host,
|
||||
'username': username,
|
||||
'password': password,
|
||||
}
|
||||
|
||||
all_clients_data = []
|
||||
print(f"Csatlakozás a(z) {host} eszközhöz...", file=sys.stderr)
|
||||
try:
|
||||
with ConnectHandler(**device) as net_connect:
|
||||
print("Sikeres csatlakozás. Adatok lekérdezése...", file=sys.stderr)
|
||||
|
||||
prompt = net_connect.find_prompt()
|
||||
switch_hostname = prompt.strip('#> ')
|
||||
|
||||
output = net_connect.send_command('show client ip')
|
||||
ip_pattern = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
|
||||
|
||||
if isinstance(output, str):
|
||||
lines = output.splitlines()
|
||||
for line in lines[2:]:
|
||||
columns = line.split()
|
||||
if len(columns) >= 4:
|
||||
potential_ip = columns[-1]
|
||||
if ip_pattern.match(potential_ip):
|
||||
client_data = {
|
||||
'switch_name': switch_hostname,
|
||||
'mac_address': columns[0],
|
||||
'interface': columns[1],
|
||||
'vlan': columns[2],
|
||||
'ip_address': potential_ip,
|
||||
}
|
||||
all_clients_data.append(client_data)
|
||||
return all_clients_data
|
||||
|
||||
print("Hiba: A parancs kimenete nem volt a várt formátumban.", file=sys.stderr)
|
||||
return None
|
||||
|
||||
except NetmikoTimeoutException:
|
||||
print(f"Hiba: Időtúllépés a(z) {host} eszközhöz való csatlakozáskor.", file=sys.stderr)
|
||||
return None
|
||||
except NetmikoAuthenticationException:
|
||||
print(f"Hiba: Sikertelen hitelesítés a(z) {host} eszközön. Ellenőrizd a jelszót.", file=sys.stderr)
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Váratlan hiba történt: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Konfigurációs fájl beolvasása
|
||||
try:
|
||||
with open('config.json', 'r') as f:
|
||||
config = json.load(f)
|
||||
except FileNotFoundError:
|
||||
print("Hiba: A 'config.json' fájl nem található.", file=sys.stderr)
|
||||
exit(1)
|
||||
except json.JSONDecodeError:
|
||||
print("Hiba: A 'config.json' fájl formátuma érvénytelen.", file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
SWITCH_IPS = config.get("switch_ips", [])
|
||||
USERNAME = config.get("username")
|
||||
PASSWORD = config.get("password")
|
||||
|
||||
if not all([SWITCH_IPS, USERNAME, PASSWORD]):
|
||||
print("Hiba: A 'config.json' fájl hiányos. Tartalmaznia kell 'switch_ips', 'username', és 'password' kulcsokat.", file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
all_switches_clients = []
|
||||
for ip in SWITCH_IPS:
|
||||
print(f"\n--- Feldolgozás: {ip} ---", file=sys.stderr)
|
||||
clients_from_one_switch = get_aruba_clients(ip, USERNAME, PASSWORD)
|
||||
if clients_from_one_switch:
|
||||
all_switches_clients.extend(clients_from_one_switch)
|
||||
print(f"Sikeresen lekérdezve {len(clients_from_one_switch)} kliens a(z) {ip} switch-ről.", file=sys.stderr)
|
||||
else:
|
||||
print(f"Nem sikerült adatot lekérdezni a(z) {ip} switch-ről.", file=sys.stderr)
|
||||
|
||||
# Ha nem sikerült adatot gyűjteni, jelezzük a stderr-en
|
||||
if not all_switches_clients:
|
||||
print("\nEgyetlen switch-ről sem sikerült adatot gyűjteni.", file=sys.stderr)
|
||||
|
||||
# --- Fő kimenet: JSON a stdout-ra ---
|
||||
print(json.dumps(all_switches_clients, indent=2))
|
||||
|
||||
Reference in New Issue
Block a user