perl模块Net::SCP::Expect批量下发文件

作用一:可以分发一台机器,可以在线上测试用;

作用二:可以批量分发多台机器,统一部署;

见码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
use  Getopt::Std;
use  Net::SCP::Expect;
use  strict;
use  vars  qw($opt_s $opt_u $opt_H $opt_d $opt_p $opt_h) ;
getopts( 's:u:H:d:p:h' );
my  ( $src_file , $username , $Hostname , $dest_file , $passwd , $help , $jd );
&main ();
sub  main {
         get_args();
         scp_auto();
         }
sub  get_args {
         $src_file  $opt_s  if  $opt_s ;
         $username  $opt_u  if  $opt_u ;
         $Hostname  $opt_H  if  $opt_H ;
         $dest_file  $opt_d  if  $opt_d ;
         $passwd  $opt_p  if  $opt_p ;
         $help  $opt_h  if  $opt_h ;
         &Usage ()  if  (! defined ( $src_file ) || ! defined ( $username ) || ! defined ( $Hostname )
|| ! defined ( $dest_file ) || ! defined ( $passwd ));
         }
sub  scp_auto {
         if  (-f  $Hostname ) {
         open  (HD, "$Hostname" ) ||  die  "Cannot find file :$!\n" ;
         my  @jd =<HD>;
         close  (HD);
         foreach  $jd ( @jd ) {
         chomp  ( $jd );
  my   $scpe  = Net::SCP::Expect->new( host => $jd , user => "$username" , password => "$passwd" ,
auto_yes =>1, timeout =>200, verbose =>1, recursive =>1);
           print  "$jd ---------------------\n" ;
                 $scpe ->scp( "$src_file" , "$dest_file" );
         }
         else  {
my   $scpe  = Net::SCP::Expect->new( host => $Hostname , user => "$username" , password => "$passwd"
, auto_yes =>1, timeout =>200, verbose =>1, recursive =>1);
           print  "$Hostname ---------------------\n" ;
                 $scpe ->scp( "$src_file" , "$dest_file" );
         }
         }
sub  Usage{
         print  <<  "END" ;
           ./scp_patch.pl -s <src_file> -u <username> -H <Hostname or ip> -d <dest_file> -p <password>
END
         exit  2;
}
#############################################END##############################################

实例一:

1
./scp_batch.pl -s /root/install.log -u root -H  192.168 . 100.144  -d /tmp/c -p  123456

实例二:

1
2
3
4
5
./scp_batch.pl -s /root/install.log -u root -H t -d /tmp/c -p  123456
###t为文件######
[root@master test]# cat t
192.168 . 100.145
192.168 . 100.144



本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1325272
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值