批量ssh並執行命令


批量ssh登錄並執行命令

維護集群時,常常需要在大量服務器上執行相同的命令,雖然可以自己寫循環執行腳本,但是不僅麻煩而且執行效率不高。此時我們通常會使用pssh工具來並發執行SSH指令。

  • 控制機以centos7系統為例

  • 安裝 pssh和 sshpass
    yum -y install pssh
    yum -y install sshpass

  • 批量獲取機器名:
    sshpass -p 'password' pssh -O StrictHostKeyChecking=no -h ip.txt -l root -A -i "hostname"

  • 批量重啟:
    sshpass -p 'password' pssh -O StrictHostKeyChecking=no -h ip.txt -l root -A -i "reboot"

password 是root密碼
ip.txt 內容格式如下:

172.18.0.11
172.18.0.12:2222
172.18.0.13:3333

如果SSH默認22端口可以不用加端口。如果SSH端口不同,可在ip后面加上端口號。

成功返回 SUCCESS
失敗返回 FAILURE

pssh 常用參數

-h	主機名列表文件
-l	登陸用戶名,例如 -l root
-A	提供統一的登陸密碼
-i	交互模式,遠程服務器的命令執行結果會輸出
-O ssh參數

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 "[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 it

Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
  • sshpass 常用參數
-f 指定密碼文件
-p 指定密碼
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

   -P prompt     Which string should sshpass search for to detect a password prompt
   -v            Be verbose about what you're doing
   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM