您的位置:首页 > 编程语言 > PHP开发

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

2007-03-29 15:57 597 查看
最近没什么事情做,偷懒不想每次都登陆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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