~ubuntu-branches/ubuntu/trusty/libnet-openssh-perl/trusty

« back to all changes in this revision

Viewing changes to sample/autosudo.pl

  • Committer: Package Import Robot
  • Author(s): Florian Schlichting
  • Date: 2012-02-22 23:35:55 UTC
  • Revision ID: package-import@ubuntu.com-20120222233555-j839vhem3058ewpx
Tags: upstream-0.57
ImportĀ upstreamĀ versionĀ 0.57

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
# see http://perlmonks.org/?node_id=890441
 
4
 
 
5
use strict;
 
6
use warnings;
 
7
 
 
8
use Net::OpenSSH;
 
9
use Expect;
 
10
 
 
11
@ARGV == 3 or die <<EOU;
 
12
Usage:
 
13
  $0 host user_passwd root_passwd
 
14
 
 
15
EOU
 
16
 
 
17
my $host = $ARGV[0];
 
18
my $pass1 = $ARGV[1];
 
19
my $pass2 = $ARGV[2];
 
20
 
 
21
my $ssh = Net::OpenSSH->new($host, passwd => $pass1);
 
22
$ssh->error and die "unable to connect to remote host: " . $ssh->error;
 
23
 
 
24
$ssh->system("sudo -K");
 
25
 
 
26
my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, 'sudo', -p => 'configtest:', 'bash', '-i')
 
27
    or return "failed to attempt sudo bash: $!\n";
 
28
 
 
29
my $expect = Expect->init($pty);
 
30
 
 
31
$expect->expect(2,
 
32
                [ qr/configtest:/ => sub { shift->send("$pass2\n"); exp_continue;} ],
 
33
                [ qr/Sorry/       => sub { die "Login failed" } ],
 
34
                [ qr/.*#\s+/      => sub { print shift->match }]
 
35
               ) or die "Timeout!";
 
36
 
 
37
$expect->interact();
 
38