【问题标题】:Python twisted ssh client: client blocks after sending 10 commandsPython扭曲的ssh客户端:发送10条命令后客户端阻塞
【发布时间】:2016-12-21 16:09:51
【问题描述】:

我最近开始使用twisted,我正在尝试创建一个客户端来连接并向SSH 服务器发送命令。 (我只是创建客户端并使用一些 SSH 服务器来测试它)。 在发送 10 个命令(例如“ls”命令)并收到每个命令的答案后,我的客户端被阻止。有人可以帮我找到解决方案吗?这是我的客户最重要的部分。 PS:我正在使用扭曲的 12.0.0(msi 二进制文件)。

class SimpleConnection(connection.SSHConnection):
    def serviceStarted(self):
        self.openChannel(CommandChannel(conn=self))

class CommandChannel(channel.SSHChannel):
    name = 'session'
    def channelOpen(self, data):
        global command
        command = "ls"
        d = self.conn.sendRequest(self, 'exec', common.NS(command), wantReply=True) 
        d.addCallback(self.dataReceived) 
    def dataReceived(self, data): 
        print (data)
    def closeReceived(self): 
        self.conn.openChannel(self)

【问题讨论】:

    标签: twisted twisted.conch


    【解决方案1】:

    问题是服务器阻塞了 10 号会话: "[SSHChannel session (10) on SSHService ssh-connection on SimpleTransport,client] 对方拒绝打开 原因:('打开失败',1)“。 这是正常行为,(sshd_config 中的 MaxSessions 指定每个网络连接允许的最大打开会话数设置为 10)。

    SSHChannel 总是在运行命令后自行关闭。因此,在关闭旧通道后,应该为新命令创建一个新通道。这是我的客户更正的最重要的部分:

    class SimpleConnection(connection.SSHConnection):
        def serviceStarted(self):
            self.openChannel(CommandChannel(conn=self)) 
    
    class CommandChannel(channel.SSHChannel):
        name = 'session'
        def channelOpen(self, data): 
            global command
            command = "ls"
            d = self.conn.sendRequest(self, 'exec', common.NS(command), wantReply=True) 
            d.addCallback(self.dataReceived) 
        def dataReceived(self, data):  
            print (data)
        def closed(self):
            self.conn.serviceStarted()
    

    【讨论】:

      猜你喜欢
      • 2011-05-30
      • 2012-09-13
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多