Twisted Conch SSHCommandClientEndpoint issues with ssh keys

23 Views Asked by At

I'm trying to open a basic ssh connection using conch and I'm using SSHCommandClientEndpoint to do so. And the connection works if I pass it a password, but I want to do it using ssh keys. However, anytime i pass in the keys argument it fails to authenticate. I can ssh into my machine using the keys on my own so I do not know what the issue is.

Here is my code:

from twisted.internet import protocol, defer, reactor, task
from twisted.conch.endpoints import SSHCommandClientEndpoint
from twisted.conch.ssh import keys

async def conchSSH():
    
    finished = defer.Deferred()

    class ShowOutput(protocol.Protocol):
        received = b""
        def dataReceived(self, data):
            self.received += data
        def connectionLost(self, reason):
            finished.callback(self.received)

    key = keys.Key.fromFile("id_rsa")
    endpoint = SSHCommandClientEndpoint.newConnection(reactor, "ls", "user", (i have my host here), 22, keys=[key])
    factory = protocol.Factory.forProtocol(ShowOutput)
    await endpoint.connect(factory)
    print("SSH response:", await finished)

task.react(lambda *a, **k: defer.ensureDeferred(conchSSH()))

but then i get this error

main function encountered error
Traceback (most recent call last):
  File "/home/user/latentbuildbot/master_branch/sandbox/lib/python3.10/site-packages/twisted/internet/defer.py", line 735, in errback
    self._startRunCallbacks(fail)
  File "/home/user/latentbuildbot/master_branch/sandbox/lib/python3.10/site-packages/twisted/internet/defer.py", line 798, in _startRunCallbacks
    self._runCallbacks()
  File "/home/user/latentbuildbot/master_branch/sandbox/lib/python3.10/site-packages/twisted/internet/defer.py", line 892, in _runCallbacks
    current.result = callback(  # type: ignore[misc]
  File "/home/user/latentbuildbot/master_branch/sandbox/lib/python3.10/site-packages/twisted/internet/defer.py", line 1792, in gotResult
    _inlineCallbacks(r, gen, status, context)
--- <exception caught here> ---
  File "/home/user/latentbuildbot/master_branch/sandbox/lib/python3.10/site-packages/twisted/internet/defer.py", line 1693, in _inlineCallbacks
    result = context.run(
  File "/home/user/latentbuildbot/master_branch/sandbox/lib/python3.10/site-packages/twisted/python/failure.py", line 518, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/home/user/latentbuildbot/master_branch/my_master/config/twistedconch.py", line 21, in conchSSH
    await endpoint.connect(factory)
twisted.conch.endpoints.AuthenticationFailed: Connection lost while authenticating
0

There are 0 best solutions below