132 lines
4.1 KiB
Python
132 lines
4.1 KiB
Python
import paramiko
|
|
import time
|
|
|
|
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)
|
|
|
|
# Get current scheduler on-event scripts
|
|
schedulers = ["Bulanan2", "Bulanan4", "vc-2K3J", "vc-5K10J", "Family", "DevB2"]
|
|
|
|
# Use shell for multi-step operations
|
|
chan = client.invoke_shell()
|
|
chan.settimeout(15)
|
|
time.sleep(2)
|
|
|
|
def read_output():
|
|
out = b''
|
|
while chan.recv_ready():
|
|
out += chan.recv(65535)
|
|
return out.decode('utf-8', errors='replace')
|
|
|
|
# Clear initial banner
|
|
banner = read_output()
|
|
|
|
for s in schedulers:
|
|
print(f"\n=== Fixing {s} ===")
|
|
|
|
# Step 1: Get current on-event via exec_command
|
|
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 on-event: {err}")
|
|
continue
|
|
|
|
if ':pic' not in on_event:
|
|
print(f" No :pic found, skipping")
|
|
continue
|
|
|
|
fixed = on_event.replace(':pic', ':pick')
|
|
count = on_event.count(':pic')
|
|
|
|
# Step 2: Create temp script with fixed content
|
|
# Escape the content for RouterOS CLI: replace " with \", $ with \$
|
|
escaped = fixed.replace('"', '\\"').replace('$', '\\$')
|
|
|
|
temp_name = f"fix_{s}"
|
|
cmd = f'/system script add name={temp_name} source="{escaped}"'
|
|
chan.send(cmd + '\n')
|
|
time.sleep(1.5)
|
|
out = read_output()
|
|
if 'failure' in out.lower():
|
|
# Maybe already exists, try remove first
|
|
chan.send(f'/system script remove {temp_name}\n')
|
|
time.sleep(1)
|
|
read_output()
|
|
chan.send(cmd + '\n')
|
|
time.sleep(1.5)
|
|
out = read_output()
|
|
|
|
print(f" Temp script created: {out[:100]}")
|
|
|
|
# Step 3: Set scheduler on-event to use temp script source
|
|
cmd = f'/system scheduler set {s} on-event=[/system script get {temp_name} source] disabled=no'
|
|
chan.send(cmd + '\n')
|
|
time.sleep(1.5)
|
|
out = read_output()
|
|
print(f" Scheduler set: {out[:100]}")
|
|
|
|
# Step 4: Remove temp script
|
|
chan.send(f'/system script remove {temp_name}\n')
|
|
time.sleep(1)
|
|
read_output()
|
|
|
|
print("\n=== Verification ===")
|
|
for s in schedulers:
|
|
stdin, stdout, stderr = client.exec_command(f'/system scheduler get {s} on-event')
|
|
oe = stdout.read().decode().strip()
|
|
has_pic = ':pic' in oe
|
|
has_pick = ':pick' in oe
|
|
status = 'OK' if has_pick and not has_pic else 'BROKEN'
|
|
print(f" {s}: {status} (pic={has_pic} pick={has_pick})")
|
|
|
|
# Also fix profile on-login via same approach
|
|
print("\n=== Fixing profile on-login ===")
|
|
profiles = ["Bulanan2", "Bulanan4", "vc-2K3J", "vc-5K10J", "Family", "home", "DevB2"]
|
|
|
|
for p in profiles:
|
|
print(f"\n--- {p} ---")
|
|
stdin, stdout, stderr = client.exec_command(f'/ip hotspot user/profile get {p} on-login')
|
|
ol = stdout.read().decode().strip()
|
|
err = stderr.read().decode().strip()
|
|
if err:
|
|
print(f" Error: {err}")
|
|
continue
|
|
|
|
if ':pic' not in ol:
|
|
print(f" No :pic found, skipping")
|
|
continue
|
|
|
|
fixed = ol.replace(':pic', ':pick')
|
|
count = ol.count(':pic')
|
|
escaped = fixed.replace('"', '\\"').replace('$', '\\$')
|
|
|
|
temp_name = f"fix_{p}_login"
|
|
chan.send(f'/system script add name={temp_name} source="{escaped}"\n')
|
|
time.sleep(1.5)
|
|
out = read_output()
|
|
print(f" Temp script created: {out[:80]}")
|
|
|
|
cmd = f'/ip hotspot user/profile set {p} on-login=[/system script get {temp_name} source]'
|
|
chan.send(cmd + '\n')
|
|
time.sleep(1.5)
|
|
out = read_output()
|
|
print(f" Profile set: {out[:80]}")
|
|
|
|
chan.send(f'/system script remove {temp_name}\n')
|
|
time.sleep(1)
|
|
read_output()
|
|
|
|
print("\n=== Profile Verification ===")
|
|
for p in profiles:
|
|
stdin, stdout, stderr = client.exec_command(f'/ip hotspot user/profile get {p} on-login')
|
|
ol = stdout.read().decode().strip()
|
|
has_pic = ':pic' in ol
|
|
has_pick = ':pick' in ol
|
|
status = 'OK' if has_pick and not has_pic else 'BROKEN'
|
|
print(f" {p}: {status}")
|
|
|
|
client.close()
|
|
print("\nALL DONE")
|