A, pssh Introduction and Installation
First, we have to understand what is at pssh, it is doing with? pssh is a tool written in python can execute commands on multiple servers, file replication can be achieved. In other words, a batch is a small tool management of multiple servers. We often have this experience, is the need to execute the same command on many hosts, in the past, we need to write a script, and then write a loop, and then write the command to be executed, I feel very troublesome, with pssh this tool, a lot of things we do not need to write scripts to complete, and with pssh manage multiple hosts, as if in their own host command execution, we will not write a script you can manage multiple hosts. Next we come to install it pssh
1, see the package information pssh
[Root @ test ~] # yum info pssh Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile Available Packages Name: pssh Arch : noarch Version : 2.3.1 Release : 5.el6 Size : 50 k Repo: warm Summary : Parallel SSH tools URL : http://code.google.com/p/parallel-ssh/ License : BSD Description : This package provides various parallel tools based on ssh and scp. : Parallell version includes: : o ssh : pssh : A scp: pscp : O nuke: pnuke : o rsync : prsync : O slurp: pslurp [root@test ~]#
Description: This package can be seen pssh main function is to provide a variety of ssh and scp-based command-line tool, package includes pssh, pscp, pnuke, prsync, pslurp, these five tools. This package source epel, so before you are asked to check for and install with their own epel source.
2, the installation package pssh
[root@test ~]# yum install -y pssh
Second, the use of tools pssh
pssh command options are as follows:
--version: View version information
[Root @ test ~] # pssh --version 2.3.1 [root@test ~]#
-h: Host file list, content format "[user @] host [: port]"
[root@docker test]#cat > iplist << EOF > [email protected] > [email protected] > [email protected] > EOF [root@docker test]#cat iplist [email protected] [email protected] [email protected] [root@docker test]#pssh -h iplist 'ls -l' [1] 22:08:50 [SUCCESS] [email protected] [2] 22:08:50 [SUCCESS] [email protected] [3] 22:08:50 [SUCCESS] [email protected] [root@docker test]#
Note: the meaning of the above command is executed ls -l command on my iplist defined host, to say here, pssh default is no password on the command key authentication to connect to remote host, so I'm in the implementation of the above.
-H: host string content format "[user @] host [: port]".
[root@docker ~]#pssh -H 192.168.0.128 'ls /' [1] 22:17:40 [SUCCESS] 192.168.0.128 [root@docker ~]#
Description: -H option is to specify a host, of course, you want to specify multiple hosts, can all be specified -H, many hosts can also be specified with -H, but each host separated by a space, and multiple hosts enclosed in quotation marks.
-A: manual input mode password
[root@docker ~]#pssh -H [email protected]:41319 -A 'ls' Warning: do not enter your password if anyone else has superuser privileges or access to your account. Password: [1] 22:40:20 [SUCCESS] [email protected]:41319 [root@docker ~]#
Description: pssh default ssh key authentication is used to execute the command, if we do not manage to verify SSHkey host, then we can use -A to specify manually enter a password mode, based on the relevant ssh key authentication login, please refer to my blog https : //www.cnblogs.com/qiuhom-1874/p/11783371.html
-i: each internal server processes information output
[root@docker ~]#pssh -h test/iplist -i 'ip addr show' [1] 22:44:31 [SUCCESS] [email protected] 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link / ether 00: 0c: 29: 41: ff: ff c0 brd: ff: ff: ff: ff: ff inet 192.168.0.128/24 brd 192.168.0.255 scope global eth1 inet6 fe80::20c:29ff:fe41:ffc0/64 scope link valid_lft forever preferred_lft forever [2] 22:44:31 [FAILURE] [email protected] Exited with error code 127 Stderr: bash: ip: command not found [3] 22:44:31 [SUCCESS] [email protected] 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:98:0a:46 brd ff:ff:ff:ff:ff:ff inet 192.168.0.217/24 brd 192.168.0.255 scope global dynamic ens33 valid_lft 4184sec preferred_lft 4184sec inet6 fe80::f565:ff52:6ab3:b9da/64 scope link valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000 link/ether 52:54:00:f8:82:a0 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000 link/ether 52:54:00:f8:82:a0 brd ff:ff:ff:ff:ff:ff [root@docker ~]#
Note: We do not specify the option -i, pssh default output only execute commands success or failure status, the result of the implementation of the information will not be printed. Use -i we can execute commands on the results of each host and the command is successful or not displayed together. There are no top on a host centos6 mounted iproute package so no ip default this command, so the command is not executed successfully.
-L log in using the username
[root@docker ~]#pssh -H 192.168.0.218 -l qiuhom -i 'whoami' [1] 22:49:46 [SUCCESS] 192.168.0.218 qiuhom [root@docker ~]#pssh -H 192.168.0.218 -l root -i -A 'whoami' Warning: do not enter your password if anyone else has superuser privileges or access to your account. Password: [1] 22:50:03 [SUCCESS] 192.168.0.218 root [root@docker ~]#
Description: -l option is to specify that the user to execute commands, the specified user is a user on the remote host. Typically -l (lowercase L) used in conjunction with -A.
-o: the output file directory
[root@docker ~]#pssh -o /root/dr1/ -h test/iplist 'ls /' [1] 23:02:44 [SUCCESS] [email protected] [2] 23:02:44 [SUCCESS] [email protected] [3] 23:02:44 [SUCCESS] [email protected] [root@docker ~]#tree /root/dr1/ /root/dr1/ ├── [email protected] ├── [email protected] └── [email protected] 0 directories, 3 files [root@docker ~]#cat /root/dr1/[email protected] bin boot dev etc home lib lib64 lost+found media misc mnt net opt proc root sbin selinux srv sys tmp usr where [root@docker ~]#
Description: -o specifying the output command to the host-named file storage places. That is the result of command execution is stored in a host-named file, -o is to specify a directory to store these files.
-t: TIMEOUT timeout, 0 unlimited
[root@docker ~]#pssh -t 3 -h test/iplist -i 'ping 8.8.8.8' [1] 23:09:39 [FAILURE] [email protected] Timed out, Killed by signal 9 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=53.5 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=52 time=53.6 ms 64 bytes from 8.8.8.8: icmp_seq=4 ttl=52 time=53.5 ms [2] 23:09:39 [FAILURE] [email protected] Timed out, Killed by signal 9 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=54.2 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=52 time=53.8 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=52 time=53.4 ms [3] 23:09:39 [FAILURE] [email protected] Timed out, Killed by signal 9 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=53.2 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=52 time=53.9 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=52 time=53.6 ms [root@docker ~]#
These are the common options pssh tool description and usage
Three, pscp.pssh command options Description and use
pscp.pssh function is to bulk copy a local file to a remote host
Command usage:
pscp.pssh [-vAr] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par] [-o outdir] [-e errdir] [-t timeout] [-O options] [-x args] [-X arg] local remote
Description: pscp.pssh pssh more than a lot of options and usage is the same, such as -A -H -h -l etc.
-v: displays the replication process
-r: recursive copy directories
[root@docker ~]#pscp.pssh -r -h test/iplist scripts/ /home/ [1] 23:27:57 [SUCCESS] [email protected] [2] 23:27:57 [FAILURE] [email protected] Exited with error code 1 [3] 23:27:57 [SUCCESS] [email protected] [root@docker ~]#pssh -h test/iplist -i 'ls /home/' [1] 23:28:30 [SUCCESS] [email protected] abc admin scripts tom [2] 23:28:30 [SUCCESS] [email protected] qiuhom testuser1 [3] 23:28:31 [SUCCESS] [email protected] roo scripts tom [root@docker ~]#
Note: You can see that we specify the directory recursively copied to the target host, and there on top of a host fails, because an ordinary user to do SSH key authentication on the host, we are using qiuhom the ordinary user's identity in management on the remote host, because qiuhom the common users / home / directory is not writable, so execution failed.
Example:
The local copy of a single file to the remote host
[root@docker ~]#pssh -H [email protected] -i 'ls -l /home/qiuhom/' [1] 23:39:53 [SUCCESS] [email protected] 0 Total amount [root@docker ~]#pscp.pssh -H [email protected] /etc/fstab /home/qiuhom/ [1] 23:40:26 [SUCCESS] [email protected] [root@docker ~]#pssh -H [email protected] -i 'ls -l /home/qiuhom/' [1] 23:40:29 [SUCCESS] [email protected] The total amount of 4 RW r - r--. Qiuhom qiuhom 1 552 11 月 8 23:40 fstab [root@docker ~]#
Copy multiple local files to the remote host
[root@docker ~]#pssh -H [email protected] -i 'ls -l /home/qiuhom/' [1] 23:40:29 [SUCCESS] [email protected] The total amount of 4 RW r - r--. Qiuhom qiuhom 1 552 11 月 8 23:40 fstab [root@docker ~]#pscp.pssh -H [email protected] /root/f1 /etc/inittab /var/log/messages /home/qiuhom/ [1] 23:42:01 [SUCCESS] [email protected] [root@docker ~]#pssh -H [email protected] -i 'ls -l /home/qiuhom/' [1] 23:42:06 [SUCCESS] [email protected] The total amount of 248 -rw-r--r--. 1 qiuhom qiuhom 18 11月 8 23:41 f1 RW r - r--. Qiuhom qiuhom 1 552 11 月 8 23:40 fstab -rw-r--r--. 1 qiuhom qiuhom 511 11月 8 23:41 inittab -rw-------. 1 qiuhom qiuhom 239787 11月 8 23:41 messages [root@docker ~]#
The bulk copy to the local directory on the remote host
[root@docker ~]#pssh -H 192.168.0.128 -i 'ls -l /home/' [1] 23:51:31 [SUCCESS] 192.168.0.128 0 Total amount [root@docker ~]#pscp.pssh -H 192.168.0.128 -r /etc/sysconfig/network-scripts/ /home/ [1] 23:52:32 [SUCCESS] 192.168.0.128 [root@docker ~]#pssh -H 192.168.0.128 -i 'ls -l /home/' [1] 23:52:53 [SUCCESS] 192.168.0.128 The total amount of 4 drwxr-xr-x 2 root root 4096 10月 25 03:36 network-scripts [root@docker ~]#
A four, pslurp command
Pslurp command function is to host remote batch of files copied to the local
Command usage:
pslurp [-vAr] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par][-o outdir] [-e errdir] [-t timeout] [-O options] [-x args] [-X arg] [-L localdir] remote local(本地名)
Description: Usage and pscp.pssh of this tool usage is very similar, are the operation of the document, pscp.pssh is to push on the remote host, pslurp got me a file from a remote host to the local.
-L: Specifies downloaded from a remote host to the storage directory of the present machine, local name is downloaded to the local post
-r: recursive copy directories
Example:
Batch download files to the target server's passwd / root under, and named user
[root@docker ~]#ll The total amount of 4 drwxr-xr-x 2 root root 24 8月 31 18:04 ansible drwxr-xr-x 7 root root 95 7月 11 11:21 docker-training drwxr-xr-x 2 root root 86 11月 8 23:02 dr1 -rw-r--r-- 1 root root 18 11月 8 23:20 f1 drwxr-xr-x 2 root root 42 10月 24 21:51 scripts drwxr-xr-x 2 root root 47 7月 22 10:11 temp drwxr-xr-x 2 root root 100 11月 8 23:20 test [root@docker ~]#pslurp -h test/iplist -L /root/ /etc/passwd user [1] 00:03:00 [SUCCESS] [email protected] [2] 00:03:00 [SUCCESS] [email protected] [3] 00:03:04 [SUCCESS] [email protected] [root@docker ~]#ls 192.168.0.128 192.168.0.217 192.168.0.218 ansible docker-training dr1 f1 scripts temp test [root@docker ~]#ll 192.168.0.128/ The total amount of 4 -rw-r--r-- 1 root root 1262 11月 9 00:03 user [root@docker ~]#cat 192.168.0.128/user root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown stop: x: 7: 0: wait: / sbin: / sbin / halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin abc:x:500:500::/home/abc:/bin/bash admin:x:501:501::/home/admin:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash apache:x:48:48:Apache:/var/www:/sbin/nologin tom:x:502:502::/home/tom:/bin/bash [root@docker ~]#
Description: -L is specified remote host to store downloaded files are stored, each host files are in a directory name of the remote host ip name placed in its designated local directory, the file name we specify the file name.
The directory on the remote host /etc/profile.d/ downloaded to the local host 192.168.0.128
[root@docker test]#ll The total amount of 4 -rw-r--r-- 1 root root 59 11月 8 22:04 iplist [root@docker test]#pslurp -r -H 192.168.0.128 /etc/profile.d/ . [1] 00:30:09 [SUCCESS] 192.168.0.128 [root@docker test]#tree . ├── 192.168.0.128 │ └── profile.d │ ├── colorls.csh │ ├── colorls.sh │ ├── cvs.csh │ ├── cvs.sh │ ├── glib2.csh │ ├── glib2.sh │ ├── lang.csh │ ├── lang.sh │ ├── less.csh │ ├── less.sh │ ├── modules.csh │ ├── modules.sh │ ├── vim.csh │ ├── vim.sh │ └── which2.sh └── iplist 2 directories, 16 files
Note: If you do not specify a file stored in the local name, the default is stored under the current directory to the remote host ip name of the directory, the file name we downloaded the file name on the remote host.
Use the 5, pnuke command
Pnuke command function is the ability to kill processes in parallel on multiple hosts programs
Command usage:
pnuke [-vA] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par] [-o outdir] [-e errdir] [-t timeout] [-O options] [-x args] [-X arg] pattern
Note: The above options with consistent usage options pssh
Example:
Kill the remote host httpd process
[root@docker test]#pssh -h iplist -i 'ss -ntl |grep 80' [1] 01:17:48 [SUCCESS] [email protected] LISTEN 0 128 :::80 :::* [2] 01:17:48 [SUCCESS] [email protected] LISTEN 0 128 :::80 :::* [3] 01:17:48 [SUCCESS] [email protected] LISTEN 0 128 :::80 :::* [root@docker test]#pnuke -h iplist httpd [1] 01:18:18 [SUCCESS] [email protected] [2] 01:18:19 [SUCCESS] [email protected] [3] 01:18:19 [SUCCESS] [email protected] [root@docker test]#pssh -h iplist -i 'ss -ntl |grep 80' [1] 01:18:23 [FAILURE] [email protected] Exited with error code 1 [2] 01:18:23 [FAILURE] [email protected] Exited with error code 1 [3] 01:18:23 [FAILURE] [email protected] Exited with error code 1 [root@docker test]#
Note: You can see our filter command httpd is listening on port 80 to perform state failure, indicating that there is no process listening on port 80 on a remote host.
A six, prsync command
Prsync command function is to copy files to multiple hosts in parallel
Command usage:
prsync [-vAraz] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par] [-o outdir] [-e errdir] [-t time‐out] [-O options] [-x args] [-X arg] [-S args] local remote
Note: The above options with pssh command similar option.
Example:
Copy the file to the server
[root@docker test]#pssh -h iplist -i 'ls -l /root/' [1] 01:35:19 [SUCCESS] [email protected] The total amount of 4 -rw-r--r-- 1 root root 552 10月 25 05:19 fs [2] 01:35:19 [SUCCESS] [email protected] The total amount of 4 -rw-r--r--. 1 root root 552 11月 9 01:34 fs [3] 01:35:19 [SUCCESS] [email protected] The total amount of 4 -rw-r--r--. 1 root root 552 11月 9 01:35 fs [root@docker test]#prsync -h iplist /etc/inittab /root/init/ [1] 01:35:52 [SUCCESS] [email protected] [2] 01:35:52 [SUCCESS] [email protected] [3] 01:35:52 [SUCCESS] [email protected] [root@docker test]#pssh -h iplist -i 'ls -l /root/' [1] 01:36:01 [SUCCESS] [email protected] The total amount of 8 -rw-r--r-- 1 root root 552 10月 25 05:19 fs drwxr-xr-x 2 root root 4096 10月 25 05:19 init [2] 01:36:01 [SUCCESS] [email protected] The total amount of 8 -rw-r--r--. 1 root root 552 11月 9 01:34 fs drwxr-xr-x. 2 root root 4096 11月 9 01:35 init [3] 01:36:01 [SUCCESS] [email protected] The total amount of 4 -rw-r--r--. 1 root root 552 11月 9 01:35 fs drwxr-xr-x. 2 root root 21 11月 9 01:35 init [root@docker test]#
Description: Similar prsync usage and pscp.pssh usage, it is that the difference between the two, pscp.pssh does not support creating a directory (if you do not specify a directory, created on the remote server), and prsync support. pscp.pssh support various local copy multiple files at the same time to a remote server, and prsync not supported.
Copy the directory to the server
[root@docker test]#pssh -h iplist -i 'rm -rf /root/*' [1] 01:45:21 [SUCCESS] [email protected] [2] 01:45:21 [SUCCESS] [email protected] [3] 01:45:21 [SUCCESS] [email protected] [root@docker test]#pssh -h iplist -i 'ls -l /root/' [1] 01:45:38 [SUCCESS] [email protected] 0 Total amount [2] 01:45:38 [SUCCESS] [email protected] 0 Total amount [3] 01:45:38 [SUCCESS] [email protected] 0 Total amount [root@docker test]#prsync -r -h iplist /root/scripts/ /root/aaa/ [1] 01:46:20 [SUCCESS] [email protected] [2] 01:46:21 [SUCCESS] [email protected] [3] 01:46:21 [SUCCESS] [email protected] [root@docker test]#pssh -h iplist -i 'ls -l /root/' [1] 01:46:42 [SUCCESS] [email protected] The total amount of 4 drwxr-xr-x 2 root root 4096 10月 25 05:30 aaa [2] 01:46:42 [SUCCESS] [email protected] The total amount of 4 drwxr-xr-x. 2 root root 4096 11月 9 01:46 aaa [3] 01:46:42 [SUCCESS] [email protected] 0 Total amount drwxr-xr-x. 2 root root 42 11月 9 01:46 aaa [root@docker test]#
Description: Returns the directory and pscp.pssh usage, the need to add the -r option.