1. 程式人生 > >大作業11-指令碼批量遠端執行命令

大作業11-指令碼批量遠端執行命令

然後再寫一個通用的可以批量遠端執行命令的expect指令碼:

[[email protected] ~]# vim cmd.expect #!/usr/bin/expect set user [lindex $argv 0]  # 系統使用者 set host [lindex $argv 1]  # 伺服器地址 set passwd [lindex $argv 2]  # 密碼 set cm [lindex $argv 3]  # 需要執行的命令 spawn ssh [email protected]$hostset timeout -1    #沒有timeout ; set timeout XX -- 設定具體的timeout時間(秒) 防止命令宕機 expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } }

expect "]#" send "$cm\r" expect "]#" send "exit\r" interact

[[email protected] ~]# chmod a+x cmd.expect [[email protected] ~]# vim cmd.sh  # 呼叫指令碼 #!/bin/bash user=$2     ## 引數2是使用者名稱 password=$3   ## 引數3是密碼 cm=$4    ## 引數4需要執行的命令 for ip in `cat $1`  ## 引數1是儲存ip列表的檔案路徑 do   ./cmd.expect "$user" "$ip" "$password" "$cm" done

# 使用這個指令碼批量安裝一些基礎通用的工具 [[email protected] ~]# sh ./cmd.sh "/root/ipAll.txt" "root" "lri35krJF;ba" "yum -y install expect vim-enhanced epel-release libmcrypt-devel libmcrypt"