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可能使用得比较少。

相关文章
|
18天前
|
Ubuntu Shell Linux
使用sshpass工具通过SSH执行远程命令
`sshpass`是一个用于在脚本中自动输入SSH密码的工具 **在Debian/Ubuntu上安装sshpass:** ```bash sudo apt-get install sshpass ``` **在CentOS/RHEL上安装sshpass:** ```bash sudo yum install sshpass ``` **在Fedora上安装sshpass:** ```bash sudo dnf install sshpass ``` 安装完成后,你可以使用`sshpass`命令通过SSH执行远程命令。以下是一个示例: ```bash sshpass -p 'yo
|
18天前
|
存储 安全 Linux
|
7天前
|
存储 Linux 网络安全
在 Linux 中通过 SSH 执行远程命令时,无法自动加载环境变量(已解决)
SSH远程执行命令时遇到“命令未找到”问题,原因是Linux登录方式不同导致环境变量加载差异。解决方案:将环境变量写入`/etc/profile.d/`下的文件,或手动在命令前加载环境变量,如`source /etc/profile`。
|
8天前
|
存储 监控 Ubuntu
Linux系统之GoAccess实时Web日志分析工具的基本使用
【5月更文挑战第22天】Linux系统之GoAccess实时Web日志分析工具的基本使用
24 1
|
9天前
|
Linux 测试技术 开发工具
Linux系统之advcpmv工具的安装和基本使用
【5月更文挑战第21天】Linux系统之advcpmv工具的安装和基本使用
18 2
|
12天前
|
IDE Linux 测试技术
Linux项目自动化构建工具-make/Makefile
Linux项目自动化构建工具-make/Makefile
|
18天前
|
安全 Linux Shell
|
18天前
|
安全 Linux Python
Volatility3内存取证工具安装及入门在Linux下的安装教程
Volatility3内存取证工具安装及入门在Linux下的安装教程
Volatility3内存取证工具安装及入门在Linux下的安装教程
|
18天前
|
数据可视化 小程序 Linux
【Linux】自动化构建工具make/Makefile和git介绍
【Linux】自动化构建工具make/Makefile和git介绍
19 0
|
18天前
|
安全 Linux 网络安全