0

我想使用 DSA 私钥或公钥(公钥已从私钥生成)通过 ssh 连接到远程服务器,但出现此错误:

Disconnecting with error, code 14 reason: no more authentication methods available

这是我的脚本(Twisted Conch):

#!/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()

这是完整的日志:

[-] 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 连接到我的服务器而不会出现任何错误。

4

1 回答 1

1

在 ClientUserAuth 类中修复:

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

反而

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

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

反而

def getPrivateKey(self):
    return defer.succeed(keys.Key.fromFile(private_key_file).keyObject)
于 2013-06-05T10:30:00.497 回答