0

Following this post (Running Sudo Command with paramiko) I was able to run commands as sudo remotely. I can execute sudo pkill -2 pure-ftpd successfully, but when I try to execute sudo service pure-ftpd start I can't see any effect on the server although I see that the output in stdout and stderr is correct.

Here is my code:

class RemoteCmdSender(object):
    def __init__(self, host, usr=None, passwd=None):
        self.host = host
        self.usr = usr
        self.passwd = str(passwd)

    def send_cmd_as_bash(self, cmd):
        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())
        client.connect(hostname=self.host, username=self.usr,
                            password=self.passwd)
        transport = client.get_transport()
        session = transport.open_session()
        session.get_pty('bash')
        session.exec_command(cmd)
        stdin = session.makefile('wb', -1)
        stdin.write(self.passwd.strip() + '\n')
        stdin.flush()
        stdout = session.makefile('rb', -1).read()
        stderr = session.makefile_stderr('rb', -1).read()
        client.close()
        return stdout, stderr

and the execution:

print cmd_sender.send_cmd_as_bash("sudo service pure-ftpd")

output:

Starting ftp server: Running: /usr/sbin/pure-ftpd -l pam -l puredb:/etc/pure-ftpd/pureftpd.pdb -E -O clf:/var/log/pure-ftpd/transfer.log -8 UTF-8 -u 1000 -B\r\n

Which is consistent with the output that I get if I log to the server using ssh and write sudo service pure-ftpd start in the bash. PS: I want to make clear that both commands works correctly when run from an ssh session using bash

  • what do u mean by *can't see any effect on the server*? `get_pty('bash')` should be just `get_pty()`? – pynexj May 24 '17 at 01:45
  • Regarding get_pty, the docs does not specify the default value for it. – federicoferriff May 24 '17 at 11:59
  • I can actually see the output of the command in stdout but the service does not start (and I checked that it works if I enter with ssh and write the same command in bash). – federicoferriff May 24 '17 at 12:02
  • just tried and `get_pty('bash')` is the same as `get_pty(term='bash')` which would set `TERM=bash`. is that what you really want? – pynexj May 24 '17 at 12:09
  • try printing the exit status of the command. and what's in `stderr`? – pynexj May 24 '17 at 12:10
  • also try if `ssh user@host sudo service pure-ftpd start` works. and `ssh -t user@host sudo service pure-ftpd start`. – pynexj May 24 '17 at 12:20
  • stderr is empty. Anyway I found a strange behavior. If I add a "; ls" at the end of the cmd it works! Really no idea what could be happening. :( Though, I am thinking that it may be related somehow with pure-ftpd since it works for another service I implemented. – federicoferriff May 24 '17 at 12:41
  • It worked with -t parameter, not without it. Which is the difference? – federicoferriff May 24 '17 at 12:45
  • could you try `send_cmd_as_bash('sudo nohup service pure-ftpd start')`? – pynexj May 24 '17 at 12:45
  • It didn't start the service. nnohup: ignoring input and appending output to \xe2\x80\x98nohup.out\xe2\x80\x99\r\n' – federicoferriff May 24 '17 at 12:52
  • then i cannot explain for sure. `ssh -t` is similar to `get_pty()`. – pynexj May 24 '17 at 13:51

0 Answers0