Files

68 lines
2.4 KiB
Python
Raw Permalink Normal View History

"""Split and upload large file to server via JumpServer base64 chunks."""
import base64
import hashlib
import os
import subprocess
import sys
import time
PYTHON = r"C:\Users\simon\.workbuddy\binaries\python\versions\3.13.12\python.exe"
JMS = r"C:\Users\simon\.workbuddy\skills\jumpserver-ops\scripts\jms_ops.py"
LOCAL_FILE = r"D:\资料\03-项目开发\wecom_it_smart_desk\deploy_agent_v8.tar.gz"
REMOTE_FILE = "/tmp/deploy_agent_v8.tar.gz"
CHUNK_SIZE = 12 * 1024 # 12KB raw -> ~16KB base64, safe for command line
def run_jms(*args, timeout=30):
cmd = [PYTHON, JMS] + list(args)
result = subprocess.run(cmd, capture_output=True, timeout=timeout)
out = result.stdout.decode('utf-8', errors='replace') if result.stdout else ''
err = result.stderr.decode('utf-8', errors='replace') if result.stderr else ''
return out + err
def main():
with open(LOCAL_FILE, "rb") as f:
data = f.read()
md5 = hashlib.md5(data).hexdigest()
total_chunks = (len(data) + CHUNK_SIZE - 1) // CHUNK_SIZE
print(f"File: {LOCAL_FILE}")
print(f"Size: {len(data)} bytes ({len(data)/1024/1024:.2f} MB)")
print(f"MD5: {md5}")
print(f"Chunks: {total_chunks} (chunk size: {CHUNK_SIZE} bytes)")
print()
# Clear remote file
print("Clearing remote file...")
run_jms("exec", "-c", f"rm -f {REMOTE_FILE}", "--cmd-timeout", "10")
# Upload chunks
for i in range(total_chunks):
chunk = data[i * CHUNK_SIZE : (i + 1) * CHUNK_SIZE]
b64 = base64.b64encode(chunk).decode("ascii")
cmd = f'echo -n "{b64}" | base64 -d >> {REMOTE_FILE}'
result = run_jms("exec", "-c", cmd, "--cmd-timeout", "15")
if (i + 1) % 20 == 0 or i == total_chunks - 1:
print(f" Uploaded chunk {i + 1}/{total_chunks} ({(i + 1) * 100 // total_chunks}%)")
# Check for errors
if "error" in result.lower() and "traceback" not in result.lower():
# jms_ops.py always prints some status, check if the command actually failed
pass
# Verify MD5
print("\nVerifying MD5...")
result = run_jms("exec", "-c", f"md5sum {REMOTE_FILE}", "--cmd-timeout", "15")
print(f" Remote MD5: {result.strip()}")
print(f" Local MD5: {md5}")
if md5 in result:
print("\n✅ MD5 verified - upload successful!")
else:
print("\n❌ MD5 mismatch - upload may be corrupted!")
sys.exit(1)
if __name__ == "__main__":
main()