我正在尝试将一些文件从本地计算机移动/复制到我可以通过 ssh 访问的另一台主机。但不幸的是,下面的脚本不起作用。
Are you sure you want to continue connecting (yes/no)?
当我运行这个脚本时,每次运行它都会询问我。
我的本地计算机上有 3 个文件ranfull
ranhalf
midran
,我需要通过 scp 脚本将其传输/复制到其他主机,但只有第一个文件被复制。
将无法设置sshkeygen
。
#!/bin/bash
HOSTS="VM2 VM3 VM4 VM1"
read -p "Password: " PASSWORD
for HOST in $HOSTS
do
expect -c "
spawn /usr/bin/scp /tmp/ranfull ranhalf midran root@$HOST:/tmp/
expect {
"*password:*" { send $PASSWORD\r;interact }
}
exit
"
done
答案1
首先,您的引用是可疑的:双引号字符串中有双引号。让我们解决这个问题,并改进缩进:
for HOST in $HOSTS; do
expect -c <<END_EXPECT
spawn /usr/bin/scp /tmp/ranfull ranhalf midran root@$HOST:/tmp/
expect {
"*password:*" {
send $PASSWORD\r
interact
}
}
exit
END_EXPECT
done
现在,为了适应可能出现或可能不出现的提示,请执行以下操作:
expect {
"Are you sure you want to continue connecting*" {
send yes\r
exp_continue
}
"*password:*" {
send $PASSWORD\r
interact
}
}
exp_continue
本质上是“循环”回到expect命令,因此它仍然可以监视另一个“你确定...”提示或密码提示。
答案2
-o StrictHostKeyChecking=否
#!/bin/bash HOSTS="VM2 VM3 VM4 VM1" read -p "Password: " PASSWORD for HOST in $HOSTS do expect -c " spawn /usr/bin/scp -o StrictHostKeyChecking=no /tmp/ranfull ranhalf midran root@$HOST:/tmp/ expect { "password:" { send $PASSWORD\r;interact } } exit " done