写了一个查看FTP文件更新的工具

介绍了一个用Perl编写的实用程序,该程序可以根据配置文件自动检查指定FTP服务器上的文件更新情况,并将最近更新的文件及其时间输出到控制台及RESULT.txt文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近没什么事情做,偷懒不想每次都登陆FTP查看,就写了这么一个PERL程序。

命令格式:在cmd中运行,FtpFileFind-v1.0.exe ftp.txt
         ftp.txt为FTP服务器的配置文件,可以把自己想要查询的FTP服务器加进去,格式参 考原来的配置。
输出:cmd输出,并且输出到当前文件夹的一个RESULT.txt的文件中。

后语:此程序是PERL所写,就是想偷懒不用上FTP就知道最近更新了哪些文件,欢迎大家提出新的更新建议,方便大家使用。

提示:如果在cmd中不能显示中文,键入chcp 936后回车就ok!

#!/usr/bin/perl

print  "*********************************************************/n";
print  " FTP Find Tool/n";
print  " Build by YangKang /n";
print  "*********************************************************/n";
$ARGC=@ARGV;
if ($ARGC!=1) {
print  "/n Usage: $0 ftpListFile/n";
print  "/n Attention:Please make sure that your dictionary-file was exist/n";
print  "           or you have got the right address and port!/n";
print  "/n Good Luck!";
exit;
}

use Net::FTP;

#use Strict;
use Socket;
my $ftpfile=$ARGV[0];
my $logfile="RESULT.txt";
my $logo="UPDATE TIME:";
sub find_type
{
  my $path = shift;
  my $a=substr($path,0,1);
  return $a;
}

##########################################################
sub list
{
                my $current = $_[0];
                my @subdirs;
                $currenttmp=substr($current,0,2);
                if($currenttmp eq "//")
                {
                  $current=substr($current,1);
                }
                $ftp->cwd($current);
                my @dir=$ftp->dir($current);
                foreach(@dir)
                {
                  my $path=$ftp->pwd();
                  my $filetype=&find_type($_);
                  $filename=substr($_,55);
                  if(&find_type($_) eq "d")
                  {
                    if($filename ne ".")
                    {
                      if($filename ne "..")
                      {
                        $currentpath=$path . "/".$filename;
                        push @subdirs,$currentpath;

                      }
                    }
                  }
                  else
                  {
                    if(!($_ =~ /^total/))
                    {
                      #print "$_/n";
                      my @filetime=split //s+/,$_; #tiem of file
                      my $filemonth=$filetime[5];
                      my $fileday=$filetime[6];
                      my $filehour=$filetime[7];
                      my $temptime="$logo $filemonth $fileday $filehour";
                      $test=$ftp->mdtm($filename);
                      if($test>$now)
                      {
                        #print LOG "$_/n";
                        print $path."/$filename   $temptime/n";
                        print LOG ($path."/$filename   $temptime/n");
                      }
                    }
                  }
                }
                foreach (@subdirs)
                {

                    &list($_);
                }
}
##########################################################
#($name, $aliases, $proto) = getprotobyname('tcp');
#($name, $aliases, $port) = getservbyname($port, 'tcp') unless $port =~ /^/d+$/;

#read from file
open(USERFILE,$ftpfile)||die("Can't open the user's list!/n");
open(LOG,">RESULT.txt")||die("Can't open the result file!/n");
$now=time();
$now -= (24 * 3600*3 ); #day before yesterday
@data=<USERFILE>;
foreach(@data)
{
        my @a=split //s+/,$_;
        $address=shift(@a);
        $port=shift(@a);
        $username=shift(@a);
        $password=shift(@a);
        print "Testing on $address:$port username:$username;pass:$password .../n";
        print LOG ("/n");
        print LOG ("/n");
        print LOG ("==========new update file in FTP Server of $address==========/n");
        print LOG ("/n");
        print LOG ("/n");
        $ftp=Net::FTP->new($address,Port => $port);
        die "couldn't connect!/n" unless $ftp;
        $ftp->login($username,$password);
        if($ftp->cwd('/'))
        {
                print "FTP Server:$address can be connected now!/n";
                print "Analysing $address............/n";
                &list('/');
                print "Finish Analysing $address/n";
                print "############################################/n";

        }
        else
        {
                print "FTP Server:$address cann't be connected now!/n";
        }
        $ftp->quit();
}
close (USERFILE);
exit(0); 

ftp.txt格式:

ip(或者域名) port name password  中间以空格分隔。

如下:

xxxx.ihep.ac.cn 21 xxx xxx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值