複数のホストにログインする必要があります。このスクリプトの配列またはリストにホスト名変数を追加する方法を決定することはできません。誰でも提案できますか?
2つ目は、このスクリプトの実行中にエラーが発生することです。
#!/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を1つだけ追加すると、次のエラーが発生します。
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
}
}