Files
mikhmon-fix/fix-router.py
T
2026-08-15 00:28:08 +07:00

108 lines
3.5 KiB
Python

import paramiko
import re
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.100.2', username='mi4', password='m14', look_for_keys=False, allow_agent=False)
chan = client.invoke_shell()
chan.settimeout(30)
def send_cmd(cmd, wait=2):
import time
chan.send(cmd + '\n')
time.sleep(wait)
output = b''
while chan.recv_ready():
output += chan.recv(65535)
return output.decode('utf-8', errors='replace')
# Get current scheduler on-event scripts
schedulers = ["Bulanan2", "Bulanan4", "vc-2K3J", "vc-5K10J"]
for s in schedulers:
print(f"\n=== Fixing scheduler: {s} ===")
# Get current on-event
stdin, stdout, stderr = client.exec_command(f'/system scheduler get {s} on-event')
on_event = stdout.read().decode().strip()
err = stderr.read().decode().strip()
if err:
print(f" ERROR reading {s}: {err}")
continue
# Fix :pic to :pick
fixed = on_event.replace(':pic', ':pick')
if fixed == on_event:
print(f" No :pic found - skipping")
continue
# How many :pic -> :pick replacements?
count = on_event.count(':pic')
print(f" Fixed {count} occurrences of :pic")
# Set via API - need careful quoting
# Use a temporary script approach to avoid quoting hell
stdin, stdout, stderr = client.exec_command(f'/system scheduler set {s} disabled=no on-event="{fixed}"')
result = stdout.read().decode().strip()
err = stderr.read().decode().strip()
if result: print(f" Result: {result}")
if err: print(f" ERROR: {err}")
# Verify
stdin, stdout, stderr = client.exec_command(f'/system scheduler get {s} on-event')
verify = stdout.read().decode().strip()
verr = stderr.read().decode().strip()
if verr:
print(f" Verify error: {verr}")
elif ':pic' in verify:
print(f" WARNING: :pic still present!")
elif ':pick' in verify:
print(f" VERIFIED: :pick present, script fixed!")
else:
print(f" UNEXPECTED: neither :pic nor :pick found")
print(f" First 100 chars: {verify[:100]}")
# Now fix the on-login scripts in the hotspot profiles
print(f"\n\n=== Fixing hotspot profile on-login scripts ===")
profiles_to_fix = ["Bulanan2", "Bulanan4", "vc-2K3J", "vc-5K10J", "Family", "DevB2"]
for p in profiles_to_fix:
print(f"\n--- Profile: {p} ---")
stdin, stdout, stderr = client.exec_command(f'/ip hotspot user/profile get {p} on-login')
on_login = stdout.read().decode().strip()
err = stderr.read().decode().strip()
if err:
print(f" ERROR reading: {err}")
continue
if ':pic' not in on_login:
print(f" No :pic found (already fixed)")
continue
count = on_login.count(':pic')
fixed = on_login.replace(':pic', ':pick')
print(f" Fixed {count} occurrences")
# It might be easier to just use the set command
import time
chan.send(f'/ip hotspot user/profile set {p} on-login="{fixed}"\n')
time.sleep(1)
while chan.recv_ready():
time.sleep(0.5)
# Verify
stdin, stdout, stderr = client.exec_command(f'/ip hotspot user/profile get {p} on-login')
verify = stdout.read().decode().strip()
verr = stderr.read().decode().strip()
if verr:
print(f" Verify error: {verr}")
elif ':pic' in verify:
print(f" WARNING: :pic still present in on-login!")
elif ':pick' in verify:
print(f" VERIFIED: on-login fixed!")
else:
print(f" First 100: {verify[:100]}")
client.close()
print("\n\nDONE")