#!/usr/bin/env python3 """Remove www.gstatic.com and *.gstatic.com from walled garden via RSC import.""" from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) RSC = r""" :put "WG-GSTATIC START" :foreach i in=[/ip hotspot walled-garden find where dst-host~"gstatic"] do={ :local h [/ip hotspot walled-garden get $i dst-host] :if ($h != "fonts.gstatic.com") do={ :put ("REMOVE " . $h) :do { /ip hotspot walled-garden remove $i } on-error={} } } :put "WG-GSTATIC LEFT" /ip hotspot walled-garden print where dst-host~"gstatic" """ def hub() -> None: from mk_hub_exec import import_rsc print(import_rsc("wo-wg-gstatic.rsc", RSC, timeout=60)) def nko() -> None: import paramiko from mk_rb4011_exec import JUMP, MK jump = paramiko.SSHClient() jump.set_missing_host_key_policy(paramiko.AutoAddPolicy()) jump.connect(JUMP[0], username=JUMP[1], password=JUMP[2], timeout=25, allow_agent=False, look_for_keys=False) chan = jump.get_transport().open_channel("direct-tcpip", (MK[0], 22), ("127.0.0.1", 0)) mk = paramiko.SSHClient() mk.set_missing_host_key_policy(paramiko.AutoAddPolicy()) mk.connect(MK[0], username=MK[1], password=MK[2], sock=chan, timeout=25, allow_agent=False, look_for_keys=False) try: sftp = mk.open_sftp() with sftp.file("wo-wg-gstatic.rsc", "w") as f: f.write(RSC) sftp.close() stdin, stdout, stderr = mk.exec_command("/import file-name=wo-wg-gstatic.rsc", timeout=60) print(stdout.read().decode("utf-8", "replace")) err = stderr.read().decode("utf-8", "replace") if err.strip(): print("STDERR", err) finally: mk.close() jump.close() if __name__ == "__main__": which = sys.argv[1] if len(sys.argv) > 1 else "both" if which in ("hub", "both"): print("==== HUB ====") hub() if which in ("nko", "both"): print("==== NKO ====") nko()