hello,
using Net::Openssh how can i use sudo to become some other user (say root) on a target machine and then execute a series of commands as root?
i looked and tried to use the expect example given on Net::Openssh page but could not make it to work.
my $myssh = Net::OpenSSH->new($host,
port => $SSHPORT,
user => $USER,
password => $PASS,)
now how can i execute a sudo on this, thus becoming a different user and, then execute bunch of commands on this handle?
please advice.
thank you.
Rajeev
CODE:
#!/usr/bin/perl
# see http://perlmonks.org/?node_id=890441
use strict;
use warnings;
use Net::OpenSSH;
use Expect;
#$Expect::Exp_Internal = 1;
@ARGV == 2 or die <<EOU;
Usage:
$0 host user_passwd
EOU
my $host = $ARGV[0];
my $pass1 = $ARGV[1];
my $ssh = Net::OpenSSH->new($host, passwd => $pass1);
$ssh->error and die "unable to connect to remote host: " . $ssh->error;
#$ssh->system("sudo -k");
my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, '/usr/local/bin/sudo', -p => 'runasroot:', 'su', '-')
or return "failed to attempt su: $!\n";
my $expect = Expect->init($pty);
$expect->log_file("expect.pm_log", "w");
$expect->expect(2,
[ qr/runasroot:/ => sub { shift->send("$pass1\n");} ], #use pass2 if using only su
[ qr/Sorry/ => sub { die "Login failed" } ]);
$expect->send("\n\n\n");
$expect->expect(2,[ qr/#/ => sub { shift->send("ls -l\n");} ]) #use pass2 if using only su
in above the ls command is not working.... if i do exp interactive that works fine....
________________________________
From: Rajeev Prasad <rp.n...@yahoo.com>
To: perl list <begi...@perl.org>
Sent: Friday, January 20, 2012 1:10 PM
Subject: Net::Openssh and sudo?
hello,
please advice.
thank you.
Rajeev
--
To unsubscribe, e-mail: beginners-...@perl.org
For additional commands, e-mail: beginne...@perl.org
http://learn.perl.org/
#!/usr/bin/perl
# see http://perlmonks.org/?node_id=890441
use strict;
use warnings;
use Net::OpenSSH;
use Expect;
$Expect::Exp_Internal = 1;
@ARGV == 2 or die <<EOU;
Usage:
$0 host user_passwd
EOU
my $host = $ARGV[0];
my $pass1 = $ARGV[1];
my $ssh = Net::OpenSSH->new($host, passwd => $pass1);
$ssh->error and die "unable to connect to remote host: " . $ssh->error;
#$ssh->system("sudo -k");
my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, '/usr/local/bin/sudo', -p => 'runasroot:', 'su', '-')
or return "failed to attempt su: $!\n";
my $expect = Expect->init($pty);
$expect->log_file("expect.pm_log", "w");
$expect->expect(2,
[ qr/runasroot:/ => sub { shift->send("$pass1\n"); exp_continue;} ], #use pass2 if using only su
[ qr/Sorry/ => sub { die "Login failed" } ],
[qr/#/ => sub { shift->send("ls -l\n"); exp_continue;}]
) or die "___Timeout!";
__END__
________________________________
From: Rajeev Prasad <rp.n...@yahoo.com>
To: perl list <begi...@perl.org>
Sent: Friday, January 20, 2012 4:14 PM
Subject: Re: Net::Openssh and sudo?
#!/usr/bin/perl
# see http://perlmonks.org/?node_id=890441
use strict;
use warnings;
use Net::OpenSSH;
use Expect;
$Expect::Exp_Internal = 1;
@ARGV == 2 or die <<EOU;
Usage:
$0 host user_passwd
EOU
my $host = $ARGV[0];
my $pass1 = $ARGV[1];
my $ssh = Net::OpenSSH->new($host, passwd => $pass1);
$ssh->error and die "unable to connect to remote host: " . $ssh->error;
#$ssh->system("sudo -k");
my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, '/usr/local/bin/sudo', -p => 'runasroot:', 'su', '-')
or return "failed to attempt su: $!\n";
my $expect = Expect->init($pty);
$expect->log_file("expect.pm_log", "w");
my @cmdlist =("ls -l","pwd","ls","who am i","id","whoami");
foreach my $cmd (@cmdlist){
$expect->expect(2,
[ qr/runasroot:/ => sub { shift->send("$pass1\n");} ], #use pass2 if using only su
[ qr/Sorry/ => sub { die "Login failed" } ],
[ qr/#/ => sub { shift->send("$cmd \n");}]
) or die "___Timeout!";
}
$expect->expect(2);
________________________________
From: Rajeev Prasad <rp.n...@yahoo.com>
To: perl list <begi...@perl.org>
Sent: Friday, January 20, 2012 4:26 PM
Subject: Re: Net::Openssh and sudo? made some progress: version 3
thx, but the installed sudo version does not have -S option. so i am not sure how will i then pass the password....
CU Sudo version 1.5.7p2
sudo -V | -h | -l | -v | -k | -H | [-b] [-p prompt] [-u username/#uid] -s | <command>
________________________________
From: Salvador Fandiño <sfan...@yahoo.com>
To: begi...@perl.org
Sent: Sunday, January 22, 2012 11:08 AM
Subject: Re: Net::Openssh and sudo?
https://metacpan.org/module/Net::OpenSSH#FAQ