Linux远程批量工具mooon_ssh和mooon_upload使用示例

简介: Linux远程批量工具mooon_ssh和mooon_upload使用示例.pdf 目录 目录 1 1. 前言 1 2. 批量执行命令工具:mooon_ssh 2 3.

img_e25d4fb2f8de1caf41a735ec53088516.pngLinux远程批量工具mooon_ssh和mooon_upload使用示例.pdf

目录

目录 1

1. 前言 1

2. 批量执行命令工具:mooon_ssh 2

3. 批量上传文件工具:mooon_upload 2

4. 使用示例 3

4.1. 使用示例1:上传/etc/hosts 3

4.2. 使用示例2:检查/etc/profile文件是否一致 3

4.3. 使用示例3:批量查看crontab 3

4.4. 使用示例4:批量清空crontab 3

4.5. 使用示例5:批量更新crontab 3

4.6. 使用示例6:取远端机器IP 3

4.7. 使用示例7:批量查看kafka进程(环境变量方式) 4

4.8. 使用示例8:批量停止kafka进程(参数方式) 5

5. 如何编译批量工具? 5

5.1. GO版本 5

5.2. C++版本 6

 

1. 前言

远程批量工具包含:

1) 批量命令工具mooon_ssh

2) 批量上传文件工具mooon_upload

3) 批量下载文件工具mooon_download

 

可执行二进制包下载地址:

https://github.com/eyjian/libmooon/releases

 

源代码包下载地址:

https://github.com/eyjian/libmooon/archive/master.zip

 

批量工具除由三个工具组成外,还分两个版本:

1) C++版本

2) GO版本

 

当前C++版本比较成熟,GO版本相当简略,但C++版本依赖C++运行时库,不同环境需要特定编译,而GO版本可不依赖CC++运行时库,所以不需编译即可应用到广泛的Linux环境。

 

使用简单,直接执行命令,即会提示用法,如C++版本:

mooon_ssh

parameter[-c]'s value not set

 

usage:

-h[]: Connect to the remote machines on the given hosts separated by comma, can be replaced by environment variable 'H', example: -h='192.168.1.10,192.168.1.11'

-P[36000/10,65535]: Specifies the port to connect to on the remote machines, can be replaced by environment variable 'PORT'

-u[]: Specifies the user to log in as on the remote machines, can be replaced by environment variable 'U'

-p[]: The password to use when connecting to the remote machines, can be replaced by environment variable 'P'

-t[60/1,65535]: The number of seconds before connection timeout

-c[]: The command is executed on the remote machines, example: -c='grep ERROR /tmp/*.log'

-v[1/0,2]: Verbosity, how much troubleshooting info to print

 

2. 批量执行命令工具:mooon_ssh

参数名

默认值

说明

-u

用户名参数,可用环境变量U替代

-p

密码参数,可用环境变量P替代

-h

IP列表参数,可用环境变量H替代

-P

22,可修改源码,编译为常用端口号

SSH端口参数,可用环境变量PORT替代

-c

在远程机器上执行的命令,建议单引号方式指定值,除非要执行的命令本身已经包含了单引号有冲突。使用双引号时,要注意转义,否则会被本地shell解释

-v

1

工具输出的详细度

3. 批量上传文件工具:mooon_upload

参数名

默认值

说明

-u

用户名参数,可用环境变量U替代

-p

密码参数,可用环境变量P替代

-h

IP列表参数,可用环境变量H替代

-P

22,可修改源码,编译为常用端口号

SSH端口参数,可用环境变量PORT替代

-s

以逗号分隔的,需要上传的本地文件列表,可以带相对或绝对目录

-d

文件上传到远程机器的目录,只能为单个目录

4. 使用示例

4.1. 使用示例1:上传/etc/hosts

mooon_upload -s=/etc/hosts -d=/etc

4.2. 使用示例2:检查/etc/profile文件是否一致

mooon_ssh -c='md5sum /etc/hosts'

4.3. 使用示例3:批量查看crontab

mooon_ssh -c='crontab -l'

4.4. 使用示例4:批量清空crontab

mooon_ssh -c='rm -f /tmp/crontab.empty;touch /tmp/crontab.empty'

mooon_ssh -c='crontab /tmp/crontab.emtpy'

