Question

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:

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

Here is my script (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.

Was it helpful?

Solution

Fixed in ClientUserAuth class:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top