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

來源:互聯網
上載者:User
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/
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.