来自文件的行作为期望脚本中的变量

时间:2015-11-18 01:21:52

标签: shell expect

需要一些帮助在expect脚本中创建一个循环,其中变量是从文件中的行中提取的。我当前期望的脚本如下所示;

#!/usr/bin/expect -f
set i [open "samplelist"]
set hosts [split [read $i] "\n"]

set timeout -1
foreach host $hosts {
spawn /usr/bin/ssh appadm@$host
expect "appadm@$host:~>"
send "su epos\r"
expect "Password:"
send "pa55w0rd\r"
expect "epos@$host:/home/appadm>"
send "grep playlist /appl/epos/bin/cron.epos\r"
expect "epos@$host:/home/appadm>"
send "exit\r"
expect "appadm@$host:~>"
send "exit\r"
}
expect eof
close

但是,当我运行此脚本时,它在读取文件的最后一行后无法正确终止

1 个答案:

答案 0 :(得分:1)

由Glenn指导后的最终修改期望脚本

#!/usr/bin/expect -f
set i [open "samplelist"]
set hosts [split [read -nonewline $i] "\n"]

set timeout -1
foreach host $hosts {
spawn /usr/bin/ssh appadm@$host
expect "appadm@$host:~>"
send "su epos\r"
expect "Password:"
send "p@ssw0rd\r"
expect "epos@$host:/home/appadm>"
send "grep playlist /appl/epos/bin/cron.epos\r"
expect "epos@$host:/home/appadm>"
send "exit\r"
expect "appadm@$host:~>"
send "exit\r"
expect eof
}

脚本从文件中读取主机列表,循环连接并运行命令到远程主机,直到列表末尾。