# -*- coding: utf-8 -*- import paramiko SCRIPT = r''' import json, urllib.request, http.cookiejar, time for i in range(10): try: code = urllib.request.urlopen("http://127.0.0.1:8014/api/health", timeout=3).status print("health", code) break except Exception as e: print("wait", i, type(e).__name__) time.sleep(2) else: print("health FAIL") cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) req = urllib.request.Request( "http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode(), headers={"Content-Type": "application/json"}, method="POST", ) print("login", op.open(req, timeout=10).status) p = json.loads(op.open("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", timeout=20).read()) print("exclude", p.get("ok"), "count", p.get("count"), "effective", p.get("effective_room_count")) ''' def check(port, user, pwd, label, sudo=False): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", port, user, pwd, timeout=20) if sudo: sftp = c.open_sftp() with sftp.file("/tmp/_v_tex2.py", "w") as f: f.write(SCRIPT) sftp.close() cmd = f"echo '{pwd}' | sudo -S -p '' python3 /tmp/_v_tex2.py" _, o, e = c.exec_command(cmd, timeout=80) else: _, o, e = c.exec_command("python3 -", timeout=80) o.channel.sendall(SCRIPT.encode()) o.channel.shutdown_write() print("====", label, "====") print(o.read().decode("utf-8", "replace")) err = e.read().decode("utf-8", "replace") if err.strip(): print(err[-400:]) c.close() if __name__ == "__main__": check(22, "root", "Xiao4669805", "vps") check(4014, "eric", "4669805", "phys", sudo=True)