配置文件:
===================================================
[AUTO_FTP_DOWN]
#远程下载FTPIP
HOST_IP_DOWN=127.0.0.1
#远程下载FTP用户名
HOST_USER_DOWN=11
#远程下载FTP密码
HOST_PWD_DOWN=11
#远程FTP下载目录,从这个目录GET文件
ROMOTE_DOWN_PATH=/remote/down
#存放从远程FTP下载的文件
LOCAL_DOWN_PATH=D:/uptest/local/down
#是否下载成功后是否删除远程FTP中下载成功的文件
#0--不删除
#1--删除
DELETE_REMOTE_FILE=0
#上传文件匹配规则(Perl正则表达式)
UP_RULE=/d{8}/.034/.PYRST
[AUTO_FTP_UP]
#远程上传FTPIP
HOST_IP_UP=127.0.0.1
#远程上传FTP用户名
HOST_USER_UP=11
#远程上传FTP密码
HOST_PWD_UP=11
#远程FTP上传目录,向这个目录PUT文件
ROMOTE_UP_PATH=/remote/up
#本地路径,存放需要上传到远程FTP的文件
LOCAL_UP_PATH=D:/uptest/local/up
#如果配置了备份目录则将在文件上传后备份到这个目录,否则直接删除
BACK_LOCAL_PATH=D:/uptest/local/upbak
#下载文件匹配规则(Perl正则表达式)
DOWN_RULE=/d{8}/_034/_/d{4}/.ECPAY
#autoftp.pl -f autoftp1.ini -u -s -d
===================================================
脚本:
===================================================
#!C:/Perl/bin/perl.exe -w
use strict;
use Net::FTP;
use File::Spec;
use File::Copy;
use Getopt::Std;
use Data::Dumper;
our $VERSION=qw(0.1.6);
our %ini_file;
#读ini取配置文件
sub ini_read {
my %ini_param;
my $hashref;
open( MYINI, $_[0] ) or die "打开配置文件错误:$!";
while (<MYINI>) {
next if (substr($_,0,1) eq '#');
if ( my ($key) = /^/[(.+)/]$/ ) {
$hashref = $ini_param{$key} = {};
}
elsif ( my ( $k, $v ) = /(/S+)/s*=/s*(.*)/ ) {
$hashref->{$k} = $v;
}
}
close(MYINI) or die "关闭配置文件错误:$!";
return %ini_param;
}
#将文件下载到本地路径
sub get_remote_file{
print '------------------------------------------',"/n",'开始从远程FTP获取文件',"/n";
my $ftp = Net::FTP->new($ini_file{AUTO_FTP}{HOST_IP_DOWN}, Debug => 0)
or die "cannot connect ftp server: $ini_file{AUTO_FTP}{HOST_IP_DOWN}; $@";
$ftp->login($ini_file{AUTO_FTP}{HOST_USER_DOWN}, $ini_file{AUTO_FTP}{HOST_PWD_DOWN})
or die 'Cannot login :', $ftp->message;
$ftp->binary;
chdir File::Spec->catdir($ini_file{AUTO_FTP}{LOCAL_DOWN_PATH})
or die "cannot change local download directory:$ini_file{AUTO_FTP}{LOCAL_DOWN_PATH}; $!/n";
$ftp->cwd($ini_file{AUTO_FTP}{ROMOTE_DOWN_PATH})
or die "Cannot change remote working directory:$ini_file{AUTO_FTP}{ROMOTE_DOWN_PATH};", $ftp->message;
my @remote_file_list = $ftp->ls($ini_file{AUTO_FTP}{ROMOTE_DOWN_PATH});
foreach my $file_name (@remote_file_list){
#print $file_name,"/n";
my ($v, $d, $f) = File::Spec->splitpath($file_name);
if ( $f =~ m/$ini_file{AUTO_FTP}{DOWN_RULE}/ ){
my $l_file = File::Spec->catfile($ini_file{AUTO_FTP}{LOCAL_DOWN_PATH},$f);
if ( -e $l_file && -f $l_file ){
#rename $l_file,$l_file.'-'.now_time(2);
#print "将重名文件备份为:[",$l_file.'-'.now_time(2),"]/n";
print "存在重名文件$f,不再下载!/n";
next;
}
$ftp->get($f) or die "get file failed:$f ", $ftp->message;
my $file_aize = $ftp->size($f);
print "已经下载了文件:[",$l_file,"], 文件大小: [$file_aize]","/n";
#删除远程目录文件
$ftp->delete($f) if ($ini_file{AUTO_FTP}{DELETE_REMOTE_FILE} == 1);
}
}
$ftp->quit;
print '------------------------------------------',"/n",'从远程FTP获取文件结束',"/n";
}
#将文件上传到远程路径
sub put_local_file{
print '------------------------------------------',"/n",'开始上传文件到远程FTP',"/n";
my ( %remote_file_name );
my $ftp = Net::FTP->new($ini_file{AUTO_FTP}{HOST_IP_UP}, Debug => 0)
or die "cannot connect ftp server: $ini_file{AUTO_FTP}{HOST_IP_UP}; $@";
$ftp->login($ini_file{AUTO_FTP}{HOST_USER_UP}, $ini_file{AUTO_FTP}{HOST_PWD_UP})
or die 'Cannot login :', $ftp->message;
$ftp->binary;
chdir $ini_file{AUTO_FTP}{LOCAL_UP_PATH}
or die "cannot change local download directory:$ini_file{AUTO_FTP}{LOCAL_UP_PATH}; $!/n";
$ftp->cwd($ini_file{AUTO_FTP}{ROMOTE_UP_PATH})
or die "Cannot change remote working directory:$ini_file{AUTO_FTP}{ROMOTE_UP_PATH};", $ftp->message;
my @remote_file_list = $ftp->ls($ini_file{AUTO_FTP}{ROMOTE_UP_PATH});
foreach (@remote_file_list){
#print $_,"/n";
$remote_file_name{$_} = 1;
}
#print Dumper %remote_file_name;
my @local_file = <*>;
if ($#local_file < 0){
print "本地没有文件需要上传!$ini_file{AUTO_FTP}{LOCAL_UP_PATH}/n";
return -1;
}
foreach my $l_file (@local_file){
if ($l_file =~ m/$ini_file{AUTO_FTP}{UP_RULE}/){
print "本地有需要上传的文件:$l_file/n";
if (defined $remote_file_name{$l_file} && $remote_file_name{$l_file} == 1){
print "服务器上出现同名文件!请检查文件名是否正确!/n";
return -1;
}
$ftp->put($l_file) or die "put file failed:$l_file;", $ftp->message;
my $file_aize = $ftp->size($l_file);
print "已经上传了文件:[",$l_file,"], 文件大小: [$file_aize]","/n";
#如果配置了备份目录,则进行备份,否则直接删除
if (defined $ini_file{AUTO_FTP}{BACK_LOCAL_PATH}){
my $file_path = File::Spec->catfile($ini_file{AUTO_FTP}{LOCAL_UP_PATH},$l_file);
my $bak_dir = $ini_file{AUTO_FTP}{BACK_LOCAL_PATH};
move $file_path,$bak_dir or die "不能移动文件,$!/n";
print "已经备份文件$file_path到目录:$bak_dir/n";
}
else{
my $file_path = File::Spec->catfile($ini_file{AUTO_FTP}{LOCAL_UP_PATH},$l_file);
unlink $file_path or die "delete file failed:$file_path; $!/n";
}
}
}
$ftp->quit;
print '------------------------------------------',"/n",'上传文件到远程FTP结束',"/n";
}
#获取当前时间
sub now_time {
my ($_r_type) = @_;
my ($__now_time);
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime;
$year += 1900 if ( $year >= 100 );
$mon++;
if ( $_r_type == 1 || !defined $_r_type ){
$__now_time = sprintf( "%4s-%02s-%02s %02s:%02s:%02s",
$year, $mon, $mday, $hour, $min, $sec );
}
elsif( $_r_type == 2 ){
$__now_time = sprintf( "%4s%02s%02s_%02s%02s%02s", $year, $mon, $mday, $hour, $min, $sec );
}
elsif( $_r_type == 3 ){
$__now_time = sprintf( "%02s%02s", $hour, $min );
}
else{
die "未知的返回时间类型!/$_r_type=$_r_type/n";
}
return $__now_time;
}
sub p_usage {
my $u=<<"END";
Usage:
autoftp [-u] [-d] [-f IniFileName]
-u 进行上传
-d 进行下载
-f filename 配置文件
-s 关闭调试模式,当未设置时信息会打印在屏幕,并且等待输入回车退出
本程序不写日志,出现异常请直接运行一下程序
zdl0812/@163.com Version ${VERSION}
END
print $u;
print "/n/n输入回车退出程序!/n";
<>;
exit;
}
print now_time(1),"/n";
my %CmdOpertion;
getopts('sudf:', /%CmdOpertion);
$CmdOpertion{f} = $CmdOpertion{f} || './autoftp.ini';
my $OptNum = keys %CmdOpertion;
if ($OptNum <= 1){
print "/$OptNum=$OptNum/n";
&p_usage;
exit -1;
}
if ( -e $CmdOpertion{f} && -f $CmdOpertion{f} ){
%ini_file = &ini_read($CmdOpertion{f});
#print Dumper %CmdOpertion;
#print Dumper %ini_file;
get_remote_file() if defined $CmdOpertion{d}; #从远程获取文件
put_local_file() if defined $CmdOpertion{u}; #上传文件到远程目录
if (!defined $CmdOpertion{s}){
print "/n/n输入回车退出程序!/n";
<>;
exit;
}
}
else{
print "找不到配置文件:$CmdOpertion{f}";
&p_usage;
exit -1;
}