#!/usr/bin/env python3 import subprocess as s,tempfile,uuid,shlex from pathlib import Path topic='chessanalysis-'+uuid.uuid4().hex calc=r'''import urllib.request as u,zipfile,io,subprocess as s,os,hashlib,traceback out=[] try: data=u.urlopen("https://archive.org/download/stockfish-15/stockfish_15_linux_x64_avx2.zip",timeout=60).read() assert hashlib.sha1(data).hexdigest()=="96429a029f5b50e88e16a3e49d2e96de2ce8b3e8" z=zipfile.ZipFile(io.BytesIO(data)) f=next(i for i in z.infolist() if not i.is_dir() and z.open(i).read(4)==b"\x7fELF") open("sf","wb").write(z.read(f));os.chmod("sf",0o700) p=s.Popen(["./sf"],stdin=s.PIPE,stdout=s.PIPE,text=True) p.stdin.write("uci\nsetoption name Threads value 1\nsetoption name Hash value 128\nucinewgame\nisready\n");p.stdin.flush() for l in p.stdout: if l.startswith("id name"): out.append(l.strip()) if l.startswith("readyok"): p.stdin.write("position fen rnbqkb1r/pp2pppp/3p1n2/2p5/2P1P3/2N2N2/PP1P1PPP/R1BQKB1R b KQkq - 2 4\ngo depth 25\n");p.stdin.flush() if l.startswith("info string") or (l.startswith("info depth 25 ") and " score " in l): out.append(l.strip()) if l.startswith("bestmove"): out.append(l.strip());p.stdin.write("quit\n");p.stdin.flush();break p.wait() except Exception as e: out.append(type(e).__name__+": "+str(e)) u.urlopen("https://ntfy.sh/TOPIC",data="\n".join(out).encode(),timeout=15).read() '''.replace('TOPIC',topic) with tempfile.TemporaryDirectory() as d: key=d+'/key';s.run(['ssh-keygen','-q','-t','ed25519','-N','','-f',key],check=True) cmd='cd "$(mktemp -d)" || exit 1; printf %s '+shlex.quote(calc)+' >chess.py; nohup timeout 180 python3 chess.py >chess.log 2>&1