python - None of Paramiko's Read methods work for me? -
i have class i've written:
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): paramiko import autoaddpolicy, sshclient threading import thread 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: line in channel.readlines(): method(line) sleep(1) thread(target = monitor, args = (stdout, self.stdout)).start() thread(target = monitor, args = (stderr, self.stderr)).start()
then try running this:
>>> remote import remote >>> address = <removed> >>> username = 'root' >>> password = <removed> >>> r = remote(address, username, password) >>> r.sh('echo hello')
and no output. if change monitor method around instead of:
for line in channel.readlines(): method(line)
i have method(channel.read())
or method(channel.readline())
, in case, see:
out: err:
once second - never gives me expected results of:
out: hello
i know address, username, , password right, because can feed them fabric
fine.
>>> fabric.api import env >>> fabirc.operations import sudo >>> env.host_string, env.user, env.password = address, username, password >>> sudo('echo hello') [<omitted>]: hello
what doing wrong in paramiko
based class fabric
evidently able handle?
edit
i want method asynchronous. should return immediately. example, if this:
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')
then results should be:
out: hello out: hello out: world out: world
indicating 2 calls ran in parallel, not:
out: hello out: world out: hello out: world
which indicate 2 calls ran synchronously.
the problem while true
loop in monitor
prevents thread end. leaving first part , changing last lines to:
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()
will print output of given command line line, while there's returned.
Comments
Post a Comment