需要登录到多个主机我无法决定如何在此脚本数组或列表中添加主机名变量。任何人都可以建议吗?
第二件事是我在执行此脚本时遇到错误。
#!/usr/bin/env expect
set timeout 12
set date [exec date "+%d-%B-%Y"]
spawn sh -c "cd /backup/"
for ((i=0;i<8;i++))
do
spawn sh -c "ssh host001n < ./backup.py > /backup/dbbackup-$file-$date.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "pass\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "pass\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "pass\r"
expect "Password:"
send "pass\r"
interact
done
仅添加一个 shebang 后,出现以下错误。
spawn sh -c cd /backup/
wrong # args: should be "for start test next command"
while executing
"for ((i=0"
(file "./backup.py" line 14)
答案1
尝试这个:
#!/usr/bin/env expect
set timeout 12
set date [timestamp -format "+%d-%B-%Y"] ;# don't need to call out to date
cd /backup ;# use the built-in cd command
# need to use Tcl syntax for the for loop, not shell syntax
for {set i 0} {$i < 8} {incr i} {
spawn sh -c "ssh host001n < ./backup.py > /backup/dbbackup-$file-$date.txt"
# more DRY
expect {
"Enter passphrase for key '/root/.ssh/id_rsa':" {
send "pass\r"
exp_continue
}
"Password:" {send "pass\r"}
eof
}
}