三月阳光 发表于 2018-8-31 10:35:20

perl远程执行多台服务器shell命令

#!/usr/bin/perl  use strict;
  use Net::SSH::Expect;
  my @ssh_list;
  my $ssh_txt='ip_list.txt';
  my $command_txt='command_txt.txt';
  open FH,$ssh_txt;
  while(){
  @ssh_list=split;
  print "正在登陆".$ssh_list."...\n";
  &ssh_conn("$ssh_list","$ssh_list","$ssh_list","$ssh_list");
  }
  close    FH;
  sub    ssh_conn(){
  my($host,$port,$user,$pass) = @_;
  my $ssh = Net::SSH::Expect->new(
  host      =>            $host,
  port      =>            $port,
  user      =>            $user,
  password                =>$pass,
  no_terminal         =>0,
  raw_pty =>1,
  timeout =>            3,
  );
  $ssh->debug(0);
  $ssh->run_ssh() or die "SSH process coundn't start:$!";
  $ssh->waitfor( '\(yes\/no\)\?$', 1 ); #交互式修改密码,给予2秒的时间
  $ssh->send("yes\n");
  $ssh->waitfor( 'password:\s*$/', 1);
  $ssh->send("$ssh_list");
  $ssh->send("su - root");
  $ssh->waitfor( 'password:\s*$/', 1);
  $ssh->send("$ssh_list");
  #$ssh->waitfor("#\s*",2);
  open F1,$command_txt;
  while(){
  my @command=split/\n/,$_;
  print "$command-->    ";
  $ssh->exec("$command");
  print "$ssh_list命令执行完毕\n";
  }
  close F1;
  $ssh->close();

页: [1]
查看完整版本: perl远程执行多台服务器shell命令