pssh使用情境
假設現在需要對數百台伺服器節點進行配置更新或者執行一些簡短command,而目前並沒有完備的部署工具軟體, 那可以選擇向pssh這樣的並行登入遠程終端並執行指定命令的shell工具。
以前機器節點少的時候,直接用shell寫個for迴圈來執行命令,也沒什麼問題。當節點數量多了之後,一個shell命令可能要消耗幾秒, 這時才能感受到pssh這種並行方式的好處,省時省力。 pssh可選配置參數列表
除了pssh,當需要傳遞登入密碼時,可以用到sshpass命令:
pintai@MG:~/bak$ pssh --helpUsage: 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 "[user@]host[:port]") -H HOST_STRING, --host=HOST_STRING additional host entries ("[user@]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 itExample: pssh -h hosts.txt -l irb2 -o /tmp/foo uptimepintai@MG:~/bak$ sshpass --helpsshpass: 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 informationAt most one of -f, -d, -p or -e should be used
1.使用sshpass傳遞登入密碼
先把需要遠程登入的host集中寫到一個檔案, 比如叫hostlist,寫入host列表:
192.168.1.11:22192.168.1.12:22192.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 superuserprivileges or access to your account.[1] 11:23:21 [SUCCESS] 192.168.1.11:22test1.hostname[2] 11:23:21 [SUCCESS] 192.168.1.12:22test2.hostname[3] 11:23:21 [SUCCESS] 192.168.1.13:22test3.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 superuserprivileges 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:22pintai@MG:~/bak$ cat output/*test1.hostnametest2.hostnametest3.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/