paramiko python module hangs at stdout.read() -
i using below code:
import paramiko def runsshcmd(hostname, username, password, cmd, timeout=none): client = paramiko.sshclient() client.set_missing_host_key_policy(paramiko.autoaddpolicy()) client.connect(hostname, username=username, password=password, allow_agent=false, look_for_keys=false, timeout=timeout) stdin, stdout, stderr = client.exec_command(cmd) stdin.flush() data = stdout.read() print (data) client.close() runsshcmd("10.128.12.32", "root", "c0mput3gr!d", "ts_menu")
when comes stdout.read() , hangs... prints output after long time.
can please suggest if can done issue??
i see issue has been reported in :
https://bugs.python.org/issue24026
is there better module in python ssh connection , run commands ??
could related https://github.com/paramiko/paramiko/issues/109
below explanation of facing , how worked around it.
i experienced issue due stdout.channel.eof_received == 0
import paramiko client = paramiko.sshclient() client.set_missing_host_key_policy(paramiko.autoaddpolicy()) client.connect("1.1.1.1", username="root", password="pass") stdin, stdout, stderr = client.exec_command("service xxx start")
stdin, stdout , stderr staying open...
>>> print stdin <paramiko.channelfile <paramiko.channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.transport @ 0x17eff90l (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> >>> print stdout <paramiko.channelfile <paramiko.channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.transport @ 0x17eff90l (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> >>> print stderr <paramiko.channelfile <paramiko.channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.transport @ 0x17eff90l (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
so eof not received...
>>> print stdin.channel.eof_received 0
usually receive true , can stdout.read(), safe use workaround (which works!): wait timeout, force stdout.channel.close() , stdout.read():
>>> timeout = 30 >>> import time >>> endtime = time.time() + timeout >>> while not stdout.channel.eof_received: ... sleep(1) ... if time.time() > endtime: ... stdout.channel.close() ... break >>> stdout.read() 'starting xxx: \n[ ok ]\rprogram started . . .\n' >>>
btw use:
python 2.6.6 paramiko (1.15.2)
hope helps...
Comments
Post a Comment