perl ssh 여러 서버에 자동으로 로그인하고 종료 동작을 실행합니다


script 

 , 。  

  
  
  
  
  1. #!/usr/bin/perl -w 
  2. use strict; 
  3. use Net::SSH::Expect; 
  4.  
  5. open (AF, "host_list"or die "don't open host_list: $!
    "
  6. my @list; 
  7. foreach (<AF>) { 
  8.         chomp; 
  9.         @list=split(/\s/); 
  10.         SHUTDOWN("$list[0]","$list[1]","$list[2]","$list[3]"); 
  11. }; 
  12. close AF; 
  13.  
  14. sub SHUTDOWN { 
  15.  
  16.         my $host=shift; 
  17.         my $user=shift; 
  18.         my $password=shift; 
  19.         my $wait=shift; 
  20.         my $ssh=Net::SSH::Expect->new ( 
  21.                 host=>$host, 
  22.                 password=>$password
  23.                 user=>$user
  24.                 raw_pty=>1, 
  25.                 ); 
  26.         $ssh->debug(0); 
  27.  
  28.         $ssh->run_ssh() or die "SSH process couldn't start: $!"
  29.         sleep 3; 
  30. if ($ssh->waitfor('\(yes\/no\)\?', 2)) { 
  31.                 $ssh->send("yes
    "
    ); 
  32.         }; 
  33.  
  34.         $ssh->waitfor('password', 5); 
  35.         $ssh->send("$password"); 
  36.         $ssh->waitfor("$wait", 2); 
  37. #$ssh->exec("stty raw -echo"); 
  38.         my $cmd=$ssh->exec("init 0"); 
  39. #       print "$cmd

    "
  40.         print "$host: --------------------------------------------------
    "
  41.         $ssh->close(); 

 

host_list.txt  :

ip            
-----------------------------
10.13.55.11 root 1234 gaia
10.13.55.10 root aaa- rdfs
10.13.55.30 root bbb- dns
10.13.55.12 root 1234 dns
10.13.55.17 root 1234 dotproject
10.13.55.19 root cccc ftp

좋은 웹페이지 즐겨찾기