Chinaunix首页 | 论坛 | 博客
  • 博客访问: 195647
  • 博文数量: 17
  • 博客积分: 2459
  • 博客等级: 大尉
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 21:32
文章分类

全部博文(17)

文章存档

2012年(6)

2011年(16)

2009年(1)

分类: Python/Ruby

2011-04-10 20:50:50

我们知道,要往FTP上面上传或下载文件,通常的做法是这样的:

1.  在命令行终端上,输入类似:
test@test ~: lftp ftp://public@xxxx
Passwd: xxx


登录到FTP服务器上,输入帐号和密码

2. 执行类似“put/get"指令来下载文件或是上传文件


自己对这种做法进行了改良,改良后的使用方法如下:

1. 比如想要上传某文件(如test1)到FTP服务器上,可以执行下面命令:
test@test ~: ./upload.pl  public@xxxx:test1
Passwd:  xxx

2.  想要下载某文件,可以使用下面命令:
test@test ~: ./download.pl  public@xxx:test1
Passwd: xx

具体脚本如下: 注意(这里FTP服务器端口默认为22, 如果不是22还需要设置下)

upload.pl:

  1. #!/usr/bin/perl

  2. #This script is used to upload file from ftp server automatically

  3. use strict;
  4. use Net::FTP;


  5. die "Usage:\n\t$0 [user]\@server:file\n" if (! defined @ARGV);


  6. my ($user,$passwd, $server, $file);
  7. my $debug = 0;
  8. my $argv = $ARGV[0];


  9. if ($argv =~ /\@(\d+.\d+.\d+.\d+):(.*)/){
  10.       $server = $1;
  11.       $file = $2;
  12. }

  13. print "FTP Server is :$server\n";
  14. print "Put filename is :$file\n";


  15. if ($argv =~ /(.*)\@/){
  16.     $user = $1;
  17.     print "Login Name :$user\n";

  18.     system "stty -echo";
  19.     print "Input password:";
  20.     
  21.     chomp($passwd = <STDIN>);
  22.     system "stty echo";
  23.     
  24. # print $passwd;
  25. }

  26. print "\n";

  27. ###Connect the ftp server
  28. print "Connect the ftp server....\n";

  29. my $ftp = Net::FTP->new("$server", Debug => 0)
  30.     or die "Cannot connect :$@";


  31. $ftp->login("$user", "$passwd")
  32.     or die "Cannot login: $ftp->message";


  33. #$ftp->binary()
  34. # or die "Cannot set binary mode: $ftp->message";

  35. $ftp->put("$file")
  36.     or die "Put file faild: $ftp->message";

  37. print "File has been uploaded successfully...\n";

  38. $ftp->quit;

download.pl
  1. #!/usr/bin/perl

  2. #This script is used to download file from ftp server automatically

  3. use strict;
  4. use Net::FTP;


  5. die "Usage:\n\t$0 [user]\@server:file\n" if (! defined @ARGV);


  6. my ($user,$passwd, $server, $file);
  7. my $debug = 0;
  8. my $argv = $ARGV[0];


  9. if ($argv =~ /\@(\d+.\d+.\d+.\d+):(.*):(.*)/){
  10.       $server = $1 . ":" . $2;
  11.       $file = $3;
  12. }

  13. print "FTP Server is :$server\n";
  14. print "Get filename is :$file\n";


  15. if ($argv =~ /(.*)\@/){
  16.     $user = $1;
  17.     print "Login Name:$user";

  18.     system "stty -echo";
  19.     print "Input password:";
  20.     
  21.     chomp($passwd = <STDIN>);
  22.     system "stty echo";
  23.     
  24. # print $passwd;
  25. }


  26. ###Connect the ftp server
  27. print "Connect the ftp server....\n";

  28. my $ftp = Net::FTP->new("$server", Debug => 0)
  29.     or die "Cannot connect :$@";

  30. $ftp->login("$user", "$passwd")
  31.     or die "Cannot login: $ftp->message";

  32. $ftp->get("$file")
  33.     or die "Put file faild: $ftp->message";

  34. print "File has been downloaded successfully...\n";

  35. $ftp->quit;

脚本可能还比较的粗糙,欢迎大家提点建议...

阅读(3131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~