最近一个周一直在尝试用 Kettle中的Move Files 来做FTP的远程移动文件操作。说来也怪,Move Files在使用FTP的时候应该是用java的FTPClient来进行操作。我在本地的Windows和VM(LINUX)上都能成功的实现此操作,但是唯独在Amazon 的EC2(Amazon Linux)上无法正常运行,总是提示File/ Folder does not exists. 真是奇怪了。
好在Kettle提供了Shell功能可以来运行Script。 于是,我就开始查找资料写一段script来执行。
2 pem ftp 服务器
3 echo open 22.19.67.14>>temp.txt
4 pem user, password
5 echo user 123456 123456>>temp.txt
6 pem 执行的命令
7 echo rename test.txt moved/test.txt>>temp.txt
8 pem 关闭连接
9 echo bye>>temp.txt
10 ftp -n -s :temp.txt
11 del temp.txt
在本地通过测试后,非常高兴,于是我急忙对我的程序做了修改,发布。结果,FAILED!!!with F*** word. 原来FTP的命令在windows和linux还是有点不一样的。Linux中FTP命令的选择项中没有-s:filename 这个项选择。有点小崩溃和无奈。继续google吧。 终于有了新的发现.Auto File Transfer Via FTP Batch Scripts In Both Windows And Linux。文章中提到通过修改用户根目录下的 .netrc
来实现。
login walker-login-id-here
password walker-login-password-here
macdef init
prompt
mget *.*
quit
Leave one mandatory blank line here
Leave one mandatory blank line here
machine ftp.walkernews. net
login walker-login-id-here
password walker-login-password-here
macdef init
pwd
quit
Leave one mandatory blank line here
Understand the .netrc file content
- the keyword
machine
is used to specify the target ftp host that the ftp client connect to
- the keyword
login
is used to specify the user login ID
- the keyword
password
is used to specify the password used to authenticate the user login ID
- the keyword
macdef init
is used to define the auto-ftp macro command that named as init (refer toman netrc
for more information)
- supposed the login authentication is successful, then the rest of lines after macdec init down to
quit
keyword are ftp client commands. Thequit
is identical to thebye
command.
- after the quit keyword, press ENTER twice to leave two empty lines! These two lines are mandatory syntax of .netrc file for auto-ftp. As of this .netrc sample file, I’ve another ftp server added right after the first two empty lines, and end the second ftp server with another two empty lines as well.
2 open ${FTP_SERVER}
3 user ${FTP_USER} ${FTP_PASSWORD}
4 rename ${SOURCE_FILE} ${DESTINATION_FILE}
5 bye
6 ftp_cmd
参数说明:
- ${FTP_SERVER}:ftp 服务器。
- ${FTP_USER }:ftp 用户名。
- ${FTP_PASSWORD }:ftp 用户的密码。
- ${SOURCE_FILE }:ftp 上的源文件。
- ${DESTINATION_FILE }:ftp 要移动到的目标文件。
最后,大功告成。