4.5. 使用示例5:批量更新crontab

mooon_ssh -c='crontab /tmp/crontab.online'

4.6. 使用示例6:取远端机器IP

因为awk用单引号,所以参数“-c”的值不能使用单引号,所以内容需要转义,相对其它来说要复杂点:

mooon_ssh -c="netstat -ie | awk -F[\\ :]+ 'BEGIN{ok=0;}{if (match(\$0, \"eth1\")) ok=1; if ((1==ok) && match(\$0,\"inet\")) { ok=0; if (7==NF) printf(\"%s\\n\",\$3); else printf(\"%s\\n\",\$4);} }'"

 

不同的环境,IP在“netstat -ie”输出中的位置稍有不同,所以awk中加了“7==NF”判断,但仍不一定适用于所有的环境。需要转义的字符包含:双引号、美元符和斜杠。

4.7. 使用示例7:批量查看kafka进程(环境变量方式)

$ export H=192.168.31.9,192.168.31.10,192.168.31.11,192.168.31.12,192.168.31.13

$ export U=kafka

$ export P='123456'

 

mooon_ssh -c='/usr/local/jdk/bin/jps -m'

[192.168.31.15]

50928 Kafka /data/kafka/config/server.properties

125735 Jps -m

[192.168.31.15] SUCCESS

 

[192.168.31.16]

147842 Jps -m

174902 Kafka /data/kafka/config/server.properties

[192.168.31.16] SUCCESS

 

[192.168.31.17]

51409 Kafka /data/kafka/config/server.properties

178771 Jps -m

[192.168.31.17] SUCCESS

 

[192.168.31.18]

73568 Jps -m

62314 Kafka /data/kafka/config/server.properties

[192.168.31.18] SUCCESS

 

[192.168.31.19]

123908 Jps -m

182845 Kafka /data/kafka/config/server.properties

[192.168.31.19] SUCCESS

 

 

================================

[192.168.31.15 SUCCESS] 0 seconds

[192.168.31.16 SUCCESS] 0 seconds

[192.168.31.17 SUCCESS] 0 seconds

[192.168.31.18 SUCCESS] 0 seconds

[192.168.31.19 SUCCESS] 0 seconds

SUCCESS: 5, FAILURE: 0

 

4.8. 使用示例8:批量停止kafka进程(参数方式)

$ mooon_ssh -c='/data/kafka/bin/kafka-server-stop.sh-u=kafka -p='123456' -h=192.168.31.15,192.168.31.16,192.168.31.17,192.168.31.18,192.168.31.19

[192.168.31.15]

No kafka server to stop

command return 1

 

[192.168.31.16]

No kafka server to stop

command return 1

 

[192.168.31.17]

No kafka server to stop

command return 1

 

[192.168.31.18]

No kafka server to stop

command return 1

 

[192.168.31.19]

No kafka server to stop

command return 1

 

================================

[192.168.31.15 FAILURE] 0 seconds

[192.168.31.16 FAILURE] 0 seconds

[192.168.31.17 FAILURE] 0 seconds

[192.168.31.18 FAILURE] 0 seconds

[192.168.31.19 FAILURE] 0 seconds

SUCCESS: 0, FAILURE: 5

5. 如何编译批量工具?

5.1. GO版本

依赖的crypto包,从https://github.com/golang/crypto下载,放到目录$GOPATH/src/golang.org/x$GOROOT/src/golang.org/x下。注意需要先创建好目录$GOROOT/src/golang.org/x,然后在此目录下解压crypto包。如果下载的包名为crypto-master.zip,则解压后的目录名为crypto-master,需要重命名为crypto

安装crypto包示例:

1)安装go

cd /usr/local

tar xzf go1.10.3.linux-386.tar.gz

2)mkdir -p go/golang.org/x

3)cd go/golang.org/x

4)unzip crypto-master.zip

5)mv crypto-master crypto

 

命令行执行“go help gopath”可了解gopath,或执行“go env”查看当前的设置。编译方法:

go build -o mooon_ssh mooon_ssh.go

 

上述编译会依赖glibc,如果不想依赖,这样编译:

go build -o mooon_ssh -ldflags '-linkmode "external" -extldflags "-static"' mooon_ssh.go

