Advertisement
Guest User

perl expect ssh

a guest
Feb 4th, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.50 KB | None | 0 0
  1. sub interactiveSsh
  2. {
  3.         my ($pConfig,$pass)=@_;
  4.         my $exp = new Expect;
  5.         $exp->slave->clone_winsize_from(\*STDIN);
  6.         $exp->spawn("ssh root\@".$pConfig->{'host'});
  7.  
  8.         my $spawn_ok;
  9.  
  10.         $exp->expect(40,
  11.                         [
  12.                                 qr'(yes/no)',
  13.                                 sub {
  14.                                         my $fh = shift;
  15.                                         $fh->send("yes\n");
  16.                                         exp_continue;
  17.                                 }
  18.                         ],
  19.                         [
  20.                                 qr'assword:',
  21.                                 sub {
  22.                                         if ($spawn_ok) {
  23.                                                 $exp->interact();
  24.                                         }
  25.                                         my $fh = shift;
  26.                                         $fh->send("$pass\n");
  27.                                         $spawn_ok=1;
  28.                                         exp_continue;
  29.                                 }
  30.                         ],
  31.                         [
  32.                                 qr'#',
  33.                                 sub {
  34.                                         my $fh = shift;
  35.                                         $fh->send("bash\n");
  36.                                         $exp->send("stty -echo\n");
  37.                                         $exp->interact();
  38.                                         exp_continue;
  39.                                 }
  40.                         ],
  41.                         [
  42.                                 eof =>
  43.                                 sub {
  44.                                         if ($spawn_ok) {
  45.                                                 print BOLD GREEN, "SSH close connexion to ".$pConfig->{'host'}.".\n", RESET;
  46.                                                 exit 0;
  47.                                         } else {
  48.                                                 die "ERROR: could not spawn ssh.\n";
  49.                                         }
  50.                                 }
  51.                         ],
  52.                         [
  53.                                 timeout =>
  54.                                 sub {
  55.                                         die "No login.\n";
  56.                                 }
  57.                         ],
  58.                         '-re', qr'[#>:] $',
  59.                 );
  60.                 $exp->interact();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement