Chinaunix首页 | 论坛 | 博客
  • 博客访问: 958239
  • 博文数量: 99
  • 博客积分: 3306
  • 博客等级: 中校
  • 技术积分: 1238
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-21 10:14
文章分类

全部博文(99)

文章存档

2012年(37)

2011年(56)

2010年(6)

分类: LINUX

2012-07-01 14:34:31

1.   Net::Ping,perl的ping模块,范例:


点击(此处)折叠或打开

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Net::Ping;
  4. sub ping_check{
  5.         my $dest=shift;
  6.         my $mp = Net::Ping->new("icmp");
  7.         if($mp->ping($dest,2)){
  8.                 print "$dest is alive\n";
  9.         }else {
  10.                 print "$dest is dead\n";
  11.         }

  12.         $mp->close;
  13. }
  14. while (my $host=<>){
  15.         chomp $host;
  16.         &ping_check($host);

  17. }

2.File::Copy 主要提供了copy和move函数


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Copy;
  5. my $filein=$ARGV[0];
  6. my $fileout=$ARGV[1];
  7. copy($filein,$fileout) or die "copy $filein to $fileout failed\n";
  8. move($fileout,"$fileout.test") or die "mv $fileout to $fileout.txt failed\n";

3.File::Rsync;


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Rsync;
  5. my $filein=$ARGV[0];
  6. my $fileout=$ARGV[1];
  7. &rsync_file($filein,$fileout);
  8. sub rsync_file{
  9.         my $localdir=shift;
  10.         my $remotedir=shift;
  11.         print "rsync file from $localdir to $remotedir\n";
  12.         my $obj = File::Rsync->new( { archive => 1, compress => 1 ,del=>1} );
  13.         $obj->exec( { src => $localdir, dest => $remotedir } ) or warn "rsync failed\n";

  14. }

需要注意在使用rsync的时候Rsync->new里面的参数del,del表示删除目标文件中有但是源文件没有的文件。

另外就是如果我们是同步两个目录,应该使用rsync dir1/ dir2这样的形式。

如果使用sync dir1 dir2的话,结果就是dir1被放到dir2下面去了,也不要使用rsync dir1/* dir2的形式,否则当dir1是空文件夹的时候会报错。

4.  Net::OpenSSH


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Net::OpenSSH;
  5. my $rhost=shift;
  6. my $cmd=shift;
  7. &remotessh_cmd($rhost,$cmd);

  8. sub remotessh_cmd{
  9.         my $host=shift;
  10.         my $cmd=shift;
  11.         chomp $host;
  12.         my $private_key_path='/root/.ssh/id_rsa';
  13.         my $ruser="root";
  14.         my %opt=("user"=>$ruser,"key_path"=>$private_key_path,"timeout"=>3,"kill_ssh_on_timeout" => 1);
  15.         my $ssh= Net::OpenSSH->new($host,%opt);
  16.         $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
  17.         my $out=$ssh->capture($cmd);
  18.         print "SSH result:$out";
  19.         $ssh->error and die "remote command failed:".$ssh->error;
  20. }

如果不是太在意执行结果的输出,可以直接使用 $ssh->system($cmd)来操作,需要注意执行返回值的问题,比如你强制杀掉某个进程,如果这个进程不存在了返回值就不是0,需要用$ssh->system("$cmd;exit0")这样的形式。

 

5.Getopt::Std

这个就不用多说了,解析参数用的。

getopts("f:s:ul:",\%options);

然后对$options{'f'}等等进行判断是否存在,如果存在的话那么-f指定的参数就是$options{'f'}。

6. Expect;

perl里面使用expect脚本也简单的,下面是用expect的签发证书的脚本中的一部分。


  1. my $exp = Expect->spawn ($cmd) or die "Cannot spawn : $cmd \n";
  2.        $exp->log_stdout(0);
  3.        $exp->log_file("expect.log");#记录整个文件
  4.        $exp->expect(30,
  5.                [ qr#Enter pass phrase for $choice{dir}/$choice{file}.key:#i => sub { my $exp = shift;
  6.                                      $exp->send("$choice{'passwd'}\r");
  7.                                      exp_continue; }],

  8.          .........................

  9.             )


 

阅读(6877) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

tom_linux2013-10-16 17:18:16

请问4.  Net::OpenSSH
如何通过$ssh->capture()执行多条命令呢?例如想远程备份文件,并替换文件内容
cp get.sh.bak.20131016
sed "s/name/NAME/g" get.sh > get.sh.bak
mv get.sh.bak get.shjavascript:void(0);