"""Upload large file to server via plink PTY base64 chunks.""" import subprocess import base64 import hashlib import sys import os LOCAL_FILE = r"D:\资料\03-项目开发\wecom_it_smart_desk\tmp-h5-dist.tar.gz" REMOTE_FILE = "/tmp/h5-dist.tar.gz" CHUNK_SIZE = 4000 # chars per chunk PLINK = r"C:\Users\simon\.workbuddy\skills\jumpserver-ops\scripts\plink.exe" HOST = "sxn@10.212.189.210" PORT = "2222" PASSWORD = os.environ.get("JMS_PASSWORD", "") # May be cached def run_plink(commands): """Run commands via plink PTY.""" cmd = [PLINK, "-P", str(PORT), "-batch", HOST] stdin_data = "\n".join(commands) + "\nexit\n" result = subprocess.run( cmd, input=stdin_data, capture_output=True, text=True, timeout=300 ) return result.stdout + result.stderr def main(): # Read file with open(LOCAL_FILE, "rb") as f: data = f.read() local_md5 = hashlib.md5(data).hexdigest() print(f"File: {LOCAL_FILE}") print(f"Size: {len(data)} bytes") print(f"MD5: {local_md5}") # Base64 encode b64 = base64.b64encode(data).decode("ascii") total_chunks = (len(b64) + CHUNK_SIZE - 1) // CHUNK_SIZE print(f"Base64 length: {len(b64)} chars, {total_chunks} chunks") # Clear remote file print("Clearing remote file...") run_plink([f"> {REMOTE_FILE}.b64"]) # Send chunks for i in range(total_chunks): start = i * CHUNK_SIZE end = min(start + CHUNK_SIZE, len(b64)) chunk = b64[start:end] cmd = f"echo '{chunk}' >> {REMOTE_FILE}.b64" run_plink([cmd]) if (i + 1) % 10 == 0 or i == total_chunks - 1: print(f" Sent chunk {i+1}/{total_chunks}") # Decode and verify print("Decoding and verifying...") verify_cmds = [ f"base64 -d {REMOTE_FILE}.b64 > {REMOTE_FILE}", f"wc -c < {REMOTE_FILE}", f"md5sum {REMOTE_FILE}", f"rm -f {REMOTE_FILE}.b64", ] output = run_plink(verify_cmds) print(f"Server output:\n{output}") if local_md5 in output: print(f"\n✅ MD5 match! Upload successful.") return 0 else: print(f"\n❌ MD5 mismatch! Expected: {local_md5}") return 1 if __name__ == "__main__": sys.exit(main())