perl自动化批量执行脚本
发布时间:2020-12-15 21:10:25 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/perl#use strict;use feature 'say';use Net::SSH::Perl;use Net::SCP::Expect;use Net::OpenSSH;use Term::ReadKey;alarm 60;####服务器
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #!/usr/bin/perl #use strict; use feature 'say'; use Net::SSH::Perl; use Net::SCP::Expect; use Net::OpenSSH; use Term::ReadKey; alarm 60; ####服务器列表 my @server_list = ( { 'username'=>'root','host'=>'192.168.1.108'},{ 'username'=>'root','host'=>'192.168.1.109'},); #拷贝脚本至多台服务器,并执行该脚本,用了Net::SCP::Expect模块 sub autocopy { my($cmd) = @_; foreach my $element (@Ad) { my $host = $$element{'host'}; my $username = $$element{'username'}; my $password = $$element{'passwd'}; my $ssh = Net::SSH::Perl->new($host); $ssh->login($username,$password); my($stdout,$stderr,$exit) = $ssh->cmd($cmd); if(!$exit) { my $scp = Net::SCP::Expect->new('auto_yes'=>1);##一般第一次连接服务器的时候会出现"Are you sure you want to continue connecting (yes/no)?",这个'auto_yes'=>1 就是默认帮我们输入了yes $scp->login($username,$password); eval { $scp->scp('/root/bin/pubkey.sh',"$host:/root/bin/"); }; if([email?protected]) { print $host."n"; print [email?protected]; } } else { print $host.' cmd error'; exit; } } } ####根据用户名密码登入操作 sub autoexec { my($cmd) = @_; foreach my $element (@Ad) { my $host = $$element{'host'}; my $username = $$element{'username'}; my $password = $$element{'passwd'}; my $ssh = Net::SSH::Perl->new($host); $ssh->login($username,$exit) = $ssh->cmd($cmd); if($exit) { say $host; } } } ####通过加密过的密钥登入操作 sub auto_to_identity { my($cmd) = @_; ReadMode('noecho'); ###隐藏输入的密钥密码 print "Enter passphrase for keyfile '/root/.ssh/id_rsa':"; my $passphrase = ReadLine(0); chomp($passphrase); ReadMode('restore'); say ''; foreach my $element (@server_list) { my $host = $$element{'host'}; my $username = $$element{'username'}; my $key = '/root/.ssh/id_rsa'; my %param = ( user => $username,passphrase => $passphrase,key_path => $key,timeout => 10 ); my $ssh = Net::OpenSSH->new($host,%param); my ($stdout,$stderr) = $ssh->capture2($cmd); if($stdout){ say $stdout; } else { say $ssh->error; say $stderr; } } } my $cmd = 'mkdir /root/dd'; auto_to_identity($cmd); 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |