加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 00:06:01 所属栏目:大数据 来源:网络整理
导读:作用一:可以分发一台机器,可以在线上测试用; 作用二:可以批量分发多台机器,统一部署; 见码: #!/usr/bin/perluse 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 ($

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

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

见码:


#!/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,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##############################################

实例一:

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

实例二:

./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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读