简体   繁体   English

通过SSH与Twisted Conch连接时出错

[英]Error while connecting via ssh with Twisted Conch

I want to connect to remote server via ssh with DSA private or public key (public key has been generated from private key), but I have this error: 我想使用DSA私钥或公钥(已从私钥生成公钥)通过ssh连接到远程服务器,但是出现此错误:

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

Here is my script (Twisted Conch): 这是我的脚本(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()

And here is a full log: 这是完整的日志:

[-] 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>

So, the question is what's wrong with my code, because I can connect to my server without any errors using OpenSSH SSH client and paramiko lib. 所以,问题是我的代码有什么问题,因为使用OpenSSH SSH客户端和paramiko lib,我可以连接到服务器而没有任何错误。

Fixed in ClientUserAuth class: 已在ClientUserAuth类中修复:

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

instead 代替

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

and

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

instead 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM