1 Star 0 Fork 0

chmodke/remote_exec

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

remote_exec

linux服务器批量操作工具。

提供三个功能:

  1. 批量上传文件
  2. 以root用户批量执行命令
  3. 批量下载文件

通过组合这三个功能能完成大多数Linux服务器上的操作。

特性

  • 单文件分发,无依赖,兼容好
  • 跨平台,可在Windows、Linux,x86、ARM环境中执行
  • 任务固化,可重复使用,可对任务进行分组
  • 可配置性好,支持多种远端环境
  • 敏感配置内容支持加密存储

配置说明

服务器信息配置

config.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文件

put:
  - /home/kehao/test1.sh#/home/kehao
exec:
  - /bin/sh /home/kehao/test1.sh
get:
  - /home/kehao/result1.txt#/home/kehao/result

install-arthas:
  put:
    - /home/kehao/arthas-xxx.zip#/home/kehao
    - /home/kehao/install_arthas.sh#/home/kehao
    - /home/kehao/jdk-xxx.zip#/home/kehao
    - /home/kehao/install_jdk.sh#/home/kehao
  exec:
    - /bin/sh /home/kehao/install_arthas.sh
    - /bin/sh /home/kehao/install_jdk.sh
  get:
    - /home/kehao/install_arthas_result.txt#/home/kehao/result
    - /home/kehao/install_jdk8_result.txt#/home/kehao/result

功能列表

  • put:上传文件列表,格式:本地文件路径#远程目录名称,远程目录名称可缺省,默认与本地文件路径的目录名相同,ssh登录用户需要具有远程目录的写权限;
  • exec:需要执行的命令列表,命令将以root用户执行;
  • get:下载的文件列表,格式:远程文件路径#本地目录名称,本地目录名称可缺省,默认与远程文件路径的目录名相同,ssh登录用户需要具有远程文件的读权限;
  • enc:配置文件加解密。

使用方法

命令

批量上传

remote put [flags] [section]

批量执行

remote exec [flags] [section]

批量下载

remote get [flags] [section]

配置加密

对config.yaml文件中的密码字段经行加密存储。

remote enc [flags]

参数

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")
  -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, --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三种写法,匹配的主机才会执行命令

使用样例

测试脚本:

test1.sh

echo "test1.sh" >/home/kehao/result1.txt
hostname -s >>/home/kehao/result1.txt
hostname -i >>/home/kehao/result1.txt
id >>/home/kehao/result1.txt
chown kehao: /home/kehao/result1.txt
echo "I am $(hostname -i) :)"

运行日志:

kehao@kehaopcs ~/remote_exec $ ./remote_x86 put
2024/02/04 16:59:32 start put file...
2024/02/04 16:59:32 progress [2/3]...
2024/02/04 16:59:32 [172.18.0.3:22] upload /home/kehao/test1.sh to /home/kehao.
2024/02/04 16:59:32 [172.18.0.3:22] upload /home/kehao/test1.sh finished!
2024/02/04 16:59:32 progress [1/3]...
2024/02/04 16:59:32 [172.18.0.4:22] upload /home/kehao/test1.sh to /home/kehao.
2024/02/04 16:59:32 [172.18.0.4:22] upload /home/kehao/test1.sh finished!
2024/02/04 16:59:33 progress [0/3]...
2024/02/04 16:59:34 [127.0.0.1:23] upload /home/kehao/test1.sh to /home/kehao.
2024/02/04 16:59:34 [127.0.0.1:23] upload /home/kehao/test1.sh finished!
2024/02/04 16:59:34 put file finished.

kehao@kehaopcs ~/remote_exec $ ./remote_x86 exec
2024/02/04 16:59:37 start execute command...
2024/02/04 16:59:37 progress [2/3]...
2024/02/04 16:59:38 [172.18.0.3:22] execute (/bin/sh /home/kehao/test1.sh).
2024/02/04 16:59:38 [172.18.0.3:22] execute (/bin/sh /home/kehao/test1.sh) result: 
I am 172.18.0.3 :)
[root@5f0c9135f133 ~]# 
2024/02/04 16:59:38 [172.18.0.3:22] execute command finished!
2024/02/04 16:59:38 progress [1/3]...
2024/02/04 16:59:38 [172.18.0.4:22] execute (/bin/sh /home/kehao/test1.sh).
2024/02/04 16:59:38 [172.18.0.4:22] execute (/bin/sh /home/kehao/test1.sh) result: 
I am 172.18.0.4 :)
[root@05d92c6bf1a1 ~]# 
2024/02/04 16:59:38 [172.18.0.4:22] execute command finished!
2024/02/04 16:59:44 progress [0/3]...
2024/02/04 16:59:45 [127.0.0.1:23] execute (/bin/sh /home/kehao/test1.sh).
2024/02/04 16:59:45 [127.0.0.1:23] execute (/bin/sh /home/kehao/test1.sh) result: 
I am 172.18.0.5 :)
[root@1244ba6621fc ~]# 
2024/02/04 16:59:45 [127.0.0.1:23] execute command finished!
2024/02/04 16:59:45 execute command finished.

kehao@kehaopcs ~/remote_exec $ ./remote_x86 get
2024/02/04 16:59:51 start get file...
2024/02/04 16:59:51 progress [2/3]...
2024/02/04 16:59:51 [172.18.0.3:22] download file from /home/kehao/result1.txt to /home/kehao/result.
2024/02/04 16:59:51 [172.18.0.3:22] download /home/kehao/result1.txt finished!
2024/02/04 16:59:51 progress [1/3]...
2024/02/04 16:59:51 [172.18.0.4:22] download file from /home/kehao/result1.txt to /home/kehao/result.
2024/02/04 16:59:51 [172.18.0.4:22] download /home/kehao/result1.txt finished!
2024/02/04 16:59:53 progress [0/3]...
2024/02/04 16:59:53 [127.0.0.1:23] download file from /home/kehao/result1.txt to /home/kehao/result.
2024/02/04 16:59:53 [127.0.0.1:23] download /home/kehao/result1.txt finished!
2024/02/04 16:59:53 get file finished.

操作结果:

远端

kehao@kehaopcs ~/remote_exec $ docker exec -u kehao test-ssh1 bash -c 'ls -l ~'
total 24
-rw-r--r--. 1 kehao kehao 146 Feb  4 16:59 result1.txt
-rw-r--r--. 1 kehao kehao 146 Feb  4 16:59 result2.txt
-rw-rw-r--. 1 kehao kehao 319 Feb  4 16:59 test1.sh
-rw-rw-r--. 1 kehao kehao  10 Feb  4 16:59 test1.txt
-rw-rw-r--. 1 kehao kehao 319 Feb  4 16:59 test2.sh
-rw-rw-r--. 1 kehao kehao  10 Feb  4 16:59 test2.txt

本地:

kehao@kehaopcs ~/remote_exec $ ls -l /home/kehao/result/
total 64
-rw-r--r--. 1 kehao manager 146 Feb  4 16:59 127.0.0.1_23_result1.txt
-rw-r--r--. 1 kehao manager 146 Feb  4 16:59 172.18.0.3_22_result1.txt
-rw-r--r--. 1 kehao manager 146 Feb  4 16:59 172.18.0.4_22_result1.txt
kehao@kehaopcs ~/remote_exec $ cat /home/kehao/result/172.18.0.3_22_result1.txt
test1.sh
4c2db381a3d8
172.18.0.3
uid=0(root) gid=0(root) groups=0(root)
MIT License Copyright (c) 2023 chmodke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

shell批量执行工具 / shell batch execute tool 展开 收起
README
MIT
取消

发行版 (7)

全部
28天前

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chmodke/remote_exec.git
git@gitee.com:chmodke/remote_exec.git
chmodke
remote_exec
remote_exec
master

搜索帮助