5.2. C++版本

C++版本为libmooon组成部分,编译libmooon即可得到mooon_sshmooon_uploadmooon_download。但libmooon依赖libssh2,而libssh2又依赖openssl,所以需要先依次安装好openssllibssh2

libssh2下载地址:http://www.libssh2.org

 

1) openssl编译安装方法

解压后进入openssl源码目录,以版本openssl-1.0.2i为例,依次执行:

./config --prefix=/usr/local/openssl-1.0.2i shared threads

make

make install

ln -s /usr/local/openssl-1.0.2i /usr/local/openssl

 

2) libssh2编译安装方法

解压后进入libssh2源码目录,以版本libssh2-1.6.0为例,依次执行:

./configure --prefix=/usr/local/libssh2-1.6.0 --with-libssl-prefix=/usr/local/openssl

make

make install

 

注意:libssh2和比较新版本的openssl可能存在兼容问题。

 

3) libmooon编译方法

采用cmake编译,所以需要先安装好cmake,并要求cmake版本不低于2.8.11,可执行“cmake --version”查看cmake版本,当cmakelibssh2openssl准备好后执行下列命令编译libmooon即可得到批量工具:

cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local/mooon .

make

make install

 

make一步成功后,即可在tools子目录中找到mooon_sshmooon_uploadmooon_download,实践中mooon_download可能使用得比较少。

相关文章
|
7天前
|
安全 Linux Shell
Linux中SSH命令介绍
Linux中SSH命令介绍
18 2
|
2天前
|
JavaScript Linux
【详细讲解】Linux grep命令用法大全 片尾有示例搜索指定目录中指定文件后缀的指定字符
【详细讲解】Linux grep命令用法大全 片尾有示例搜索指定目录中指定文件后缀的指定字符
10 1
|
7天前
|
网络协议 Linux 网络安全
Linux配置SSH允许TCP转发
Linux配置SSH允许TCP转发
12 1
|
12天前
|
存储 Prometheus 监控
Linux技术工具:bpftrace介绍
Linux技术工具:bpftrace介绍
20 7
|
12天前
|
安全 Linux Shell
【Linux基础】SSH登录
安全外壳协议(Secure Shell Protocol,简称SSH)是一种加密的网络传输协议,可在不安全的网络中为网络服务提供安全的传输环境。 SSH通过在网络中建立安全隧道来实现SSH客户端与服务器之间的连接。 SSH最常见的用途是远程登录系统,人们通常利用SSH来传输命令行界面和远程执行命令。
26 6
|
13天前
|
Shell Linux 网络安全
Linux怎样在使用ssh 链接时就指定gcc 的版本
Linux怎样在使用ssh 链接时就指定gcc 的版本
20 7
|
11天前
|
Shell Linux 数据安全/隐私保护
蓝易云 - Linux中的chsh命令及示例
注意:只有拥有适当权限的用户(如root用户)才能更改其他用户的登录shell。普通用户只能更改自己的登录shell。
14 3
|
14天前
|
监控 算法 Linux
探索Linux中的lz4命令:高效的数据压缩工具
**探索Linux中的LZ4工具:快速数据压缩。LZ4算法提供高速压缩与解压缩,适合实时数据处理。命令行工具如`lz4c`用于文件压缩(`lz4c file.txt compressed.lz4`)和解压缩(`lz4c -d compressed.lz4 decompressed.txt`)。特点是速度快、低内存占用,可选压缩级别。注意命令的实际形式取决于安装的实现,使用前应查阅文档。**
|
11天前
|
Linux 数据处理
Linux中的numfmt命令:数字格式化的强大工具
**numfmt命令在Linux中用于数字格式化,如转换进制、添加千位分隔符、处理字节单位。它支持从文件读取数字并能自定义分隔符、小数位数。例如:`numfmt 12345` 输出12,345(十进制),`numfmt -b 255` 输出11111111(二进制),`numfmt --to=iec 1000000` 输出976.6K(字节单位)。使用时注意选项组合及单位标准。**
|
15天前
|
监控 安全 Linux
【权限维持】Linux&OpenSSH&PAM后门&SSH软链接&公私钥登录
【权限维持】Linux&OpenSSH&PAM后门&SSH软链接&公私钥登录