Bash sftp期望脚本在检索多个文件后不会继续执行

问题描述 投票:0回答:1

我需要制定一种从远程服务器获取FTP文件的方法。我无法使用键,因此登录名必须是“交互式的”。我觉得期望是要走的路。

我在bash脚本中具有以下期望代码:

expect <<EOD
    spawn sftp $sftp_user@$sftp_svr
    expect "password:"
    send "$sftp_pw\r"
    expect "sftp> "
    send "mget $getfile_path$getfile_name $savefile_path\r"
    expect "sftp> "
    send "bye\r"
EOD

该代码段仅检索一个文件时效果很好。但是,如果需要检索多个文件,则永远不会执行最终的“ expect sftp>”。工作:

[ed@svr1 ~]$ ./sftp_expect.sh
spawn sftp user@svr2
user@svr2's password:
Connected to svr2.
sftp> mget /data/user/upload/lastcheck1.txt /home/ed/report_received/
Fetching /data/user/upload/lastcheck1.txt to /home/ed/report_received/lastcheck1.txt
/data/user/upload/lastcheck1.txt                                                   100%   56    26.9KB/s   00:00
sftp>
[ed@svr1 ~]$

不工作:

[ed@svr1 ~]$ ./sftp_expect.sh
spawn sftp user@svr2
user@svr2's password:
Connected to svr2.
sftp> mget /data/user/upload/lastcheck*.txt /home/ed/report_received/
Fetching /data/user/upload/lastcheck1.txt to /home/ed/report_received/lastcheck1.txt
/data/user/upload/lastcheck1.txt                                                   100%   56    26.5KB/s   00:00
Fetching /data/user/upload/lastcheck2.txt to /home/ed/report_received/lastcheck2.txt
/data/hf_user/upload/lastcheck2.txt                                                   100%   56    19.3KB/s   00:00
Fetching /data/user/upload/lastcheck_svr2.txt to /home/ed/report_received/lastcheck_svr2.txt
/data/user/upload/lastcheck_svr2.txt                                            100%   56    31.9KB/s   00:00
sftp>
^C
[ed@svr1 ~]$

脚本获取文件,期望得到“期望的东西”,但不会继续:

expect: does "mget /data/user/upload/lastcheck*.txt /home/ed/report_received/\r\nFetching /data/user/upload/lastcheck1.txt to /home/ed/report_received/lastcheck1.txt\r\n\r/data/user/upload/lastcheck1.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck1.txt                                                                                                                                                                     100%   56    24.5KB/s   00:00    \r\nFetching /data/user/upload/lastcheck2.txt to /home/ed/report_received/lastcheck2.txt\r\n\r/data/user/upload/lastcheck2.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck2.txt                                                                                                                                                                     100%   56    38.3KB/s   00:00    \r\nFetching /data/user/upload/lastcheck_svr2.txt to /home/ed/report_received/lastcheck_svr2.txt\r\n\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                                0%    0     0.0KB/s   --:-- ETA" (spawn_id exp6) match glob pattern "sftp> "? no
/data/user/upload/lastcheck_svr2.txt                                                                                                                                                              100%   56    25.7KB/s   00:00

expect: does "mget /data/user/upload/lastcheck*.txt /home/ed/report_received/\r\nFetching /data/user/upload/lastcheck1.txt to /home/ed/report_received/lastcheck1.txt\r\n\r/data/user/upload/lastcheck1.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck1.txt                                                                                                                                                                     100%   56    24.5KB/s   00:00    \r\nFetching /data/user/upload/lastcheck2.txt to /home/ed/report_received/lastcheck2.txt\r\n\r/data/user/upload/lastcheck2.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck2.txt                                                                                                                                                                     100%   56    38.3KB/s   00:00    \r\nFetching /data/user/upload/lastcheck_svr2.txt to /home/ed/report_received/lastcheck_svr2.txt\r\n\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                                0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                              100%   56    25.7KB/s   00:00    \r\n" (spawn_id exp6) match glob pattern "sftp> "? no
sftp>
expect: does "mget /data/user/upload/lastcheck*.txt /home/ed/report_received/\r\nFetching /data/user/upload/lastcheck1.txt to /home/ed/report_received/lastcheck1.txt\r\n\r/data/user/upload/lastcheck1.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck1.txt                                                                                                                                                                     100%   56    24.5KB/s   00:00    \r\nFetching /data/user/upload/lastcheck2.txt to /home/ed/report_received/lastcheck2.txt\r\n\r/data/user/upload/lastcheck2.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck2.txt                                                                                                                                                                     100%   56    38.3KB/s   00:00    \r\nFetching /data/user/upload/lastcheck_svr2.txt to /home/ed/report_received/lastcheck_svr2.txt\r\n\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                                0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                              100%   56    25.7KB/s   00:00    \r\nsftp> " (spawn_id exp6) match glob pattern "sftp> "? yes
expect: set expect_out(0,string) "sftp> "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "mget /data/user/upload/lastcheck*.txt /home/ed/report_received/\r\nFetching /data/user/upload/lastcheck1.txt to /home/ed/report_received/lastcheck1.txt\r\n\r/data/user/upload/lastcheck1.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck1.txt                                                                                                                                                                     100%   56    24.5KB/s   00:00    \r\nFetching /data/user/upload/lastcheck2.txt to /home/ed/report_received/lastcheck2.txt\r\n\r/data/user/upload/lastcheck2.txt                                                                                                                                                                       0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck2.txt                                                                                                                                                                     100%   56    38.3KB/s   00:00    \r\nFetching /data/user/upload/lastcheck_svr2.txt to /home/ed/report_received/lastcheck_svr2.txt\r\n\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                                0%    0     0.0KB/s   --:-- ETA\r/data/user/upload/lastcheck_svr2.txt                                                                                                                                                              100%   56    25.7KB/s   00:00    \r\nsftp> "
send: sending "bye\r" to { exp6 }

^C
[ed@svr2 ~]$

[如果有人有什么想法阻止了期望,我将不胜感激。

编辑

sftp expect
1个回答
-1
投票
expect <<EOD
    set timeout -1
    spawn sftp $sftp_user@$sftp_svr
    expect "password:"
    send "$sftp_pw\r"
    expect "sftp> "
    send "mget $getfile_path$getfile_name $savefile_path\r"
    expect "sftp> "
    send "bye\r"
EOD
© www.soinside.com 2019 - 2024. All rights reserved.