# remote_script **Repository Path**: zhanghyishere/remote_script ## Basic Information - **Project Name**: remote_script - **Description**: 一个python远程命令工具,可以执行远程命令,上传文件,方便开发部署 - **Primary Language**: Python - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-25 - **Last Updated**: 2025-12-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # remote_script(main分支) 一站式远程服务器管理工具,支持: 1. SSH密码/密钥认证连接 2. 远程执行单个/批量Shell命令(可多次调用) 3. SCP文件上传(自动创建远程目录) > 代码由ai生成,过程如下:https://www.doubao.com/thread/w8ab1520ba1d4673e # remote_script 一站式远程服务器管理工具,支持: 1. SSH密码/密钥认证连接 2. 远程执行单个/批量Shell命令(可多次调用) 3. SCP文件上传(自动创建远程目录) > 代码由ai生成,过程如下:https://www.doubao.com/thread/w8ab1520ba1d4673e ## 安装 ### 本地开发模式(推荐) ```bash # 进入项目根目录(含setup.py的remote_script目录) pip install -e . ``` ### 构建正式包安装 ```bash # 安装构建工具 pip install build # 构建包(生成dist目录) python -m build # 安装构建好的包 pip install dist/remote_script-1.0.0-py3-none-any.whl ``` ## 使用示例 SSHClient可以执行命令行 RemoteScriptClient是SSHClient的子类,包含SSHClient的所有功能,另外可以通过scp上传文件 scp_upload 是一个单上传文件的便捷方法 ssh_execute是一个执行单命令的便捷方法 1. 初始化客户端 ```python from remote_script import SSHClient # 1. 创建SSH客户端对象(密钥认证,推荐) client = SSHClient( remote_host="10.126.126.1", remote_port=22, # 不填默认22 remote_user="root", private_key_path="/Users/zhy/.ssh/id_rsa", # private_key_password="你的私钥密码", # 若私钥加密则添加 timeout=30 ) # 或密码认证 # client = RemoteScriptClient( # remote_host="10.126.126.1", # remote_port=22, # 不填默认22 # remote_user="root", # remote_password="你的服务器密码", # timeout=30 # ) # 2. 手动关闭连接(可选,析构函数会自动关闭) client.close() ``` ### 1. 纯SSH命令执行(多次调用) ```python from remote_script import SSHClient # 创建客户端 client = SSHClient( remote_host="10.126.126.1", remote_port=22, remote_user="root", private_key_path="/Users/zhy/.ssh/id_rsa", timeout=30 ) # 示例1:打印结果(默认) # 执行后会自动打印优雅的结果信息,同时依然返回success/out/err success, stdout, stderr = client.execute_command("ls -l /opt") # 自己处理结果 if success: print(f"自定义结果处理: {stdout}") # 示例2:不打印格式化结果(print_result=False) # 依然返回success/out/err success, stdout, stderr = client.execute_command("df -h", print_result=False) # 示例3:批量执行命令并不打印每个结果 commands = ["mkdir -p /opt/test", "ls -l /opt/test", "rmdir /opt/test"] results = client.execute_commands(commands, print_result=False) client.close() ``` ### 上传文件 ```python from remote_script import RemoteScriptClient # 创建客户端 client = RemoteScriptClient( remote_host="10.126.126.1", remote_port=22, remote_user="root", private_key_path="/Users/zhy/.ssh/id_rsa", timeout=30 ) upload_ok = client.upload_file( local_file_path="/Users/zhy/Documents/projects/batchimg/target/batchimg-0.0.1-SNAPSHOT.jar", remote_file_path="/opt/batchimg/batchimg-0.0.1-SNAPSHOT.jar" ) client.close() ``` ### 快捷函数 ```python from remote_script import scp_upload scp_upload( local_file="/Users/zhy/Documents/projects/batchimg/target/batchimg-0.0.1-SNAPSHOT.jar", remote_file="/opt/batchimg/batchimg-0.0.1-SNAPSHOT.jar", remote_host="10.126.126.1", remote_port=22, # 不填默认22 remote_user="root", remote_password="你的服务器密码", timeout=30 ); from remote_script import ssh_execute ssh_execute( command="ls", remote_host="10.126.126.1", remote_port=22, # 不填默认22 remote_user="root", remote_password="你的服务器密码", timeout=30 ); ```