# remote_exec **Repository Path**: chmodke/remote_exec ## Basic Information - **Project Name**: remote_exec - **Description**: shell批量执行工具 / shell batch execute tool - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-07-27 - **Last Updated**: 2025-12-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: shell, 批处理, 工具, 服务器管理 ## README # remote_exec linux服务器批量操作工具。 提供三个功能: 1. 批量上传文件 2. 以root用户批量执行命令 3. 批量下载文件 通过组合这三个功能能完成大多数Linux服务器上的操作。 ## 特性 - 单文件分发,无依赖,兼容好 - 跨平台,可在Windows、Linux,x86、ARM环境中执行 - 任务固化,可重复使用,可对任务进行分组 - 可配置性好,支持多种远端环境 - 敏感配置内容支持加密存储 ## 配置说明 ### 服务器信息配置 config.yaml文件 ```yaml port: 22 root-prompt: '#' passwd-prompt: '.*assword.*' timeout: 10 user: 'kehao' passwd: '123456' root-passwd: '123456' hosts: - '172.18.0.3' - '172.18.0.4' spc-hosts: - '127.0.0.1 23 123456 123456' ``` - port: ssh端口; - root-prompt:root用户的提示信息,golang正则表达式; - passwd-prompt:root用户输入password的提示信息,golang正则表达式; - timeout:等待登录的超时时长,单位:秒; - user:ssh登录用户; - passwd:ssh登录密码; - root-passwd:root用户密码; - hosts:服务器IP地址列表; - spc_hosts:特殊密码的服务器列表,格式:`IP PORT 用户密码 root用户密码`。 ### 任务配置 command.yaml文件 ```yaml put: - /home/kehao/test/test.sh#/home/kehao exec: - /bin/sh /home/kehao/test.sh get: - /home/kehao/result.txt#/home/kehao/result arthas: put: - /home/kehao/test/install-jdk.sh#/home/kehao - /home/kehao/test/install-arthas.sh#/home/kehao exec: - /bin/sh /home/kehao/install-jdk.sh - /bin/sh /home/kehao/install-arthas.sh {{.arthas_version}} get: - /home/kehao/arthas-result.txt#/home/kehao/result - /home/kehao/jdk-result.txt#/home/kehao/result ``` ### 功能列表 - put:上传文件列表,格式:`本地文件路径#远程目录名称`,远程目录名称可缺省,默认与本地文件路径的目录名相同,ssh登录用户需要具有远程目录的写权限; - exec:需要执行的命令列表,命令将以`root`用户执行; - get:下载的文件列表,格式:`远程文件路径#本地目录名称`,本地目录名称可缺省,默认与远程文件路径的目录名相同,ssh登录用户需要具有远程文件的读权限; - enc:配置文件加解密。 ## 使用方法 ### 命令 #### 批量上传 ```shell remote put [flags] [section] ``` #### 批量执行 ```shell remote exec [flags] [section] ``` #### 批量下载 ```shell remote get [flags] [section] ``` #### 配置加密 > 对config.yaml文件中的密码字段经行加密存储。 ```shell remote enc [flags] ``` ### 参数 ```shell remote --help shell batch execute tool Usage: remote [command] [flags] [section] Available Commands: completion Generate the autocompletion script for the specified shell enc encrypt config file exec execute command on remote get get file from remote help Help about any command put put file to remote Flags: -c, --command string specify commands configuration (default "command.yaml") -f, --config string specify servers configuration (default "config.yaml") -e, --execute execute single command, will disable the command option -h, --help help for remote -m, --netmask string ip filter, e.g. 192.168.1.1 192.168.1.1,192.168.1.2 192.168.0.0/24 -t, --thread int maximum number of concurrent (0 < t <= 16) (default 1) -v, --var strings command params, e.g. key1=value1. Use parameter value via {{.key1}}. Note: parameter naming rule is ^[a-zA-Z][a-zA-Z0-9_\\-]*$ -v, --version version for remote Use "remote [command] --help" for more information about a command. ``` - 服务器配置文件(f),默认值config.yaml - 命令配置文件(c),默认值command.yaml - 最大并发数(t),0 < t <= 16,默认值1,开启并发执行能提高执行效率,但是输出结果会乱序 - IP地址过滤(m),默认为空,支持192.168.1.1、192.168.1.1,192.168.1.2、192.168.0.0/24三种写法,匹配的主机才会执行命令 - 单命令执行(e),默认为空,命令行规则与配置文件一致;当启用单命令模式时,配置文件预置命令(c)将失效 - 命令模板参数(v),向命令模板中传递参数,可传递多个参数,使用golang template语法读取参数值 ## 使用样例 测试脚本: install-jdk.sh ```shell curl -s -k https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz -o jdk-8u202-linux-x64.tar.gz mkdir -p /opt/jdk tar -xf jdk-8u202-linux-x64.tar.gz -C /opt/jdk chmod -R a+rx /opt/jdk rm -f jdk-8u202-linux-x64.tar.gz echo "deploy jdk 8u202 finished" >/home/kehao/jdk-result.txt ``` install-arthas.sh ```shell version=$1 curl -s -k https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/${version}/arthas-packaging-${version}-bin.zip -o arthas-packaging-${version}-bin.zip mkdir -p /opt/arthas unzip -q -o arthas-packaging-${version}-bin.zip -d /opt/arthas chmod -R a+rx /opt/arthas rm -f arthas-packaging-${version}-bin.zip echo "deploy arthas ${version} finished" > /home/kehao/arthas-result.txt ``` 运行日志: ```powershell kehao@kehao-server:~/remote_exec$ sh remote.sh put arthas 2025/11/15 21:17:20 start put file... 2025/11/15 21:17:20 [172.18.0.3:22] upload /home/kehao/test/install-jdk.sh to /home/kehao. 2025/11/15 21:17:20 [172.18.0.3:22] upload /home/kehao/test/install-jdk.sh finished! 2025/11/15 21:17:20 [172.18.0.3:22] upload /home/kehao/test/install-arthas.sh to /home/kehao. 2025/11/15 21:17:20 [172.18.0.3:22] upload /home/kehao/test/install-arthas.sh finished! 2025/11/15 21:17:20 progress [1/3]... 2025/11/15 21:17:21 [172.18.0.4:22] upload /home/kehao/test/install-jdk.sh to /home/kehao. 2025/11/15 21:17:21 [172.18.0.4:22] upload /home/kehao/test/install-jdk.sh finished! 2025/11/15 21:17:21 [172.18.0.4:22] upload /home/kehao/test/install-arthas.sh to /home/kehao. 2025/11/15 21:17:21 [172.18.0.4:22] upload /home/kehao/test/install-arthas.sh finished! 2025/11/15 21:17:21 progress [2/3]... 2025/11/15 21:17:21 [127.0.0.1:23] upload /home/kehao/test/install-jdk.sh to /home/kehao. 2025/11/15 21:17:21 [127.0.0.1:23] upload /home/kehao/test/install-jdk.sh finished! 2025/11/15 21:17:21 [127.0.0.1:23] upload /home/kehao/test/install-arthas.sh to /home/kehao. 2025/11/15 21:17:21 [127.0.0.1:23] upload /home/kehao/test/install-arthas.sh finished! 2025/11/15 21:17:21 progress [3/3]... 2025/11/15 21:17:21 put file finished. kehao@kehao-server:~/remote_exec$ sh remote.sh exec arthas -v arthas_version=3.6.9 2025/11/15 21:17:23 start execute command... 2025/11/15 21:17:23 [172.18.0.3:22] execute (/bin/sh /home/kehao/install-jdk.sh). 2025/11/15 21:17:34 [172.18.0.3:22] execute (/bin/sh /home/kehao/install-jdk.sh) result: [root@32ab35c7df80 ~]# 2025/11/15 21:17:34 [172.18.0.3:22] execute (/bin/sh /home/kehao/install-arthas.sh 3.6.9). 2025/11/15 21:17:36 [172.18.0.3:22] execute (/bin/sh /home/kehao/install-arthas.sh 3.6.9) result: [root@32ab35c7df80 ~]# 2025/11/15 21:17:36 [172.18.0.3:22] execute command finished! 2025/11/15 21:17:36 progress [1/3]... 2025/11/15 21:17:36 [172.18.0.4:22] execute (/bin/sh /home/kehao/install-jdk.sh). 2025/11/15 21:17:47 [172.18.0.4:22] execute (/bin/sh /home/kehao/install-jdk.sh) result: [root@f0be69fcc20f ~]# 2025/11/15 21:17:47 [172.18.0.4:22] execute (/bin/sh /home/kehao/install-arthas.sh 3.6.9). 2025/11/15 21:17:49 [172.18.0.4:22] execute (/bin/sh /home/kehao/install-arthas.sh 3.6.9) result: [root@f0be69fcc20f ~]# 2025/11/15 21:17:49 [172.18.0.4:22] execute command finished! 2025/11/15 21:17:49 progress [2/3]... 2025/11/15 21:17:49 [127.0.0.1:23] execute (/bin/sh /home/kehao/install-jdk.sh). 2025/11/15 21:18:02 [127.0.0.1:23] execute (/bin/sh /home/kehao/install-jdk.sh) result: [root@fcde43436641 ~]# 2025/11/15 21:18:02 [127.0.0.1:23] execute (/bin/sh /home/kehao/install-arthas.sh 3.6.9). 2025/11/15 21:18:03 [127.0.0.1:23] execute (/bin/sh /home/kehao/install-arthas.sh 3.6.9) result: [root@fcde43436641 ~]# 2025/11/15 21:18:03 [127.0.0.1:23] execute command finished! 2025/11/15 21:18:03 progress [3/3]... 2025/11/15 21:18:03 execute command finished. kehao@kehao-server:~/remote_exec$ sh remote.sh get arthas 2025/11/15 21:18:13 start get file... 2025/11/15 21:18:13 [172.18.0.3:22] download file from /home/kehao/arthas-result.txt to /home/kehao/result. 2025/11/15 21:18:13 [172.18.0.3:22] download /home/kehao/arthas-result.txt finished! 2025/11/15 21:18:13 [172.18.0.3:22] download file from /home/kehao/jdk-result.txt to /home/kehao/result. 2025/11/15 21:18:13 [172.18.0.3:22] download /home/kehao/jdk-result.txt finished! 2025/11/15 21:18:13 progress [1/3]... 2025/11/15 21:18:13 [172.18.0.4:22] download file from /home/kehao/arthas-result.txt to /home/kehao/result. 2025/11/15 21:18:13 [172.18.0.4:22] download /home/kehao/arthas-result.txt finished! 2025/11/15 21:18:13 [172.18.0.4:22] download file from /home/kehao/jdk-result.txt to /home/kehao/result. 2025/11/15 21:18:13 [172.18.0.4:22] download /home/kehao/jdk-result.txt finished! 2025/11/15 21:18:13 progress [2/3]... 2025/11/15 21:18:13 [127.0.0.1:23] download file from /home/kehao/arthas-result.txt to /home/kehao/result. 2025/11/15 21:18:14 [127.0.0.1:23] download /home/kehao/arthas-result.txt finished! 2025/11/15 21:18:14 [127.0.0.1:23] download file from /home/kehao/jdk-result.txt to /home/kehao/result. 2025/11/15 21:18:14 [127.0.0.1:23] download /home/kehao/jdk-result.txt finished! 2025/11/15 21:18:14 progress [3/3]... 2025/11/15 21:18:14 get file finished. ```