首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我该怎么做(可以吗?)从命令中获取退出状态?

我该怎么做(可以吗?)从命令中获取退出状态?
EN

Stack Overflow用户
提问于 2013-04-05 23:00:49
回答 1查看 486关注 0票数 2

使用twisted.conch,我正在处理以下SSHChannel实现,该实现在实例化时会被提供一个运行的命令:

代码语言:javascript
复制
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可能是我进入的方式,但它看起来不像是这样的。

我在我的日志中看到了这一点,这似乎应该是一个提示,应该在哪里寻找,但我不确定如何遵循上述提示:

代码语言:javascript
复制
2013-04-05 10:39:37-0400 [SSHChannel session (0) on SSHService ssh-connection
    on CommandTransport,client] unhandled request for exit-status
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-05 23:42:36

为此,您可能需要实现两个额外的回调。这些都是通道上的回调,所以你已经很接近了。

第一个是request_exit_status。一旦您实现了这一点,Conch将不再记录未处理的exit-status请求。下面是一个示例实现:

代码语言:javascript
复制
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。下面是一个示例实现:

代码语言:javascript
复制
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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15837208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档