in reply to Problem using Net::OpenSSH->capture to su to another user

su doesn't change the UID of the current shell but creates a new one.

Try as follows:

my @out = $ssh->capture({ stdin_data => "$password\n" }, 'sudo', '-Sk', '-p', '', '--', 'su -c "ls" imail');

Replies are listed 'Best First'.
Re^2: Problem using Net::OpenSSH->capture to su to another user
by tim.culhane (Initiate) on Jun 04, 2014 at 08:37 UTC

    Hi Salva,

    Thanks for your response.

    Unfortunately when I try your code I get:

    sudo: sorry, you must have a tty to run sudo

    When I add:

    tty => 1,

    I get my original error back again:

    muxserver_accept_control: tcgetattr: Invalid argument tcgetattr: Invalid argument

    If I ssh to the remote server and manually issue some 'sudo su' commands I get output like the below:

    loguser@enfcdb01 ~$ sudo su imail We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. sudo password for loguser: imail@enfcdb01 loguser$

    So this works and I'm now able to operate as the imail user.

    Using the '-c' option to su I get:

    loguser@enfcdb01 ~$ sudo su -c "ls" imail ls: cannot open directory .: Permission denied

    Trying the same using the 'pwd' command:

    loguser@enfcdb01 ~$ sudo su -c "pwd" imail /home/loguser

    This seems to just report the current working directory of the loguser user, i.e. it has not changed to the imail home directory (which is perhaps expected with the '-c' option to su?)

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

    Thanks,

    Tim

      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
        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?
      sudo: sorry, you must have a tty to run sudo

      You are using an old version of sudo. Update it if you can.

      I get my original error back again:
      muxserver_accept_control: tcgetattr: Invalid argument tcgetattr: Inval +id argument

      This error is harmless, you can safely ignore it. Also, you can silence it adding the following option into the constructor call: master_stderr_discard => 1

      sudo su -c "pwd" imail /home/loguser
      This seems to just report the current working directory of the loguser user

      Add the flag -l to the su command: sudo su -lc "pwd" imail

        Hi again,

        Unfortunately the system is locked down so upgrading sudo is not an option. Anyway, adding tty >=1 seems to fix that error?

        I added the master_stderr_discard option to the constructor and one of the error lines has gone away:

        " muxserver_accept_control: tcgetattr: Invalid argument "

        but I still get one instance of

        " tcgetattr: Invalidargument "

        So, using the below code I still can't seem to get access to the imail account:

        my @out = $ssh->capture({ tty => 1, stdin_data => "$password\n" }, 'sudo', '-Sk', '-p', '', '--', 'su -lc "ls" imail');

        Any ideas as to what I'm still doing wrong?

        tim