Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8053461
  • 博文数量: 594
  • 博客积分: 13065
  • 博客等级: 上将
  • 技术积分: 10324
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 16:44
个人简介

推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html

文章分类

全部博文(594)

分类: 系统运维

2018-09-09 18:09:32

目录

 

1. 前言

远程批量工具包含:

1) 批量命令工具mooon_ssh

2) 批量上传文件工具mooon_upload

3) 批量下载文件工具mooon_download

 

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

 

源代码包下载地址:

 

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

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

工具输出的详细度

-thr

1

线程数,当线程数大于2时,并发执行;如果值为0,表示线程数和IP数相同

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

参数名

默认值

说明

-u

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

-p

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

-h

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

-P

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

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

-s

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

-d

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

-thr

1

线程数,当线程数大于2时,并发执行;如果值为0,表示线程数和IP数相同

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包,从下载,放到目录$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下载地址:

 

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

 

阅读(5965) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

Aquester2018-09-12 20:56:18

参数“-thr”指定线程数