How is it different to start a service using paramiko in linux rather than executing another commands such as pkill?

677 Views Asked by At

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

0

There are 0 best solutions below