问题描述
我需要以下代码的帮助。我只需要完成一些简单的任务即可。 bakcup并替换一些ssl证书文件。但它似乎不起作用。下面的代码有什么问题:
import_esx() {
local username=root
local backuptimestamp=$(date +"%m-%d-%y-%I:%M:%s_%p")
/usr/bin/expect << EOF
set timeout 30
spawn ssh -l $username $Ip_Address
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"*?assword: " { send "$CommonPassword\r"; exp_continue}
}
send_user "backing up current certificates\r"
send "mv /etc/vmware/ssl/rui.key /etc/vmware/ssl/rui.key.$backuptimestamp\r"
send "mv /etc/vmware/ssl/rui.crt /etc/vmware/ssl/rui.crt.$backuptimestamp\r"
send "mv /etc/vmware/ssl/castore.pem /etc/vmware/ssl/castore.pem.$backuptimestamp\r"
EOF
}
谢谢 杰瑞
解决方法
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"*?assword: " { send "$CommonPassword\r"; exp_continue}
}
您exp_continue
在每个分支上:您必须期待其他事情,以便停止循环。
在每次发送之前,您应该期待一些提示,通常是shell提示。另外,在执行最后一个mv命令之后,您需要先send "exit\r"
,然后再expect eof
假设您的shell提示符以“美元空间”结尾,则可以将它们放在一起:
set prompt {[$] $}
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"*?assword: " { send "$CommonPassword\r"; exp_continue}
-re $prompt
}
send_user "Backing up current certificates\r"
send "mv /etc/vmware/ssl/rui.key /etc/vmware/ssl/rui.key.$backuptimestamp\r"
expect -re $prompt
send "mv /etc/vmware/ssl/rui.crt /etc/vmware/ssl/rui.crt.$backuptimestamp\r"
expect -re $prompt
send "mv /etc/vmware/ssl/castore.pem /etc/vmware/ssl/castore.pem.$backuptimestamp\r"
e1xpect -re $prompt
send "exit\r"
expect eof