Paramiko的Read方法都不适合我吗?

时间:2016-04-21 20:55:39

标签: python fabric paramiko

我写了这堂课:

class Remote(object):
    def __init__(self, address, username, password):
        self.address  = address
        self.username = username
        self.password = password

    def stdout(self, s):
        print('out: ' + s)

    def stderr(self, s):
        print('err: ' + s)

    def sh(self, s):
        from paramiko  import AutoAddPolicy, SSHClient
        from threading import Thread
        from time      import sleep

        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.connect(self.address, username = self.username, password = self.password)
        stdin, stdout, stderr = ssh.exec_command(s)

        def monitor(channel, method):
            while True:
                for line in channel.readlines():
                    method(line)
                sleep(1)

        Thread(target = monitor, args = (stdout, self.stdout)).start()
        Thread(target = monitor, args = (stderr, self.stderr)).start()

然后我尝试这样运行:

>>> from remote import Remote
>>> address  = <removed>
>>> username = 'root'
>>> password = <removed>
>>> r = Remote(address, username, password)
>>> r.sh('echo Hello')

我没有输出。如果我更改监视器方法而不是:

for line in channel.readlines():
    method(line)

我只是method(channel.read())method(channel.readline()),但在这种情况下,我只看到:

out:
err:

一秒钟 - 它实际上从未给我预期的结果:

out: Hello

我知道我的地址,用户名和密码都没问题,因为我可以将它们输入fabric就好了。

>>> from fabric.api        import env
>>> from fabirc.operations import sudo
>>> env.host_string, env.user, env.password = address, username, password
>>> sudo('echo Hello')
[<omitted>]: Hello

paramiko基于fabric显然能够处理的课程,我做错了什么?

修改

我希望通过异步方法实现该方法。它应该立即返回。例如,如果我这样做:

r1 = Remote(<one set of credentials removed>)
r2 = Remote(<another set of credentials removed>)
r1.sh('echo Hello; sleep 5; echo World')
r2.sh('echo Hello; sleep 5; echo World')

然后结果应该是:

out: Hello
out: Hello
out: World
out: World

表示两个调用并行运行,而不是:

out: Hello
out: World
out: Hello
out: World

这表示两个调用同步运行。

1 个答案:

答案 0 :(得分:1)

问题是while True中的monitor循环会阻止线程结束。将第一部分保留为原样并将最后一行更改为:

def monitor(channel, method):
    while True:
        l = channel.readline()
        if l:
            method(l)
        else:
            break
tout = Thread(target = monitor, args = (stdout, self.stdout))
terr = Thread(target = monitor, args = (stderr, self.stderr))
tout.start()
terr.start()
tout.join()
terr.join()
ssh.close()

将逐行打印给定命令的输出,同时返回一些内容。