简体   繁体   English

预期 SFTP 脚本在 bash 中不起作用

[英]Expect Script for SFTP not working in bash

I have created a shell script containing an expect script for getting a file from remote location.我创建了一个 shell 脚本,其中包含一个用于从远程位置获取文件的期望脚本。 Everything works well, until some command is sent.一切正常,直到发送一些命令。 Whether it is 'ls' or 'pwd' or any other command, the expect script ends abruptly.无论是 'ls' 还是 'pwd' 或任何其他命令,expect 脚本都会突然结束。 Could you guy help me out with this.你能帮我解决这个问题吗?

NOTE : Security is not a concern, hence not using Public keys.注意:安全不是问题,因此不使用公钥。

#!/bin/ksh

FTPREMOTEPATH=/Inbox
FTPREMOTEFILENAME=test.CSV


/usr/bin/expect -f - <<EOFEXPECT1
set timeout 60
spawn sftp -oPort=1002 username@test.server.com
expect {
        default { exit 1}
        -re "failed|invalid password|Permission denied" {exit 2}
        "Connection closed" {exit 1}
        timeout {exit 1}
}

expect "Password:"
send "password\r"

expect {
        default {exit 1}
        -re "password|Enter password for " {puts "Incorrect Password"; exit 2}
        "sftp>" {send "cd $FTPREMOTEPATH \r"}
}

expect "sftp>"
send "pwd\r"

send "get $FTPREMOTEFILENAME \r";

EOFEXPECT1

In above script, the scripts end abruptly after sending cd $FTPREMOTEPATH.在上面的脚本中,脚本在发送 cd $FTPREMOTEPATH 后突然结束。

Below is the Output :以下是输出:

$ ./test.sh
spawn sftp -oPort=1002 username@test.server.com
Enter password for username
Password:
sftp> cd /Inbox
sftp> $

Imagine you call a local restaurant, and say "get me some food" and then just hang up.想象一下,你给当地一家餐馆打电话,说“给我买点吃的”然后挂断电话。 What is the restaurant supposed to do?餐厅应该做什么?

When you wait for the sftp prompt, you're waiting for the food to be delivered.当您等待 sftp 提示时,您正在等待食物送达。

I'd recommend 2 things:我推荐两件事:

  1. before send "get ... change the timeout value to -1 -- that will help if it takes longer than 60 seconds to receive the file.send "get ...的超时值更改为-1如果花费超过60秒接收文件,这将有助于- 。
  2. after you send "bye", expect eof -- that lets the sftp connection close gracefully.在您发送“再见”之后, expect eof它可以让 sftp 连接正常关闭。

The reason is that your Expect script has reached the end of the script, and therefore terminates the child (ftp) process before the ftp process has a chance to finish downloading the file.原因是您的 Expect 脚本已经到达脚本的末尾,因此在 ftp 进程有机会完成下载文件之前终止了子 (ftp) 进程。

在此处输入图片说明

Okay, Apparently i was missing expect "sftp>" {send "bye\\n"} , before EOF (EOFEXPECT1).好吧,显然我在 EOF (EOFEXPECT1) 之前错过了期待 "sftp>" {send "bye\\n"}

However, i would still be interested in knowing the significance of bye in expect script.但是,我仍然有兴趣了解期待脚本中再见的重要性。

Here is the updated and working code :这是更新后的工作代码:

#!/bin/ksh

FTPREMOTEPATH=/Inbox
FTPREMOTEFILENAME=test.CSV


/usr/bin/expect -f - <<EOFEXPECT1
set timeout 60
spawn sftp -oPort=1002 username@test.server.com
expect {
        default { exit 1}
        -re "failed|invalid password|Permission denied" {exit 2}
        "Connection closed" {exit 1}
        timeout {exit 1}
}

expect "Password:"
send "password\r"

expect {
        default {exit 1}
        -re "password|Enter password for " {puts "Incorrect Password"; exit 2}
        "sftp>" {send "cd $FTPREMOTEPATH \r"}
}

expect "sftp>"
send "pwd\r"

send "get $FTPREMOTEFILENAME \r";

expect "sftp>" {send "bye\n"}

EOFEXPECT1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM