Bash sftp要求检索多个文件后脚本不会继续

时间:2018-12-24 20:21:27

标签: sftp expect

我需要为远程服务器上的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 ~]$

如果有人有什么想法阻止了期望的工作,我将非常感激。

爱德。

1 个答案:

答案 0 :(得分:-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