Giới thiệu Parallel SSH (PSSH Tools)

Khi bạn muốn thực hiện 1 lệnh hoặc 1 vài câu lệnh thông qua SSH cho nhiều hosts cùng 1 lúc kiểu như thế này:

for server in list; do
ssh user@server "command"
done

Bạn sẽ gặp nhiều trở ngại cho việc ssh vào nhiều server cùng 1 lúc và không kiểm soát được những output trả về từ phía server trả về sau khi thực hiện các command. Hoặc bạn muốn deploy 1 dịch vụ nào đó cho toàn bộ server trong hệ thống của bạn nhưng không có 1 tool nào hỗ trợ như salt hay puppet.

Biện pháp nào dành cho bạn trong trường hợp này?

PSSH là công cụ cung cấp phiên bản parallel (song song) của OpenSSH, bao gồm pssh, pscp, prsync, pnuke, pslurp…Mã nguồn được viết bằng Python hỗ trợ từ Python 2,4 trở lên, được viết chính bởi Brent N. Chun.
Project tại đây: http://code.google.com/p/parallel-ssh

pssh được định nghĩa trong pssh(1) linux man page

pssh is a program for executing ssh in parallel on a number of hosts. It provides features such as sending input to all of the processes, passing a password to ssh, saving output to files, and timing out.

Cú pháp của nó cũng hết sức đơn giản và dễ hiểu :

Options

-h host_file

