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

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

  • Comment on Re^2: Problem using Net::OpenSSH->capture to su to another user

Replies are listed 'Best First'.
Re^3: Problem using Net::OpenSSH->capture to su to another user
by poj (Abbot) on Jun 04, 2014 at 10:57 UTC
    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?
        Show us how you do it from the command line.

        Do you need to enter any password?

Re^3: Problem using Net::OpenSSH->capture to su to another user
by salva (Canon) on Jun 04, 2014 at 09:38 UTC
    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

        What happens when you run the following command from your local machine command line?
        echo $SUDO_PASSWORD |ssh host "sudo -Sk -p '' -- su -lc ls imail"