脚本操作FTP。 Run FTP command by Script.

最近一个周一直在尝试用 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来执行。

 

 1 @echo off 
 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 
来实现。

machine 10.1.1.10
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 to man 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. The quit is identical to the byecommand.
     
  • 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.

然后再进行接下来的操作, chmod 600 .netrc  和  ftp ftp.walkernews.net 。太过于复杂了。如果对于FTP的操作比较固定,比如定期的上传同一文件夹下的文件到FTP上。不适合经常改变的命令和操作。搜索(不是陈凯歌的电影哦)继续。伟大的google无所不能。How to use ftp in a shell script。原来Linux下的FTP可以同ftp -n<<ftp_cmd来缓存命令到ftp_cmd.

 

1 ftp -n<<ftp_cmd
2 open ${FTP_SERVER}
3 user ${FTP_USER} ${FTP_PASSWORD}
4 rename ${SOURCE_FILE} ${DESTINATION_FILE}
5 bye
6 ftp_cmd 

 参数说明: 

  1. ${FTP_SERVER}:ftp 服务器。 
  2. ${FTP_USER }:ftp 用户名。  
  3. ${FTP_PASSWORD }:ftp 用户的密码。  
  4. ${SOURCE_FILE }:ftp 上的源文件。  
  5. ${DESTINATION_FILE }:ftp 要移动到的目标文件。 
 ftp_cmd只是一个临时变量名,在符合命名规范的前提下可以任意命名。

最后,大功告成。 

 

 

 

 

 

posted on 2012-07-13 17:38  路政  阅读(1503)  评论(0编辑  收藏  举报