–hosts host_file
Read hosts from the given host_file. Lines in the host file are of the form [user@]host[:port] and can include blank lines and comments (lines beginning with “#”). If multiple host files are given (the -h option is used more than once), then pssh behaves as though these files were concatenated together. If a host is specified multiple times, then pssh will connect the given number of times.
-H[user@]host[:port]–host

[user@]host[:port]

-H

“[user@]host[:port] [ [user@]host[:port ] … ]”

–host

“[user@]host[:port] [ [user@]host[:port ] … ]”

Add the given host strings to the list of hosts. This option may be given multiple times, and may be used in conjunction with the -h option.
-l user
–useruser
Use the given username as the default for any host entries that don’t specifically specify a user.
-p parallelism
–par parallelism
Use the given number as the maximum number of concurrent connections.
-t timeout
–timeout timeout
Make connections time out after the given number of seconds. With a value of 0, pssh will not timeout any connections.
-o outdir
–outdir outdir
Save standard output to files in the given directory. Filenames are of the form [user@]host[:port][.num] where the user and port are only included for hosts that explicitly specify them. The number is a counter that is incremented each time for hosts that are specified more than once.
-e errdir
–errdir errdir
Save standard error to files in the given directory. Filenames are of the same form as with the -o option.
-x args
–extra-args args
Passes extra SSH command-line arguments (see the ssh(1) man page for more information about SSH arguments). This option may be specified multiple times. The arguments are processed to split on whitespace, protect text within quotes, and escape with backslashes. To pass arguments without such processing, use the -X option instead.
-X arg
–extra-arg arg
Passes a single SSH command-line argument (see the ssh(1) man page for more information about SSH arguments). Unlike the -x option, no processing is performed on the argument, including word splitting. To pass multiple command-line arguments, use the option once for each argument.
-O options
–options options
SSH options in the format used in the SSH configuration file (see the ssh_config(5) man page for more information). This option may be specified multiple times.
-A
–askpass
Prompt for a password and pass it to ssh. The password may be used for either to unlock a key or for password authentication. The password is transferred in a fairly secure manner (e.g., it will not show up in argument lists). However, be aware that a root user on your system could potentially intercept the password.
-i
–inline
Display standard output and standard error as each host completes.
–inline-stdout
Display standard output (but not standard error) as each host completes.
-v
–verbose
Include error messages from ssh with the -i and \ options.
-I
–send-input
Read input and send to each ssh process. Since ssh allows a command script to be sent on standard input, the -I option may be used in lieu of the command argument.
-P
–print
Display output as it arrives. This option is of limited usefulness because output from different hosts are interleaved.

Sau khi đọc document xong, việc đầu tiên cần làm là cài đặt nó 😀

Nếu bạn đã cài đặt pip, thì mọi chuyện đơn giản chỉ là command: sudo -E pip install pssh

nhanpt5@nhanpt5-X230:~$ sudo -E pip install pssh
[sudo] password for nhanpt5:
Downloading/unpacking pssh
Downloading pssh-2.3.1.tar.gz
Running setup.py (path:/tmp/pip_build_root/pssh/setup.py) egg_info for package pssh
Installing collected packages: pssh
Running setup.py install for pssh
changing mode of build/scripts-2.7/pssh from 644 to 755
changing mode of build/scripts-2.7/pnuke from 644 to 755
changing mode of build/scripts-2.7/prsync from 644 to 755
changing mode of build/scripts-2.7/pslurp from 644 to 755
changing mode of build/scripts-2.7/pscp from 644 to 755
changing mode of build/scripts-2.7/pssh-askpass from 644 to 755

changing mode of /usr/local/bin/pscp to 755
changing mode of /usr/local/bin/pssh-askpass to 755
changing mode of /usr/local/bin/pslurp to 755
changing mode of /usr/local/bin/prsync to 755
changing mode of /usr/local/bin/pssh to 755
changing mode of /usr/local/bin/pnuke to 755
Successfully installed pssh
Cleaning up…

Ngoài ra bạn có thể download bản cài đặt tại https://code.google.com/p/parallel-ssh/downloads/list

sudo wget http://code.google.com/p/parallel-ssh/downloads/detail?name=pssh-2.3.1.tar.gz
–2015-07-22 22:47:23–  http://code.google.com/p/parallel-ssh/downloads/detail?name=pssh-2.3.1.tar.gz
Resolving code.google.com (code.google.com)… 42.117.10.50, 42.117.10.59, 42.117.10.40, …
Connecting to code.google.com (code.google.com)|42.117.10.50|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: unspecified [text/html]
Saving to: ‘detail?name=pssh-2.3.1.tar.gz’

[ <=>                                   ] 10.634      –.-K/s   in 0,002s

2015-07-22 22:47:23 (4,99 MB/s) – ‘detail?name=pssh-2.3.1.tar.gz’ saved [10634]

Giải nén:

tar xvzf pssh-2.3.1.tar.gz

Di chuyển vào thư mục:

cd pssh-2.3.1/

Cài đặt:

sudo python setup.py install

Bắt đầu sử dụng với option cơ bản:

Với 1 host : 192.168.6.128 , command là date

nhanpt5@nhanpt5-X230:~$ pssh -i -H “192.168.6.128” -l root  date
[1] 22:38:59 [SUCCESS] 192.168.6.128
Thứ năm, 23 Tháng bảy năm 2015 05:38:59 ICT

Bạn sẽ thấy kết qủa trả về như trên. Khi gặp lỗi, 1 kết qủa trả về tương tự như sau:

nhanpt5@nhanpt5-X230:~$ pssh -i -H “192.168.6.128” date
[1] 22:38:05 [FAILURE] 192.168.6.128 Exited with error code 255
Stderr: pssh error: SSH requested a password. Please create SSH keys or use
the -A option to provide a password.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

Một số Status Code trả về :

Exit Status

The exit status codes from pssh are as follows:

0Success1

Miscellaneous error

2

Syntax or usage error

3

At least one process was killed by a signal or timed out.

4

All processes completed, but at least one ssh process reported an error (exit status 255).

5

There were no ssh errors, but at least one remote command had a non-zero exit status.

Ví dụ: Thực hiện command hostname với 2 host:

nhanpt5@nhanpt5-X230:~$ pssh -i -H “192.168.6.128 192.168.6.129” -l root hostname
[1] 23:06:09 [SUCCESS] 192.168.6.129
sample.test
[2] 23:06:09 [SUCCESS] 192.168.6.128
centos6.local

Như vậy, đã hoàn thành xong phần cơ bản giới thiệu về pssh, bạn có thể thực hiện thêm phần nâng cao và tùy chọn phù hợp tùy vào nhu cầu của mình.


1 thought on “Giới thiệu Parallel SSH (PSSH Tools)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.