通过expect scp远端文件时,如果遇到远端连接不上的情况,如何缩短scp等待的时间?
哪个大神知道请指导一下,万分感谢🙏
我自己写的expect脚本如下所示,虽然在开头设置了set timeout 5
,但是在测试的时候发生,如果远端的网络连接不上,需要等待20s左右才会退出scp,无法实现5s就让scp退出结束
#!/bin/bash
##
# scp Remote Log to local
##
expectResponseCMD="
expect {
\"*yes/no\" {set timeout 3; send \"yes\r\"; exp_continue }
\"*assword:\" {set timeout 3; send \"a1234\r\" }
}
"
/usr/bin/expect << EOF
set timeout 5
spawn scp -r ${remoteUserName}@${remoteIPAddress}:${remoteFileStoreDirectory}${sn} ${localFileStoreDirectory}
${expectResponseCMD}
set timeout 10
expect ${remoteEndFlag} { send "exit\r"}
EOF
控制台的执行
% ./expectGrammarTest.sh
spawn scp -r patrickqiwei@192.100.23.219:~/Library/Logs/DataCollection/testFile /Users/patrick/Library/Logs/DataCollectionBackup/
(这一步scp花费20s才结束退出)
%
找了一下这个问题可以使用scp本身的timeout参数来解决,使用
scp -o "ConnectTimeout 10"
,完整命令如下: