利用expect批量部署linux伺服器

2021-05-26 22:13:33 字數 1227 閱讀 4156

shell, by rake.

參考:涉及檔案:

1:login.exp

2:passwd.txt

3:expect.sh

目的批量執行test.sh檔案

1:login.exp

#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]

set passwd [lindex $argv 1]

set timeout 30

spawn ssh root@$ipaddress

expect

"password:"

}expect "]*"

send "wget \r"

send "sh test.sh\r"

send "exit\r"

expect eof

2:passwd.txt

ip    passwd

3:expect.sh

#!/bin/bash

for i in `awk '' passwd.txt`

doj=`awk -v i="$i" '' passwd.txt`

expect /root/bin/expect/login.exp $i $j

done

set timeout 30

設定超時時間的,計時單位:秒

spawn是進入expect環境後才可以執行的expect內部命令,如果沒有裝expect或者直接在預設的shell下執行是找不到spawn命令的。所以不要用 「which spawn「之類的命令去找spawn命令。好比windows裡的dir就是乙個內部命令,這個命令由shell自帶,你無法找到乙個dir.com 或 dir.exe 的可執行檔案。它主要的功能是給ssh執行程序加個殼,用來傳遞互動指令。

expect 「password:」

這裡的expect也是expect的乙個內部命令,expect的shell命令和內部命令是一樣的,但不是乙個功能。這個命令的意思是判斷上次輸出結果裡是否包含「password:」的字串,如果有則立即返回,否則就等待一段時間後返回,這裡等待時長就是前面設定的30秒

interact

執行完成後保持互動狀態,把控制權交給控制台,這個時候就可以手工操作了。如果沒有這一句登入完成後會退出,而不是留在遠端終端上。如果你只是登入過去執行一段命令就退出,可改為〔expect eof〕

利用expect互動實現對主機批量建立SSH信任

ssh免密驗證,shell自動設定 echo bin sh etc init.d functions f usr bin expect 若沒expect則安裝 0 安裝失敗則退出 pub whoami ssh id dsa.pub 公鑰路徑 f pub 若沒公鑰則生成 expect自動互動 func...

expect 批量監控主機

oracle oaprimary shell cat expect.sh while read line douser echo line awk ip echo line awk passwd echo line awk 把shell中的 user ip passwd引數傳遞給expect指令碼 ...

EXPECT 指令碼 批量 遠端互動

在shell自動部署專案中對自動部署免去使用者互動很痛苦 expect eof 這個一定要加,與spawn對應表示捕獲終端輸出資訊終止,類似於if.endif expect指令碼必須以interact或expect eof結束,執行自動化任務通常expect eof就夠了。設定expect永不超時。...