Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| scripts [2025/12/04 14:21] – [WINDOWS] huracan | scripts [2025/12/04 14:31] (Version actuelle) – [PYTHON] huracan | ||
|---|---|---|---|
| Ligne 7: | Ligne 7: | ||
| - | ---- | + | |
| Ligne 18: | Ligne 18: | ||
| Script pour commandes multithread sur plusieurs switchs | Script pour commandes multithread sur plusieurs switchs | ||
| - | # | + | # |
| - | from concurrent.futures import ThreadPoolExecutor, | + | from concurrent.futures import ThreadPoolExecutor, |
| - | from datetime import datetime | + | from datetime import datetime |
| - | from getpass import getpass | + | from getpass import getpass |
| - | import threading | + | import threading |
| - | from pathlib import Path | + | from pathlib import Path |
| - | import os | + | import os |
| - | import socket | + | import socket |
| - | from netmiko import ConnectHandler | + | from netmiko import ConnectHandler |
| - | + | ||
| - | SWITCH_FILE = " | + | SWITCH_FILE = " |
| - | MAX_WORKERS = 20 | + | MAX_WORKERS = 20 |
| - | + | ||
| - | print_lock = threading.Lock() | + | print_lock = threading.Lock() |
| - | + | ||
| - | + | ||
| - | def safe_print(*a, | + | def safe_print(*a, |
| - | with print_lock: | + | with print_lock: |
| - | print(*a, **kw, flush=True) | + | print(*a, **kw, flush=True) |
| - | + | ||
| - | + | ||
| - | def load_hosts(path): | + | def load_hosts(path): |
| - | p = Path(path) | + | p = Path(path) |
| - | if not p.exists(): | + | if not p.exists(): |
| - | return [] | + | return [] |
| - | return [x.strip() for x in p.read_text().splitlines() if x.strip()] | + | return [x.strip() for x in p.read_text().splitlines() if x.strip()] |
| - | + | ||
| - | + | ||
| - | # --------- AJOUT : TEST SSH AVANT CONNEXION ----------- | + | # --------- AJOUT : TEST SSH AVANT CONNEXION ----------- |
| - | def test_ssh_port(host, | + | def test_ssh_port(host, |
| - | try: | + | try: |
| - | with socket.create_connection((host, | + | with socket.create_connection((host, |
| - | return True | + | return True |
| - | except: | + | except: |
| - | return False | + | return False |
| - | # ------------------------------------------------------- | + | # ------------------------------------------------------- |
| - | + | ||
| - | + | ||
| - | def run_on_switch(host, | + | def run_on_switch(host, |
| - | result = { | + | result = { |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | } | + | } |
| - | + | ||
| - | # --- Test SSH avant toute connexion --- | + | # --- Test SSH avant toute connexion --- |
| - | if not test_ssh_port(host): | + | if not test_ssh_port(host): |
| - | result[" | + | result[" |
| - | result[" | + | result[" |
| - | return result | + | return result |
| - | + | ||
| - | try: | + | try: |
| - | device = { | + | device = { |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | } | + | } |
| - | + | ||
| - | conn = ConnectHandler(**device) | + | conn = ConnectHandler(**device) |
| - | + | ||
| - | except Exception as e: | + | except Exception as e: |
| - | result[" | + | result[" |
| - | result[" | + | result[" |
| - | return result | + | return result |
| - | + | ||
| - | try: | + | try: |
| - | conn.send_command(" | + | conn.send_command(" |
| - | + | ||
| - | for cmd in commands: | + | for cmd in commands: |
| - | out = conn.send_command(cmd, | + | out = conn.send_command(cmd, |
| - | for line in out.splitlines(): | + | for line in out.splitlines(): |
| - | safe_print(f" | + | safe_print(f" |
| - | result[" | + | result[" |
| - | + | ||
| - | result[" | + | result[" |
| - | + | ||
| - | except Exception as e: | + | except Exception as e: |
| - | result[" | + | result[" |
| - | + | ||
| - | finally: | + | finally: |
| - | conn.disconnect() | + | conn.disconnect() |
| - | result[" | + | result[" |
| - | + | ||
| - | return result | + | return result |
| - | + | ||
| - | + | ||
| - | def choose_switches(hosts): | + | def choose_switches(hosts): |
| - | while True: | + | while True: |
| - | safe_print(" | + | safe_print(" |
| - | for i, h in enumerate(hosts, | + | for i, h in enumerate(hosts, |
| - | safe_print(f" | + | safe_print(f" |
| - | + | ||
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | + | ||
| - | choice = input(" | + | choice = input(" |
| - | + | ||
| - | if choice == " | + | if choice == " |
| - | return None | + | return None |
| - | + | ||
| - | if choice == " | + | if choice == " |
| - | return hosts | + | return hosts |
| - | + | ||
| - | try: | + | try: |
| - | indices = [int(x.strip()) for x in choice.split("," | + | indices = [int(x.strip()) for x in choice.split("," |
| - | selected = [hosts[i - 1] for i in indices if 1 <= i <= len(hosts)] | + | selected = [hosts[i - 1] for i in indices if 1 <= i <= len(hosts)] |
| - | if selected: | + | if selected: |
| - | return selected | + | return selected |
| - | except: | + | except: |
| - | pass | + | pass |
| - | + | ||
| - | safe_print(" | + | safe_print(" |
| - | + | ||
| - | + | ||
| - | def main(): | + | def main(): |
| - | hosts = load_hosts(SWITCH_FILE) | + | hosts = load_hosts(SWITCH_FILE) |
| - | if not hosts: | + | if not hosts: |
| - | safe_print(f" | + | safe_print(f" |
| - | return | + | return |
| - | + | ||
| - | safe_print(f" | + | safe_print(f" |
| - | + | ||
| - | username = input(" | + | username = input(" |
| - | password = getpass(" | + | password = getpass(" |
| - | + | ||
| - | current_selection = hosts # par défaut : tous | + | current_selection = hosts # par défaut : tous |
| - | + | ||
| - | while True: | + | while True: |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | for h in current_selection: | + | for h in current_selection: |
| - | safe_print(f" | + | safe_print(f" |
| - | + | ||
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | safe_print(" | + | safe_print(" |
| - | + | ||
| - | choice = input("> | + | choice = input("> |
| - | + | ||
| - | if choice == " | + | if choice == " |
| - | return | + | return |
| - | + | ||
| - | if choice == " | + | if choice == " |
| - | os.system(" | + | os.system(" |
| - | continue | + | continue |
| - | + | ||
| - | if choice == " | + | if choice == " |
| - | new_sel = choose_switches(hosts) | + | new_sel = choose_switches(hosts) |
| - | if new_sel: | + | if new_sel: |
| - | current_selection = new_sel | + | current_selection = new_sel |
| - | continue | + | continue |
| - | + | ||
| - | if choice == " | + | if choice == " |
| - | safe_print(" | + | safe_print(" |
| - | commands = [] | + | commands = [] |
| - | + | ||
| - | while True: | + | while True: |
| - | cmd = input("> | + | cmd = input("> |
| - | if cmd == "": | + | if cmd == "": |
| - | if commands: | + | if commands: |
| - | break | + | break |
| - | else: | + | else: |
| - | continue | + | continue |
| - | commands.append(cmd) | + | commands.append(cmd) |
| - | + | ||
| - | safe_print(" | + | safe_print(" |
| - | + | ||
| - | start_all = datetime.now() | + | start_all = datetime.now() |
| - | + | ||
| - | results = [] | + | results = [] |
| - | with ThreadPoolExecutor(max_workers=min(MAX_WORKERS, | + | with ThreadPoolExecutor(max_workers=min(MAX_WORKERS, |
| - | future_map = { | + | future_map = { |
| - | pool.submit(run_on_switch, | + | pool.submit(run_on_switch, |
| - | for h in current_selection | + | for h in current_selection |
| - | } | + | } |
| - | + | ||
| - | for f in as_completed(future_map): | + | for f in as_completed(future_map): |
| - | results.append(f.result()) | + | results.append(f.result()) |
| - | + | ||
| - | # --- Résumé synthétique --- | + | # --- Résumé synthétique --- |
| - | safe_print(" | + | safe_print(" |
| - | for r in results: | + | for r in results: |
| - | host = r[" | + | host = r[" |
| - | status = " | + | status = " |
| - | lines = len(r[" | + | lines = len(r[" |
| - | dur = (r[" | + | dur = (r[" |
| - | safe_print(f" | + | safe_print(f" |
| - | if r[" | + | if r[" |
| - | safe_print(f" | + | safe_print(f" |
| - | + | ||
| - | total = (datetime.now() - start_all).total_seconds() | + | total = (datetime.now() - start_all).total_seconds() |
| - | safe_print(f" | + | safe_print(f" |
| - | safe_print(" | + | safe_print(" |
| + | |||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| - | if __name__ == " | ||
| - | main() | ||