首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过ssh与Twisted Conch连接时出错

通过ssh与Twisted Conch连接时出错
EN

Stack Overflow用户
提问于 2013-05-16 22:28:55
回答 1查看 752关注 0票数 0

我想使用DSA私钥或公钥通过ssh连接到远程服务器(公钥是由私钥生成的),但我有以下错误:

代码语言:javascript
复制
Disconnecting with error, code 14 reason: no more authentication methods available

这是我的脚本(扭曲的海螺):

代码语言:javascript
复制
#!/usr/bin/env python

from twisted.conch import error
from twisted.internet import defer, protocol, reactor
from twisted.conch.ssh import keys, userauth, connection, transport, channel, common
from twisted.python import log
import sys

class ClientTransport(transport.SSHClientTransport):

    def verifyHostKey(self, pubKey, fingerprint):
    return defer.succeed(1)

    def connectionSecure(self):
        self.requestService(ClientUserAuth('myusername', ClientConnection()))

private_key_file = "key_priv"
public_key_file = "key_pub"

class ClientUserAuth(userauth.SSHUserAuthClient):

    def getPassword(self, prompt=None):
        return

    def getPublicKey(self):
        return keys.Key.fromFile(public_key_file).keyObject

    def getPrivateKey(self):
        return defer.succeed(keys.Key.fromFile(private_key_file).keyObject)

class ClientConnection(connection.SSHConnection):

    def serviceStarted(self):
        self.openChannel(CatChannel(conn = self))

class CatChannel(channel.SSHChannel):

    name = 'session'

    def channelOpen(self, data):
        d = self.conn.sendRequest(self, 'exec', common.NS('cat'), wantReply = 1)
        d.addCallback(self._cbSendRequest)
        self.catData = ''

    def _cbSendRequest(self, ignored):
        self.write('This data will be echoed back to us by "cat."\r\n')
        self.conn.sendEOF(self)
        self.loseConnection()

    def dataReceived(self, data):
        self.catData += data

    def closed(self):
        print 'We got this from "cat":', self.catData

def main():
    hostname = "myhost"
    factory = protocol.ClientFactory()
    factory.protocol = ClientTransport
    reactor.connectTCP(hostname, 22, factory)
    log.startLogging(sys.stdout, setStdout=1)
    reactor.run()

if __name__ == "__main__":
    main()

下面是完整的日志:

代码语言:javascript
复制
[-] Log opened.
[ClientTransport,client] kex alg, key alg: diffie-hellman-group-exchange-sha1 ssh-rsa
[ClientTransport,client] outgoing: aes256-ctr hmac-sha1 none
[ClientTransport,client] incoming: aes256-ctr hmac-sha1 none
[ClientTransport,client] REVERSE
[ClientTransport,client] NEW KEYS
[ClientTransport,client] Key algorythm: ssh-rsa
[ClientTransport,client] starting service ssh-userauth
[SSHService ssh-userauth on ClientTransport,client] can continue with: ['publickey']
[SSHService ssh-userauth on ClientTransport,client] trying to auth with publickey
[SSHService ssh-userauth on ClientTransport,client] Disconnecting with error, code 14
reason: no more authentication methods available
[ClientTransport,client] connection lost
[ClientTransport,client] Stopping factory <twisted.internet.protocol.ClientFactory instance at 0x13c2f908>

因此,问题是我的代码出了什么问题,因为我可以使用OpenSSH SSH客户端和paramiko lib连接到我的服务器而不会出现任何错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-05 18:30:00

在ClientUserAuth类中修复:

代码语言:javascript
复制
def getPublicKey(self):
    return keys.Key.fromFile(public_key_file) 

相反,

代码语言:javascript
复制
def getPublicKey(self): 
    return keys.Key.fromFile(public_key_file).keyObject

代码语言:javascript
复制
def getPrivateKey(self):
    return defer.succeed(keys.Key.fromFile(private_key_file))

相反,

代码语言:javascript
复制
def getPrivateKey(self):
    return defer.succeed(keys.Key.fromFile(private_key_file).keyObject)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16590189

复制
相关文章

相似问题

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