Hi!请登陆

使用pssh工具实现批量管理

2020-10-27 54 10/27

1,本地服务器已做了公私钥 ssh-gen

2, 安装*python*

yum install *python*

如安装setuptools包 不成功:

yum install python-setuptools.noarch

3, 下载最新版本

wget http://www.theether.org/pssh/pssh-1.4.3.tar.gz
tar zxvf pssh-1.4.3.tar.gz
cd pssh-1.4.3
python setup.py install

4,在 other.txt 输入已经copy好 公钥的服务器名

$ cat other.txt
192.168.2.241
192.168.2.148:22 (端口不写默认为22)

5,安装成功直接执行命令

Parallel ssh (pssh) 批量登陆操作某一命令或脚本
Parallel scp (pscp) 批量copy
Parallel rsync (prsync) 批量同步
Parallel nuke (pnuke) 批量killall 某一进程
Parallel slurp (pslurp) 批量远程copy文件至当前服务器

例:pscp:

将文件lighttpd-1.4.30.tar.gz 复制到远程各服务器的/usr/local目录下.

pscp -h other.txt -l root /opt/lighttpd-1.4.30.tar.gz /usr/local/
[1] 11:26:43 [SUCCESS] 192.168.2.241 22
[2] 11:26:43 [SUCCESS] 192.168.2.148 22

例:pssh:

在各服务器上执行 hostname查看 将结果输入到 success 目录下.

pssh -h other.txt -l root -o success -e error hostname
[1] 11:21:37 [SUCCESS] 192.168.2.241 22
[2] 11:21:37 [SUCCESS] 192.168.2.148 22
opt]# pssh -h other.txt -l root -i hostname
[1] 11:39:27 [SUCCESS] 192.168.2.241 22
testrhelclone.com
[2] 11:39:27 [SUCCESS] 192.168.2.148 22
testcobb.com
pssh -h other.txt -l root -i sh /opt/aa.sh #直接执行远程脚本
[1] 14:44:16 [SUCCESS] 192.168.2.241 22
pssh this is ok 241
14:42:33
[2] 14:44:16 [SUCCESS] 192.168.2.148 22
pssh this is ok 148
14:42:33

例:prsync:

将本地的other.txt 同步到远程的/opt/other1.txt.

prsync -r -h other.txt -l root other.txt /opt/other1.txt
[1] 11:51:55 [SUCCESS] 192.168.2.148 22
[2] 11:51:55 [SUCCESS] 192.168.2.241 22

将success压缩传输:

prsync -r -az -h other.txt -v -l root success /opt/
[1] 13:40:49 [SUCCESS] 192.168.2.241 22
[2] 13:40:49 [SUCCESS] 192.168.2.148 22

例:pslurp:

pslurp 与pscp方向相反!

Usage: pslurp [OPTIONS] -h hosts.txt -o outdir remote local
-r --recursive recusively copy directories (OPTIONAL)
-L --localdir output directory for remote file copies
-h --hosts hosts file (each line "host[:port] [login]")
-l --user username (OPTIONAL)
-p --par max number of parallel threads (OPTIONAL)
-o --outdir output directory for stdout files (OPTIONAL)
-e --errdir output directory for stderr files (OPTIONAL)
-t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL)
-v --verbose turn on warning and diagnostic messages (OPTIONAL)
-O --options SSH options (OPTIONAL)
Example: pslurp -h hosts.txt -L /tmp/outdir -l irb2
/home/irb2/foo.txt foo.txt

将远程服务器的各个/opt/success 目录递归 拷贝到本地的 /opt/test/ 目录下:

pslurp -r -h other.txt -L /opt/test/ /opt/success remote
[1] 14:19:45 [SUCCESS] 192.168.2.241 22
[2] 14:19:45 [SUCCESS] 192.168.2.148 22
[root@testrhel opt]# cd test/
[root@testrhel test]# ls
192.168.2.148 192.168.2.241
[root@testrhel test]# tree *
192.168.2.148
`-- remote
|-- 192.168.2.148
|-- 192.168.2.241
|-- aa.txt
`-- daf.d
192.168.2.241
`-- remote
|-- 192.168.2.148
|-- 192.168.2.241
|-- aa.txt
`-- inotify.sh

例:pnuke:

将远程服务器中的所有以root启动的sendmail 服务关闭 (只要ps aux到 sendmail 就强行关闭 如同 killall命令).

pnuke -h /opt/other.txt -l root sendmail
[1] 14:29:58 [SUCCESS] 192.168.2.148 22
[2] 14:29:58 [SUCCESS] 192.168.2.241 22

其它:

1,如果过程中需要交互,如询问yes/no的时候,可以使用以下方式来解决.

pssh -i -h /opt/other.txt "echo y | sudo yum remove xxxx"

这样的话,yum询问是否安全的时候,就可以自动确认了.

2,添加环境变量,让命令更简洁

export PSSH_HOSTS="/opt/other.txt"
pssh -i hostname
[1] 15:04:06 [SUCCESS] 192.168.2.148 22
testcobb.com
[2] 15:04:06 [SUCCESS] 192.168.2.241 22
pssh -h ip1.txt -i hostname 也可另指目录文件
[1] 15:05:35 [SUCCESS] 192.168.2.148 22
testcobb.com
[2] 15:05:35 [SUCCESS] 192.168.2.241 22
testrhelclone.com

官网给出的一些环境变量名,可相应添加!

# export PSSH_HOSTS="/x/bnc/ips.txt"
# export PSSH_USER="irb2"
# export PSSH_PAR="32"
# export PSSH_OUTDIR="/tmp/bar"
# export PSSH_VERBOSE="0"
# export PSSH_OPTIONS="UserKnownHostsFile /tmp/known_hosts"
Tag:

相关推荐