清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
#!/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($@) {
print $host."\n";
print $@;
}
} 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,$password);
my($stdout, $stderr, $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);