自动化运维之pdsh

pdsh,与pssh类似,pdsh可并行执行对远程目标主机的操作,在有批量执行命令或分发任务的运维需求时,使用这个命令可达到事半功倍的效果。同时,pdsh还可支持交互模式,当要执行的命令不确定时,可直接进入pdsh命令行。

官网https://code.google.com/p/pdsh/

安装

1
2
3
4
5
6
1.tar jxvf pdsh-2.29.tar.bz2   
2.cd pdsh-2.29
3../configure --with-ssh --with-rsh --with-mrsh --with-mqshell --with-qshell --with-dshgroups --with-machines=/etc/pdsh/machines
4.make && make install
--with-dshgroups : 启用主机组支持
--with-machines : 是--with-dshgroups的扩展,通过所有要管理的主机列表都写入指定的文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1.pdsh -V  
2.pdsh-2.29
3.rcmd modules: ssh,rsh,exec (default: rsh)
4.misc modules: machines,dshgroup
5.
6.pdsh -h
7.Usage: pdsh [-options] command ...
8.-S return largest of remote command return values
9.-h output usage menu and quit
10.-V output version information and quit #查看版本以及可用的模块
11.-q list the option settings and quit #列出pdsh执行时的一些配置信息
12.-b disable ^C status feature (batch mode)
13.-d enable extra debug information from ^C status
14.-l user execute remote commands as user #指定远程主机上使用的用户名。pdsh -R ssh -l opsuser -w user00[1-10] "date"
15.-t seconds set connect timeout (default is 10 sec) #指定连接远程主机的超时时间,以s为单位,默认是10s。pdsh -R ssh -w slave00[1-9] -t 15 "date"
16.-u seconds set command timeout (no default) #指定远程执行命令的超时时间,以s为单位,以ssh方式连接时,默认时间为无限
17.-f n use fanout of n nodes #设置同时连接到远程主机的数量
18.-w host,host,... set target node list on command line #指定远程主机,可以指定多台,每台主机用逗号隔开,host可以是主机名也可以是IP地址。eg: pdsh -w ssh:user001,ssh:user002 "date" 或者 pdsh -w ssh:user00[1-10] "date" 或者使用正则 pdsh -w ssh:user00[10-31],/1$/ "uptime" ,10-31中选择以1结尾的主机
19.-x host,host,... set node exclusion list on command line #排除某些主机。pdsh -R ssh -l opsuser -w user00[1-9] -x user005,user007 "date"
20.-R name set rcmd module to name #指定rcmd的模块名,默认是rsh。如果要选择sshpdsh -R ssh -w user00[1-10] "date"
21.-M name,... select one or more misc modules to initialize first
22.-N disable hostname: labels on output lines #用来关闭远程主机所返回结果的主机名显示
23.-L list info on all loaded modules and exit
24.-g groupname target hosts in dsh group "groupname" #用来指定一组远程主机,编译pdsh时可以通过--with-dshgroups 参数激活此选项,默认可以将一组主机列表写入一个文件并放到本地主机的~/.dsh/group或/etc/dsh/group目录下。eg: pdsh -R ssh -g userhosts "date",其中userhosts是一个主机列表文件,可以放到~/.dsh/group或/etc/dsh/group目录下
25.-X groupname exclude hosts in dsh group "groupname" #用来排除指定组内的所有主机,经常与-a参数一起使用。pdsh -R ssh -a -X userhosts "date"
26.-a target all nodes #可以指定所有的远程主机,pdsh会查看/etc/pdsh/machines文件中的主机列表,要改变此路径,在编译pdsh时通过--with-machines参数指定
27.available rcmd modules: ssh,rsh,exec (default: rsh)

1、pdsh批量统计主机信息

1.pdsh -w ssh:192.168.1.153,ssh:192.168.1.222 “uname -n”
2.192.168.1.222: localhost.localdomain
3.192.168.1.153: local-centos02

排除某些主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.pdsh -w ssh:192.168.1.[153-222] -x 192.168.1.[154-221] "uptime"  
2.192.168.1.153: 11:46:57 up 2:42, 2 users, load average: 0.00, 0.00, 0.00
3.192.168.1.222: 11:46:55 up 3 days, 3:26, 3 users, load average: 0.00, 0.01, 0.00

支持正则

1.pdsh -w ssh:192.168.1.[153-222],/2$/ "uptime"

对于不规范的主机,可以写到一个文件中,路径是编译时指定

1.cat /etc/pdsh/machines
2.aaa
3.bbb
4.user001
5.user1111
6.
7.pdsh -R ssh -a uptime

按照组进行调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1.cat /etc/dsh/group/userhosts  
2.
3.pdsh -R ssh -g userhosts uptime
4.
5.-X #排除指定组的主机
6.
7.pdsh -R ssh -X userhosts uptime
8.
9.pdsh 在远程主机上执行命令
10.
11.pdsh -R ssh -g userhosts "rm -rf /home/opsuser/mysql"
12.pdsh -R ssh -g userhosts "sudo mkdir /mnt/test"
13.pdsh -R ssh -g userhosts "sudo /etc/init.d/gmond start"

pdsh 交互模式

1.pdsh -R ssh -w 192.168.1.153

pdcp应用

pdcp主要是本地主机和远程主机进行文件复制,在使用pdcp时,两台主机都要安装pdcp


1.pdcp -R ssh -g userhosts /home/opsuser/mysqldb.tar.gz /home/opsuser #复制文件
2.pdcp -R ssh -w userhosts -r /home/opsuser/webdata /home/opsuser #复制目录