in reply to Re^2: Problem using Net::OpenSSH->capture to su to another user
in thread Problem using Net::OpenSSH->capture to su to another user

Any additional ideas on how I can get this to work?

You could try using in conjuction with Expect;

#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Expect; select STDOUT; $| = 1; select STDERR; $| = 1; my $timeout = 5; my $password = ''; my $user = ''; my $host = ''; my $ssh = Net::OpenSSH->new(host=>$host, user=>$user, password=>$password); my ($pty, $pid) = $ssh->open2pty("sudo -k; sudo su - imail -c ls") or die "open2pty failed: " . $ssh->error . "\n"; my $expect = Expect->init($pty); $expect->raw_pty(1); $expect->expect($timeout, ':') or die "expect failed\n"; $expect->send("$password\n"); $expect->expect($timeout, "\n") or die "bad password\n"; while(<$pty>) { print "$. $_" }

poj

Replies are listed 'Best First'.
Re^4: Problem using Net::OpenSSH->capture to su to another user
by Anonymous Monk on Jun 13, 2014 at 11:50 UTC
    Similar question hope I can tag along with this thread. Need to run sudo -su nonroot and get a shell then run "cd /directory" and run a script with parameters in the directory like "./exec.sh parm1 file.lst" tried with
    @cmd = "cd /directory; ./exec.sh parm1 file.lst"; $ssh->capture2({tty=>1},'sudo', '-su', "$nonrootuser", '--', @cmd)
    The echo commands in script listed but no actual executes? Any advice?
      Show us how you do it from the command line.

      Do you need to enter any password?

        From shell sudo -su nonroot (NOPASSWD) as nonroot user cd /directory; ./exec.sh parameter file.lst i tried to run the sudo -u nonrootuser 'cd /directory; ./exec.sh parameter file.lst' get command not found