问题出现背景:
在内网的Linux机器A中使用expect脚本自动登录到云主机B中并下载文件到A中。
问题片断:
get /chinawiserv/Ftp/crawlerWorker.jar /home/expectDownload/crawlerWorker.jar
local: /home/expectDownload/crawlerWorker.jar remote: /chinawiserv/Ftp/crawlerWorker.jar
227 Entering Passive Mode (10,41,49,10,218,187).
ftp: connect: Connection timed out
ftp> quit
221 Goodbye.
解决方法:
由于两台机子处于不同的网络中,网速较慢,我们需要使用PORT方式传输数据(FTP默认为PASV模式,即被动方式),开启方式为:
ftp passive
Passive mode off.
ftp passive (注:再次运行该命令就会打开PASV模式)
Passive mode on.
修改后的脚本片断为:
expect "ftp>*"
send "passive\r"
expect "Passive mode off."
send "lcd $dir\r"
expect {
"*file" { send_user "local $dir No such file or directory";send "quit\r" }
"*now*" { send "get $remotedir/$file $dir/$file\r"}
}
expect {
"*Failed" { send_user "remote $remotefile No such file\r";send "quit\r" }
"*Transfer complete*" { send_user "$file has been download\r";send "quit\r"}
}
send "passive"