我寫了一個期望腳本的工作原理如下:而在循環問題expect腳本
- SSH到server1
- 在Server1 SSH到另一臺服務器服務器2
- 從服務器2到server(服務器),然後須藤成一個用戶並運行這些命令。
在腳本中,我從兩個名爲hostnames.out和commands.out的文件中讀取要執行的主機名和命令。我使用了一個while循環遍歷hostnames.out中的每個條目並從commands.out文件運行命令。
我在hostnames.out中使用單個條目測試了我的腳本,但它工作正常,但是當我添加多行時,它並未從第二行開始在主機名上運行命令。
的commands.out文件的格式是(每行一個命令):
ls -lrt hostname whoami
的hostnames.out文件的格式是:
server1 user password server2 user password server3 user password
我已附加的腳本參考。請讓我知道問題出在哪裏。
#!/usr/bin/expect
#####################################################
# script to automate manual works - remote 2 #
# Gopinath #
#####################################################
#Variable declaration:
#Setting variable "prompt" for multiple prompts:
set prompt {[]$#%]\s*$}
#Reading commands list from file:
set fp1 [open "commands_list_2.out" "r"]
set file_data [read $fp1]
close $fp1
# read the hosts file one line at a time
# There should be no new line at the end of the hostnames.out file
set fp [open "hostnames_2.out" "r"]
while { [gets $fp data] >= 0 } {
set ssh1 [lindex $data 0]
set ssh1_usr [lindex $data 1]
set ssh1_pwd [lindex $data 2]
set ods [lindex $data 3]
set ods_usr [lindex $data 4]
set ods_pwd [lindex $data 5]
set serv1 [lindex $data 6]
set serv1_usr [lindex $data 7]
set serv1_pwd [lindex $data 8]
puts $ssh1
puts $ssh1_usr
puts $ssh1_pwd
puts $ods
puts $ods_usr
puts $ods_pwd
puts $serv1
puts $serv1_usr
puts $serv1_pwd
spawn -noecho ssh [email protected]$ssh1
expect {
"*password:" { send "$ssh1_pwd\r"}
"*route*" { puts "login failed"; exit 1 }
"timed out" { puts "login failed timed out"; exit 1 }
}
expect {
-re $prompt { send "whoami\r"}
}
expect -re $prompt {
send "ssh [email protected]$ods\r" }
expect {
"password:" { send "$ods_pwd\r" }
}
}
expect {
-re $prompt { send "whoami\r"}
}
expect -re $prompt {
send "ssh [email protected]$serv1\r" }
expect {
"password:" { send "$serv1_pwd\r" }
}
expect -re $prompt
foreach a [list $file_data] {
send "$a"
expect -re prompt
}
expect -re prompt {
send "exit\r"
}
expect eof
close $fp
`
您是否收到任何錯誤訊息? – Dinesh
不,我沒有得到任何錯誤信息,當我添加第二行到hostnames.out文件。 – smgopinath