使用twisted.conch,我正在处理以下SSHChannel实现,该实现在实例化时会被提供一个运行的命令:
class CommandChannel(channel.SSHChannel):
name = 'session'
def __init__(self, command, *args, **kwargs):
channel.SSHChannel.__init__(self, *args, **kwargs)
self.command = command
def channelOpen(self, data):
d = self.conn.sendRequest(
self, 'exec', common.NS(self.command), wantReply=True)
d.addCallback(self._gotResponse)
def _gotResponse(self, _):
self.conn.sendEOF(self)
self.loseConnection()
def dataReceived(self, data):
log.msg('dataReceived: data=%r' % data)
def extReceived(self, dataType, data):
log.msg('extReceived: dataType=%r, data=%r' % (dataType, data))
def closed(self):
reactor.stop()为此,我提供了一个shell命令,该命令应该返回某种退出状态。我能读出退出状态是什么吗?我曾希望extReceived可能是我进入的方式,但它看起来不像是这样的。
我在我的日志中看到了这一点,这似乎应该是一个提示,应该在哪里寻找,但我不确定如何遵循上述提示:
2013-04-05 10:39:37-0400 [SSHChannel session (0) on SSHService ssh-connection
on CommandTransport,client] unhandled request for exit-status发布于 2013-04-05 23:42:36
为此,您可能需要实现两个额外的回调。这些都是通道上的回调,所以你已经很接近了。
第一个是request_exit_status。一旦您实现了这一点,Conch将不再记录未处理的exit-status请求。下面是一个示例实现:
def request_exit_status(self, data):
"""
When the server sends the command's exit status, record it for later
delivery to the protocol.
@param data: The network-order four byte representation of the exit
status of the command.
@type data: L{bytes}
"""
(status,) = unpack('>L', data)
if status != 0:
self._reason = ProcessTerminated(status, None, None)第二个是request_exit_signal。如果命令由信号终止,则发送exit-signal而不是exit-status。下面是一个示例实现:
def request_exit_signal(self, data):
"""
When the server sends the command's exit status, record it for later
delivery to the protocol.
@param data: The network-order four byte representation of the exit
signal of the command.
@type data: L{bytes}
"""
(signal,) = unpack('>L', data)
self._reason = ProcessTerminated(None, signal, None)这里的一般模式是,当SSHChannel接收到对foo的请求时,它将尝试使用构成请求的字节调用request_foo。
https://stackoverflow.com/questions/15837208
复制相似问题