Hi
I have the following code.. that executes remote command. The problem is ..
when the remote host goes down. The select on channel does not seem to be
getting in to error. (The command is a long running command... without much
output...like copying large files..etc.)
Attached is a code snippet.
I tried adding channel to the error descriptor as follows,
select([chan,], [], [chan,], timeout)
and check for it to be non-empty.. but does not seem to trigger.
Any suggestion on how to work around this ? Is it that unless there is some
exchange... the error will not trigger?
(Note : As the commands are expected to be long running.. it is bit difficult
to estimate the time out... )
Thanks
/Jd
def remote_exec_cmd(self, cmd, timeout=None,params = None):
"""
Open channel on transport, run remote command,
returns (stdout/stdin,process exit code)
"""
out = ''
exit_code = -98
if self.ssh_transport is None:
raise CommandException(0, "Transport not initialized")
chan = None
try:
try:
chan = self.ssh_transport.open_session()
if not chan:
raise Exception("remote_exec_cmd :Could not create channel")
chan.set_combine_stderr(True)
chan.setblocking(0)
chan.settimeout(socket.getdefaulttimeout())
x = chan.exec_command(to_str(cmd))
if params is not None:
for param in params:
chan.send(param+"\n")
chan.shutdown_write()
### Read when data is available
timedout = False
while True:
try:
# This one needs a timeout.. need to think about
# but can not be small as the app may take more time
# to process and return result.
if select.select([chan,], [], [], timeout) ==
([],[],[]):
timedout = True
print "remote command timedout"
out = "__TIMEOUT__" + out
exit_code = -99
break
x = chan.recv(1024)
if not x: break
out += x
select.select([],[],[],.1)
except (socket.error,select.error), e:
if (type(e.args) is tuple) and (len(e.args) > 0) and \
((e.args[0] == errno.EAGAIN or e.args[0] == 4)):
try:
select.select([],[],[],.1)
except Exception, ex:
pass
continue
else:
raise
if not timedout:
exit_code = chan.recv_exit_status()
#chan.close()
except SSHException, ex:
raise CommandException(0, to_str(ex))
finally:
if chan:
try:
chan.close()
except Exception, ex:
pass
return out, exit_code
_______________________________________________
paramiko mailing list
[email protected]
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko