FTP输出使用shell脚本重定向到我的linux机器上的文件

时间:2017-04-22 14:35:33

标签: linux bash shell ftp

我正在寻找一个Bash脚本,用于将简单的ls命令输出从我的FTP服务器重定向到我的Linux机器上的文件。

下面是一步一步的命令来说明我要编写的脚本。可以在没有用户/密码的情况下访问FTP站点,因此我在提示时将用户输入为anonymous,密码为空白。

ftp <FTP_SERVER>
Connected to <FTP_SERVER> (IP_ADDRESS).
220 Microsoft FTP Service
Name (<FTP_SERVER>:root): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password. 
Password:
230 Anonymous user logged in.
Remote system type is Windows_NT.

ftp> ls /Outgoing/Artemis/incremental/Hashes-1492870261.zip

227 Entering Passive Mode (10,37,108,77,5,87).

125 Data connection already open; Transfer starting.

04-22-17  07:11AM               227634 Hashes-1492870261.zip
226 Transfer complete.

我需要命令'ls'命令的输出我正在执行以保存在我的linux盒子上的文件中。

这是我的脚本:

ftp FTP_SERVER <<EOF >outputfile

quote USER anonymous

quote PASS 

prompt noprompt

ls -la /Outgoing/Artemis/incremental/Hashes-1492870261.zip

quit

EOF

当我执行此操作时,我收到登录失败错误。

sh -x test.sh

+ ftp FTP_SERVER`

Password:

Login failed.

local: /Outgoing/Artemis/incremental/Hashes-1492870261.zip: No such file or directory

我使用了一些随机密码作为测试而不是我之前使用的空白(null),但仍然得到相同的错误。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

关闭互动模式:

ftp -i -n FTP_SERVER <<EOF >outputfile
user <user> <password>
binary
ls -la .
quit
EOF

答案 1 :(得分:1)

使用lftp

可能会更好
lftp -e 'ls -l someFile.zip; quit' -u USER,PASSWORD FTPSERVER   > ls.txt

大写字母中的所有单词都需要用自己的值替换。

或者你可以试试curl

curl ftp://FTPSERVER --user USER:PASSWORD