首页 | 新闻 | 交流 | 问吧 | 文档 | 手册 | 下载 | 博客

简单的Net::FTP模块的简单应用

作者:  时间: 2010-11-08
这是今天所用的 Net::FTP模块,由于这次所需要编写的脚本比较复杂,我把每个需求写到每个脚本里,然后在把每个脚本的功能,作为一个函数在整合到一起,下面是代码
 
 

#!/usr/bin/perl

use warnings;
use strict;
use Net::FTP;
use Log::Log4perl qw(get_logger); #use log4perl model

use Date::Simple ('date');
use File::Glob ':glob';#directory operation model


my $host="192.168.25.214";
my $usrname="root";
my $pwd="passwd";
my $path="/root/test/tttt";
my @ftpfilenames;
my @ppth;

&ftpdownload($host,$usrname,$pwd,$path);
print "ok!\n";

sub ftpdownload
{
my $newerr = 0;
my ($host,$username,$pwd,$path)=@_;
my $ftp = Net::FTP->new($host,Debug =>0) or $newerr=1;
if($newerr)
  {
  print "create ftp sever fail\n";
  return 0;
  }
$ftp->login($username,$pwd) or $newerr=1;
if($newerr)
  {
  print "login ftp sever fail\n";
  return 0;
  }
$ftp->cwd($path) or $newerr=1;
if($newerr)
  {
  print "change the path $path fail\n";
  return 0;
  }
$ftp->binary() or $newerr=1;
if($newerr)
  {
  print "change binary fail\n";
  return 0;
  }
#@ppth = $ftp->ls($path);

#foreach(@ppth)

# {

# my $ppath = $_;

# $ftp->cwd($ppath) or $newerr=1;

# if($newerr)

# {

# print "change! $ppath fail\n";

# return 0;

# }

# }

$ftp->binary() or $newerr=1;
if($newerr)
  {
  print "change binary fail\n";
  return 0;
  }

@ftpfilenames = $ftp->ls();


#my $oriname;

#my $getresult = $ftp->get($oriname);

foreach(@ftpfilenames)
  {
   my $filename = $_;
   my $oriname= $filename;
   my $getresult ="";
   my $destname = "$host\_$oriname";
   $getresult = $ftp->get($oriname,$destname);
  if(defined($getresult))
     {
      print "get $oriname success from $host\n";
     }
  else
    {
     print "get $oriname fail from $host\n";
    }
  }
$ftp->quit;
}


今天最大的体会就是发现

use warnings;
use strict;

这两行真的很重要,以后不可以不加它们了!