asyncssh icon indicating copy to clipboard operation
asyncssh copied to clipboard

Connect to Cisco ssh

Open runtel opened this issue 3 years ago • 2 comments

Hi all! I try connect to cisco cp-6961. After ssh auth phone open some shell for internal auth (interactive).

import asyncio, asyncssh, sys
import logging
from typing import Optional
asyncssh.set_debug_level(2)

logging.basicConfig(level=logging.DEBUG)

phone_pass = "3176639538"

class MySSHClientSession(asyncssh.SSHClientSession):
    def data_received(self, data: str, datatype: asyncssh.DataType) -> None:
        print(data, end='')

    def connection_lost(self, exc: Optional[Exception]) -> None:
        if exc:
            print('SSH session error: ' + str(exc), file=sys.stderr)

async def run_client() -> None:
    async with asyncssh.connect('192.168.87.232',
                                password='a3fc7d39572b',
                                username='6c1fb1273862',
                                encryption_algs=['aes128-cbc'],
                                preferred_auth=("password"),
                                request_pty=True,
                                known_hosts=None) as conn:
        print("Connected", conn)
        chan, session = await conn.create_session(MySSHClientSession)
        await chan.wait_closed()

try:
    asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
    sys.exit('SSH connection failed: ' + str(exc))

After "Connected" session closed. Help please)

-512,hmac-sha1,[email protected],[email protected],[email protected],[email protected],[email protected]
DEBUG:asyncssh:[conn=0]   Compression algs: [email protected],none
DEBUG:asyncssh:[conn=0] Received key exchange request
DEBUG:asyncssh:[conn=0]   Key exchange algs: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1
DEBUG:asyncssh:[conn=0]   Host key algs: ssh-rsa
DEBUG:asyncssh:[conn=0]   Client to server:
DEBUG:asyncssh:[conn=0]     Encryption algs: aes128-cbc,aes192-cbc,aes256-cbc,3des-cbc,des-cbc,des-cbc,none
DEBUG:asyncssh:[conn=0]     MAC algs: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,none
DEBUG:asyncssh:[conn=0]     Compression algs: none
DEBUG:asyncssh:[conn=0]   Server to client:
DEBUG:asyncssh:[conn=0]     Encryption algs: aes128-cbc,aes192-cbc,aes256-cbc,3des-cbc,des-cbc,des-cbc,none
DEBUG:asyncssh:[conn=0]     MAC algs: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,none
DEBUG:asyncssh:[conn=0]     Compression algs: none
DEBUG:asyncssh:[conn=0] Beginning key exchange
DEBUG:asyncssh:[conn=0]   Key exchange alg: diffie-hellman-group14-sha1
DEBUG:asyncssh:[conn=0]   Client to server:
DEBUG:asyncssh:[conn=0]     Encryption alg: aes128-cbc
DEBUG:asyncssh:[conn=0]     MAC alg: hmac-sha1
DEBUG:asyncssh:[conn=0]     Compression alg: none
DEBUG:asyncssh:[conn=0]   Server to client:
DEBUG:asyncssh:[conn=0]     Encryption alg: aes128-cbc
DEBUG:asyncssh:[conn=0]     MAC alg: hmac-sha1
DEBUG:asyncssh:[conn=0]     Compression alg: none
DEBUG:asyncssh:[conn=0] Requesting service ssh-userauth
DEBUG:asyncssh:[conn=0] Completed key exchange
DEBUG:asyncssh:[conn=0] Request for service ssh-userauth accepted
INFO:asyncssh:[conn=0] Beginning auth for user 6c1fb1273862
DEBUG:asyncssh:[conn=0] Remaining auth methods: publickey,password
DEBUG:asyncssh:[conn=0] Preferred auth methods: password
DEBUG:asyncssh:[conn=0] Trying password auth
INFO:asyncssh:[conn=0] Auth for user 6c1fb1273862 succeeded
Connected <asyncssh.connection.SSHClientConnection object at 0x7fcf7b554e10>
DEBUG:asyncssh:[conn=0, chan=0] Set write buffer limits: low-water=16384, high-water=65536
INFO:asyncssh:[conn=0, chan=0] Requesting new SSH session
DEBUG:asyncssh:[conn=0, chan=0]   Initial recv window 2097152, packet size 32768
DEBUG:asyncssh:[conn=0, chan=0]   Initial send window 65536, packet size 32768
INFO:asyncssh:[conn=0, chan=0]   Interactive shell requested
INFO:asyncssh:[conn=0] Connection lost
INFO:asyncssh:[conn=0, chan=0] Closing channel due to connection close
SSH session error: Connection lost
INFO:asyncssh:[conn=0, chan=0] Channel closed: Connection lost
INFO:asyncssh:[conn=0] Closing connection
INFO:asyncssh:[conn=0] Sending disconnect: Disconnected by application (11)
SSH connection failed: Connection lost

runtel avatar Jun 09 '22 14:06 runtel

Thanks for posting the sample code and log information. Unfortunately, I don't see much which looks out of the ordinary. The key exchange completes just fine, as does the user authentication and the sending of the request for an interactive shell. It looks like it closes the connection. Perhaps the username you are using is not authorized to open a shell? Alternately, could the problem be that you are requesting a PTY but not providing a terminal type? Normally, instead of request_pty=True, you just specify something like term_type='xterm' and AsyncSSH uses that to decide whether or not to request a PTY. I've never tried requesting a PTY without providing a terminal type.

If you connect to the target with the same username/password using OpenSSH as the client, does it succeed? If so, could you provide the log output from running that with "-vvv" so we can try to compare the differences between the two?

Also, are there any logs you can look at on the target device which might indicate why it is closing the connection?

ronf avatar Jun 10 '22 03:06 ronf

@ronf Thanks for your discription. I will be able to do the tests after July 20th. I'm sorry for the wait.

runtel avatar Jun 14 '22 06:06 runtel

Closing due to inactivity, but feel free to re-open if you'd like to provide additional information.

ronf avatar Jun 22 '23 03:06 ronf