1. File::Find 模块
use File::Find 使用该模块,这时一个find的新函数就可以使用了
find subref, dirlis find的第二个参数是要搜索的目录。第一个参数是子例程引用。
$File::Find::name 当前路径名,包括目录和文件名
$File::Find:dir 当前目录名
#! /usr/bin/perl -w 这是一个删除/tmp目录下文件的脚本
use strict;
use File::Find;
sub wanted {
if (-f $File::Find::name) {
print "Remove $File::Find::name\n";
unlink $File::Find::name; 删除文件
}
}
find(\&wanted,'/tmp');
======================
#! /usr/bin/perl -w
use strict;
use File::Find;
sub wanted{
if (-f $File::Find::name) {
if ($File::Find::name =~ /\.tmp$/i) {
print "Remove $File::Find::name\n";
unlink $File::Find::name;
}
}
}
find(\&wanted,'c:/','d:/');
=== =============================
2.File::Copy 模块
use File::Copy;
copy("sourcefile","destination") || warn "Could not copy file: $!";
#! /usr/bin/perl -w
use File::Copy;
copy('/tmp/a','/root/test') || warn "Can not cp file: $!";
=====================
File::Copy模块也提供了一个move函数。将一个文件从一个目录移到另外一个目录里。
#! /usr/bin/perl -w
use File::Copy;
move('/tmp/a','/root/test/b') || warn "Can not cp file: $!"; 将/tmp目录里的a文件移到/test目录里并改名为b
======================
#! /usr/bin/perl -w
use File::Copy;
if (not move("import.doc" "d:/archives/import.doc")) {
warn "import.doc could not e moved: $!";
unlink "d:/archives/import.doc";
}
=================================
3. Net::Ping 模块
use Net::Ping
Net::Ping模块提供了一个pingecho函数,该函数有两个参数,第一个参数是要查找的主机,第二个参数是指明pingecho应该等待多长时间才能收到对方应答,以秒为单位
#! /usr/bin/perl -w
use Net::Ping;
if (pingecho("www.baidu.com",10)) {
print "baidu is on the network\n";
} else {
print "baidu is unreachable.\n";
}
===================
#! /usr/bin/perl -w
use strict;
use Net::Ping;
for(my $i=1;$i<=10;$i++) {
my $a="192.168.1.$i";
if (pingecho($a,10)) {
print "$a is up\n";
print "-" x 20,"\n";
} else {
print "$a is down\n";
print "-" x 20,"\n";
}
}
================================
use Net::Ping;
use strict;
my @host=qw(192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5
192.168.1.7 192.168.1.8 192.168.1.9 192.168.1.11 192.168.1.12
192.168.1.13 192.168.1.14 192.168.1.15 192.168.1.16 192.168.1.21
192.168.1.22 192.168.1.30 192.168.1.31 192.168.1.35);
foreach my $host (@host) {
my $p=Net::Ping->new("icmp");
if ($p->ping("$host",5)) {
print "$host is up.\n";
} else {
print "$host is down.\n";
}
}
system('pause');
===================================
4.Net::FTP 模块
use Net::FTP;
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: [email protected]";
$ftp->login("anonymous",'[email protected]')
or die "Cannot login ", $ftp->message;
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
$ftp->get("that.file")
or die "get failed ", $ftp->message;
$ftp->quit;
#! /usr/bin/perl
use Net::FTP
$ftp=Net::FTP->new("192.168.1.1",Debug=>0) or die "Cannot connect FTP";
$ftp->login('username','password') or die "Can not login",$ftp->message;
$ftp->cwd("/var/pub/") or die "Can not change working directory" #切换FTP的工作目录
chdir "/home/linux"; #更改本地目录(用于接收文件)
$ftp->get("passwd") or die "get failed",$ftp->message; #获取指定文件
$ftp->quit; #退出
请发表评论