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

sudo doesn't accept shell expressions as commands. Just simple commands. You can invoque the shell explicitly though:
sudo -u nonrootuser /bin/sh -c 'cd /directory; ./exec.sh'

Replies are listed 'Best First'.
Re^8: Problem using Net::OpenSSH->capture to su to another user
by Anonymous Monk on Jun 23, 2014 at 20:56 UTC
    Please advise:
    my $BASE='/test'; @cmd='ls -lt '.$BASE.'/Logs/*.log'; my $ssh = Net::OpenSSH->new("$user:$pass\@$host", master_opts => [ +-o => "StrictHostKeyChecking=no" ]) or warn "Can't ssh to $host: " . +$ssh->error; if ($ssh->error) {print runlog "SSH connection to $host failed: " +. $ssh->error; die ; } my($stdout, $stderr)=$ssh->capture2({tty=>1},@cmd); #my($stdout, $stderr)=$ssh->capture2(@cmd); if ($stderr) {print "get logs failed $stderr\n";}else{print "$stdo +ut\n$exit";} $ssh = Net::OpenSSH->new("$user:$pass\@$host", master_opts => [-o +=> "StrictHostKeyChecking=no" ]) or warn "Can't ssh to $host: " . $ss +h->error; my($stdout, $stderr, $exit)=$ssh->scp_get({glob=>1},$BASE.'/Logs/* +.log','/var/tmp'); if ($stderr) {print "get logs failed $stderr\n";}else{print "$stdo +ut\n$exit"
    The first command ran ok but for the scp_get the log file get scp'ied, but I'm getting : Killed by signal 1. # set_error(1 - control command failed: master ssh connection broken)
      Enable debugging:
      $Net::OpenSSH::debug = (~512);

      And post here the output.

        # io3 mloop, cin: 0, cout: 1, cerr: 0 # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 20 at offset 0 #> 45 78 69 74 20 72 65 71 75 65 73 74 20 73 65 6e 74 2e 0d 0a + | Exit request sent... # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 0 at offset 20 # leaving _io3() # _waitpid(19779) => pid: 19779, rc: # _kill_master: 19774 # waitpid(master: 19774) => pid: 19774, rc: # call args: ['ssh','-qtt','-S','/export/home/user/.libnet-openssh-per +l/user-mylinux-19767-362270','-l','user','mylinux0.qa.comp','--','ls +-l gLogs/*.log'] # open_ex: ['ssh','-qtt','-S','/export/home/user/.libnet-openssh-perl/ +user-mylinux-19767-362270','-l','user','mylinux0.qa.comp','--','ls -l + gLogs/*.log'] # io3 mloop, cin: 0, cout: 1, cerr: 1 # io3 fast, cin: 0, cout: 1, cerr: 1 # stdout, bytes read: 93 at offset 0 #> 2d 72 77 2d 72 2d 2d 72 2d 2d 20 31 20 66 6d 73 61 64 6d 36 30 20 6 +6 6d 73 20 31 30 35 30 32 20 | -rw-r--r-- 1 user usergrp 10502 #> 4a 75 6e 20 32 34 20 31 36 3a 32 37 20 2f 77 6f 72 6b 31 2f 64 36 2 +f 62 75 69 6c 64 73 2f 44 41 | Jun 24 16:27 #> 54 41 46 49 58 45 53 2f 4c 6f 67 73 2f 64 71 6c 5f 6f 75 74 70 75 7 +4 2e 6c 6f 67 0d 0a | /Logs/output.log.. # io3 fast, cin: 0, cout: 1, cerr: 1 # stdout, bytes read: 0 at offset 93 # stderr, bytes read: 0 # leaving _io3() # _waitpid(19780) => pid: 19780, rc: # DESTROY(Net::OpenSSH=HASH(0x285b710), pid: 19768) # killing master # sending exit control to master # call args: ['ssh','-O','exit','-T','-S','/export/home/user/.libnet-o +penssh-perl/user-mylinux-19767-463633','-l','user','mylinux0.qa.comp' +,'--'] # open_ex: ['ssh','-O','exit','-T','-S','/export/home/user/.libnet-ope +nssh-perl/user-mylinux-19767-463633','-l','user','mylinux0.qa.comp',' +--'] -rw-r--r-- 1 user usergrp 10502 Jun 24 16:27 Logs/output.log # io3 mloop, cin: 0, cout: 1, cerr: 0 # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 20 at offset 0 #> 45 78 69 74 20 72 65 71 75 65 73 74 20 73 65 6e 74 2e 0d 0a + | Exit request sent... # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 0 at offset 20 # leaving _io3() # _waitpid(19781) => pid: 19781, rc: # _kill_master: 19768 # waitpid(master: 19768) => pid: 0, rc: # waitpid(master: 19768) => pid: 19768, rc: Interrupted system call Killed by signal 1. # DESTROY(Net::OpenSSH=HASH(0x285c238), pid: 19777) # killing master # sending exit control to master # set_error(1 - control command failed: master ssh connection broken) # _kill_master: 19777 # waitpid(master: 19777) => pid: 0, rc: No such file or directory # waitpid(master: 19777) => pid: 19777, rc: Interrupted system call