1. 程式人生 > >使用parallel-ssh批量執行遠端shell命令

使用parallel-ssh批量執行遠端shell命令

pssh使用場景

假設現在需要對數百臺伺服器節點進行配置更新或者執行一些簡短command,而目前並沒有完備的部署工具軟體, 那可以選擇向pssh這樣的並行登入遠端終端並執行指定命令的shell工具。
以前機器節點少的時候,直接用shell寫個for迴圈來執行命令,也沒什麼問題。當節點數量多了之後,一個shell命令可能要消耗幾秒, 這時才能感受到pssh這種並行方式的好處,省時省力。

pssh可選配置引數列表

除了pssh,當需要傳遞登入密碼時,可以用到sshpass命令:

[email protected]:~/bak$ pssh --help
Usage: pssh [OPTIONS] command
[...]
Options: --version show program's version number and exit --help show this help message and exit -h HOST_FILE, --hosts=HOST_FILE hosts file (each line "[[email protected]]host[:port]") -H HOST_STRING, --host=HOST_STRING additional host entries ("[
[email protected]
]host[:port]"
) -l USER, --user=USER username (OPTIONAL) -p PAR, --par=PAR max number of parallel threads (OPTIONAL) -o OUTDIR, --outdir=OUTDIR output directory for stdout files (OPTIONAL) -e ERRDIR, --errdir=ERRDIR output directory
for stderr files (OPTIONAL) -t TIMEOUT, --timeout=TIMEOUT timeout (secs) (0 = no timeout) per host (OPTIONAL) -O OPTION, --option=OPTION SSH option (OPTIONAL) -v, --verbose turn on warning and diagnostic messages (OPTIONAL) -A, --askpass Ask for a password (OPTIONAL) -x ARGS, --extra-args=ARGS Extra command-line arguments, with processing for spaces, quotes, and backslashes -X ARG, --extra-arg=ARG Extra command-line argument -i, --inline inline aggregated output and error for each server --inline-stdout inline standard output for each server -I, --send-input read from standard input and send as input to ssh -P, --print print output as we get it Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime [email protected]:~/bak$ sshpass --help sshpass: invalid option -- '-' Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters -f filename Take password to use from file -d number Use number as file descriptor for getting password -p password Provide password as argument (security unwise) -e Password is passed as env-var "SSHPASS" With no parameters - password will be taken from stdin -h Show help (this screen) -V Print version information At most one of -f, -d, -p or -e should be used

1.使用sshpass傳遞登入密碼

先把需要遠端登入的host集中寫到一個檔案, 比如叫hostlist,寫入host列表:

192.168.1.11:22
192.168.1.12:22
192.168.1.13:22

然後將ssh登入密碼寫到另一個檔案, 比如叫remotepass, 寫入密碼:

yourpassword

最後執行相關命令, 直接列印每個節點的輸出內容:

sshpass -f remotepass pssh -h hostlist -l yourloginname -A -i "hostname"

輸出結果如下

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
[1] 11:23:21 [SUCCESS] 192.168.1.11:22
test1.hostname
[2] 11:23:21 [SUCCESS] 192.168.1.12:22
test2.hostname
[3] 11:23:21 [SUCCESS] 192.168.1.13:22
test3.hostname

2.將結果輸出到指定檔案

如果需要將輸出結果收集起來,那麼可以通過-o選項來指定結果輸出目錄,比如:

sshpass -f remotepass pssh -h hostlist -l yourloginname -o outputdir -A "hostname"

執行時終端輸出:

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
[1] 11:23:21 [SUCCESS] 192.168.1.11:22
[2] 11:23:21 [SUCCESS] 192.168.1.12:22
[3] 11:23:21 [SUCCESS] 192.168.1.13:22

而當前目錄會生成outputdir目錄,目錄中每個host佔一個檔案,如:

pintai@MG:~/bak$ ls output/
192.168.1.11:22 192.168.1.12:22 192.168.1.13:22
pintai@MG:~/bak$ cat output/*
test1.hostname
test2.hostname
test3.hostname

3. 執行sudo命令

有些shell命令可能需要通過sudo許可權來執行,一般來說本地可以這麼執行

echo your_sudo_pass | sudo -S your_command

而在pssh中可以這麼做:

sshpass -f remotepass pssh -h hostlist -l yourloginname -o outputdir -A "echo your_sudo_pass | sudo -S netstat -antup | grep xxx"

執行完畢後,具體輸出結果可以在outputdir目錄下查詢。

4. 使用private key拷貝本地檔案到多個遠端終端

pscp -x "-i /locadir/id_rsa" -l yourname -h nodes.txt /tmp/local.txt /remote/dir/