# commandSenders **Repository Path**: sanchez023/commandSenders ## Basic Information - **Project Name**: commandSenders - **Description**: No description available - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-12 - **Last Updated**: 2025-10-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SSH Command Distributor 一个强大的SSH命令分发工具,可以同时向多台服务器执行命令和上传文件。 ## 功能特性 - 🚀 **批量命令执行**:同时向多台服务器分发和执行命令 - 📁 **文件分发**:支持向多台服务器上传文件 - 🔐 **多种认证方式**:支持密码和密钥认证 - ⚡ **并行处理**:使用多线程并行执行,提高效率 - 📊 **实时反馈**:实时显示命令执行结果和状态 - 🔧 **配置化管理**:使用YAML配置文件管理服务器信息 - 🛡️ **连接测试**:支持服务器连接性测试 ## 项目结构 ``` ssh_distributor/ ├── models.py # 数据模型定义 ├── config_loader.py # 配置文件加载器 ├── ssh_connect.py # SSH连接管理 ├── command_distributor.py # 命令分发器 ├── distributor_shell.py # 交互式Shell ├── main.py # 程序入口 └── config.yaml # 配置文件示例 ``` ## 安装依赖 ```bash pip install paramiko pyyaml ``` ## 快速开始 ### 1. 创建配置文件 创建 `config.yaml` 文件: ```yaml version: "1.0" ssh: - name: "web-server-1" host: "192.168.1.100" port: "22" user: "root" pwd: "password123" group: "web-servers" - name: "db-server-1" host: "192.168.1.101" port: "22" user: "admin" key_file: "/path/to/private_key" group: "db-servers" - name: "test-server" host: "192.168.1.102" port: "2222" user: "ubuntu" pwd: "ubuntu123" group: "test-servers" ``` ### 2. 运行程序 ```bash python main.py ``` ### 3. 使用交互式Shell 程序启动后,进入交互式界面: ```bash >> ls -la # 在所有服务器执行 ls -la 命令 >> file:/path/to/local/file # 上传文件到所有服务器 >> exit # 退出程序 ``` ## 配置文件说明 ### 服务器配置字段 | 字段名 | 必填 | 说明 | |--------|------|------| | `name` | 是 | 服务器唯一标识名称 | | `host` | 是 | 服务器主机名或IP地址 | | `port` | 是 | SSH端口号 | | `user` | 是 | 用户名 | | `pwd` | 否 | 密码(与key_file二选一) | | `key_file` | 否 | 私钥文件路径(与pwd二选一) | | `group` | 否 | 服务器分组 | ## 使用方法 ### 基本命令执行 ```python from distributor_shell import CommandDistributorShell # 启动交互式Shell shell = CommandDistributorShell() shell.loop() ``` ### 编程式使用 ```python from config_loader import ConfigLoader from command_distributor import CommandDistributor # 加载配置 config = ConfigLoader("config.yaml") # 创建分发器 distributor = CommandDistributor(config) # 连接所有服务器 distributor.connect_all() # 执行命令 results = distributor.distributed_command("df -h") # 上传文件 upload_results = distributor.fileupload("/local/path/file.txt", "/remote/path/") # 关闭连接 distributor.close_all_connections() ``` ### 连接测试 ```python # 测试服务器连接性 distributor.test_connective() ``` ## API参考 ### ConfigLoader - `__init__(file_path: str)` - 初始化配置加载器 - `get_connectInfo(name: str = None)` - 获取服务器连接信息 ### CommandDistributor - `connect_all()` - 连接所有配置的服务器 - `distributed_command(command: str)` - 分发命令到所有服务器 - `fileupload(inpath: str, distpath: str)` - 上传文件到所有服务器 - `test_connective()` - 测试服务器连接性 - `close_all_connections()` - 关闭所有SSH连接 ### SSHConnect - `connect()` - 建立SSH连接 - `command(command: str)` - 执行单个命令 - `fileupload(inpath: str, distpath: str)` - 上传单个文件 - `close()` - 关闭连接 ## 输出示例 ``` have loaded 3 servers trying to connect web-server-1 ✓ Successfully connected to web-server-1 trying to connect db-server-1 ✓ Successfully connected to db-server-1 trying to connect test-server ✓ Successfully connected to test-server >> ls -la Executing command on web-server-1: ls -la Executing command on db-server-1: ls -la Executing command on test-server: ls -la ✓ web-server-1: Command executed successfully ✓ db-server-1: Command executed successfully ✓ test-server: Command executed successfully ``` ## 错误处理 - 连接失败时会显示具体错误信息 - 命令执行失败会返回错误状态 - 文件不存在时会提示错误 - 配置格式错误会抛出异常 ## 注意事项 1. **安全性**:配置文件包含敏感信息,请妥善保管 2. **网络要求**:确保本地网络可以访问所有目标服务器 3. **权限**:确保SSH用户有执行命令和文件上传的权限 4. **超时设置**:默认命令执行超时为30秒,可根据需要调整 ## 故障排除 ### 常见问题 1. **连接被拒绝** - 检查服务器SSH服务是否运行 - 确认端口号是否正确 - 检查防火墙设置 2. **认证失败** - 确认用户名和密码正确 - 检查密钥文件路径和权限 - 确认服务器是否允许该用户登录 3. **命令执行无输出** - 检查命令是否存在 - 确认用户有执行权限 - 查看服务器系统日志 ## 许可证 MIT License ## 贡献 欢迎提交Issue和Pull Request来改进这个项目。 ## 更新日志 ### v1.0.0 - 初始版本发布 - 支持批量命令执行 - 支持文件上传 - 交互式Shell界面