ronf / asyncssh

AsyncSSH is a Python package which provides an asynchronous client and server implementation of the SSHv2 protocol on top of the Python asyncio framework.
Eclipse Public License 2.0
1.54k stars 150 forks source link

Problem connecting to some servers from Windows #368

Closed Evidlo closed 3 years ago

Evidlo commented 3 years ago

With the code snippet below, I get The specified network name is no longer available when I connect to a specific host at 10.11.99.1.

I've verified that the snippet works fine with other hosts. I've also connected to 10.11.99.1 with no problems using Paramiko and directly with SSH.

Any ideas how I might debug this?

#!/usr/bin/env python

import asyncssh
import asyncio
import sys

async def run_client():
    p = open('pass.txt', 'r').read()
    client = asyncssh.connect(
        '10.11.99.1',
        #'192.168.43.232',
        username='root',
        password=open('pass.txt', 'r').read().rstrip('\n'),
        encoding=None,
        compression_algs=None,
        known_hosts=None,
    )

    async with client as conn:
        result = await conn.run('echo "Hello!"', check=True)
        print(result.stdout, end='')
        sys.stdout.flush()

asyncio.ensure_future(run_client())
loop = asyncio.get_event_loop()
loop.run_forever()
C:\Users\Evan\temp>python foo.py
Task exception was never retrieved
future: <Task finished name='Task-1' coro=<run_client() done, defined at foo.py:8> exception=ConnectionResetError(22, 'The specified network name is no longer available', None, 64, None)>
Traceback (most recent call last):
  File "C:\Users\Evan\miniconda3\lib\asyncio\windows_events.py", line 453, in finish_recv
    return ov.getresult()
OSError: [WinError 64] The specified network name is no longer available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "foo.py", line 20, in run_client
    async with client as conn:
  File "C:\Users\Evan\miniconda3\lib\site-packages\asyncssh\misc.py", line 220, in __aenter__
    self._result = await self._coro
  File "C:\Users\Evan\miniconda3\lib\site-packages\asyncssh\connection.py", line 6685, in connect
    return await _connect(options, loop, flags, conn_factory,
  File "C:\Users\Evan\miniconda3\lib\site-packages\asyncssh\connection.py", line 228, in _connect
    await conn.wait_established()
  File "C:\Users\Evan\miniconda3\lib\site-packages\asyncssh\connection.py", line 2161, in wait_established
    await self._waiter
  File "C:\Users\Evan\miniconda3\lib\asyncio\proactor_events.py", line 280, in _loop_reading
    data = fut.result()
  File "C:\Users\Evan\miniconda3\lib\asyncio\windows_events.py", line 808, in _poll
    value = callback(transferred, key, ov)
  File "C:\Users\Evan\miniconda3\lib\asyncio\windows_events.py", line 457, in finish_recv
    raise ConnectionResetError(*exc.args)
ConnectionResetError: [WinError 64] The specified network name is no longer available
Traceback (most recent call last):
  File "foo.py", line 27, in <module>
    loop.run_forever()
  File "C:\Users\Evan\miniconda3\lib\asyncio\windows_events.py", line 316, in run_forever
    super().run_forever()
  File "C:\Users\Evan\miniconda3\lib\asyncio\base_events.py", line 570, in run_forever
    self._run_once()
  File "C:\Users\Evan\miniconda3\lib\asyncio\base_events.py", line 1823, in _run_once
    event_list = self._selector.select(timeout)
  File "C:\Users\Evan\miniconda3\lib\asyncio\windows_events.py", line 430, in select
    self._poll(timeout)
  File "C:\Users\Evan\miniconda3\lib\asyncio\windows_events.py", line 779, in _poll
    status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
ronf commented 3 years ago

If you do the reading of pass.txt outside of the connect() call, does the error still occur in the connect()? It looks like you're doing it before before and in the call to connect(), so it's hard to tell.

If you remove the call to reading from pass.txt entirely, does the problem still occur?

Also, when you run this, is your current working directory set to a network file share? Looking up the error message, almost all of the references talk about it being related to accessing network file shares.

Is there anything special about the 10.11.99.1 address vs. others which are working? Could there be some kind of access control which limits which programs are allowed to connect to that address, perhaps related to a firewall or other security software?

Evidlo commented 3 years ago

It looks like you're doing it before before and in the call to connect(), so it's hard to tell. If you remove the call to reading from pass.txt entirely, does the problem still occur?

Sorry, forgot to delete that line. I know it works because I've verified that it can connect to a Linux host at 192.168.43.232.

Also, when you run this, is your current working directory set to a network file share? Looking up the error message, almost all of the references talk about it being related to accessing network file shares.

No, I'm running it in a regular folder on the desktop.

Is there anything special about the 10.11.99.1 address vs. others which are working? Could there be some kind of access control which limits which programs are allowed to connect to that address, perhaps related to a firewall or other security software?

10.11.99.1 is a Linux tablet connected via USB. I connected to it from a Linux host in verbose mode, shown below:

'ssh -vvv' output for 10.11.99.1
[evan@blackbox ~] ssh -vvv -o PreferredAuthentications=password -o PubkeyAuthentication=no root@10.11.99.1
OpenSSH_8.3p1 Debian-1, OpenSSL 1.1.1i  8 Dec 2020
debug1: Reading configuration data /home/evan/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 10.11.99.1 is address
debug2: ssh_connect_direct
debug1: Connecting to 10.11.99.1 [10.11.99.1] port 22.
debug1: Connection established.
debug1: identity file /home/evan/.ssh/id_rsa type 0
debug1: identity file /home/evan/.ssh/id_rsa-cert type -1
debug1: identity file /home/evan/.ssh/id_dsa type -1
debug1: identity file /home/evan/.ssh/id_dsa-cert type -1
debug1: identity file /home/evan/.ssh/id_ecdsa type -1
debug1: identity file /home/evan/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/evan/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/evan/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/evan/.ssh/id_ed25519 type -1
debug1: identity file /home/evan/.ssh/id_ed25519-cert type -1
debug1: identity file /home/evan/.ssh/id_ed25519_sk type -1
debug1: identity file /home/evan/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/evan/.ssh/id_xmss type -1
debug1: identity file /home/evan/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.3p1 Debian-1
debug1: Remote protocol version 2.0, remote software version dropbear_2017.75
debug1: no match: dropbear_2017.75
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 10.11.99.1:22 as 'root'
debug3: hostkeys_foreach: reading file "/home/evan/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /home/evan/.ssh/known_hosts:195
debug3: load_hostkeys: loaded 1 keys from 10.11.99.1
debug3: order_hostkeyalgs: prefer hostkeyalgs: rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-c
debug2: host key algorithms: rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,kexguess2@matt.ucc.asn.au
debug2: host key algorithms: ssh-rsa
debug2: ciphers ctos: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
debug2: ciphers stoc: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
debug2: MACs ctos: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
debug2: MACs stoc: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
debug2: compression ctos: zlib@openssh.com,none
debug2: compression stoc: zlib@openssh.com,none
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ssh-rsa SHA256:M/fZOkfFkFSi7RvS27DwwvTXgRBFfH2LTYc10ttvYvQ
debug3: hostkeys_foreach: reading file "/home/evan/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /home/evan/.ssh/known_hosts:195
debug3: load_hostkeys: loaded 1 keys from 10.11.99.1
debug1: Host '10.11.99.1' is known and matches the RSA host key.
debug1: Found key in /home/evan/.ssh/known_hosts:195
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 4294967296 blocks
debug1: Will attempt key: /home/evan/.ssh/id_rsa RSA SHA256:wzSYeC5EC1e+TINgMO1b3UjBXxT0zYWy22ig1/LNORE
debug1: Will attempt key: /home/evan/.ssh/id_dsa 
debug1: Will attempt key: /home/evan/.ssh/id_ecdsa 
debug1: Will attempt key: /home/evan/.ssh/id_ecdsa_sk 
debug1: Will attempt key: /home/evan/.ssh/id_ed25519 
debug1: Will attempt key: /home/evan/.ssh/id_ed25519_sk 
debug1: Will attempt key: /home/evan/.ssh/id_xmss 
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred password
debug3: authmethod_lookup password
debug3: remaining preferred: 
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
root@10.11.99.1's password: 
debug3: send packet: type 50
debug2: we sent a password packet, wait for reply
debug3: receive packet: type 52
debug1: Authentication succeeded (password).
Authenticated to 10.11.99.1 ([10.11.99.1]:22).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug3: send packet: type 90
debug1: Entering interactive session.
debug1: pledge: network
debug3: receive packet: type 91
debug2: channel_input_open_confirmation: channel 0: callback start
debug2: fd 3 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x10
debug2: client_session2_setup: id 0
debug2: channel 0: request pty-req confirm 1
debug3: send packet: type 98
debug1: Sending environment.
debug3: Ignored env SHELL
debug3: Ignored env WINDOWID
debug3: Ignored env COLORTERM
debug3: Ignored env HISTCONTROL
debug3: Ignored env XTERM_VERSION
debug3: Ignored env I3SOCK
debug3: Ignored env HOMEBREW_PREFIX
debug3: Ignored env XTERM_SHELL
debug3: Ignored env EDITOR
debug3: Ignored env GOBIN
debug3: Ignored env XDG_SEAT
debug3: Ignored env PWD
debug3: Ignored env LOGNAME
debug3: Ignored env XDG_SESSION_TYPE
debug3: Ignored env MANPATH
debug3: Ignored env XAUTHORITY
debug3: Ignored env DESKTOP_STARTUP_ID
debug3: Ignored env WINDOWPATH
debug3: Ignored env HOME
debug3: Ignored env AUTOJUMP_ERROR_PATH
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env VIRTUAL_ENV
debug3: Ignored env VIRTUAL_ENV_DISABLE_PROMPT
debug3: Ignored env PROMPT_COMMAND
debug3: Ignored env INVOCATION_ID
debug3: Ignored env XTERM_LOCALE
debug3: Ignored env PYTHON_HOST_PROG
debug3: Ignored env INFOPATH
debug3: Ignored env PYTHON3_HOST_PROG
debug3: Ignored env XDG_SESSION_CLASS
debug3: Ignored env TERM
debug3: Ignored env USER
debug3: Ignored env COLORFGBG
debug3: Ignored env HOMEBREW_CELLAR
debug3: Ignored env AUTOJUMP_SOURCED
debug3: Ignored env DISPLAY
debug3: Ignored env SHLVL
debug3: Ignored env HOMEBREW_REPOSITORY
debug3: Ignored env XDG_VTNR
debug3: Ignored env XDG_SESSION_ID
debug3: Ignored env XDG_RUNTIME_DIR
debug3: Ignored env NODE_PATH
debug3: Ignored env JOURNAL_STREAM
debug3: Ignored env XDG_DATA_DIRS
debug3: Ignored env HUSHLOGIN
debug3: Ignored env ALTERNATE_EDITOR
debug3: Ignored env PATH
debug3: Ignored env MAIL
debug3: Ignored env GOPATH
debug3: Ignored env _
debug2: channel 0: request shell confirm 1
debug3: send packet: type 98
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 24576 rmax 32759
debug3: receive packet: type 99
debug2: channel_input_status_confirm: type 99 id 0
debug2: PTY allocation request accepted on channel 0
debug3: receive packet: type 99
debug2: channel_input_status_confirm: type 99 id 0
debug2: shell request accepted on channel 0
reMarkable
╺━┓┏━╸┏━┓┏━┓   ┏━╸┏━┓┏━┓╻ ╻╻╺┳╸┏━┓┏━┓
┏━┛┣╸ ┣┳┛┃ ┃   ┃╺┓┣┳┛┣━┫┃┏┛┃ ┃ ┣━┫┗━┓
┗━╸┗━╸╹┗╸┗━┛   ┗━┛╹┗╸╹ ╹┗┛ ╹ ╹ ╹ ╹┗━┛
ronf commented 3 years ago

Unfortunately, I don't have a lot of experience with Windows networking and the error appears to be coming from an attempt to open a TCP connection to the SSH port on the tablet, before anything SSH-related is really happening.

Since you have a Linux client host, can you try connecting with AsyncSSH from Linux instead of Windows and see if that works? That'd be closer to the OpenSSH example you are showing here which worked.

Another thing you might try is to write a small Python program which does a connect() to the tablet on port 22 without AsyncSSH involved and see if you get the same error.

Evidlo commented 3 years ago

Also I did a little bit of digging around where the error is being raised in asyncio's windows_events.py. I added some print debugging to the finish_recv() function:

def finish_recv(trans, key, ov):
    print('trans:', trans)
    print('key:', key)
    print('ov:', ov)
    print('ov.address:', ov.address)
    print('conn:', conn)
    print('----')
    print(dir(ov))
    print('----')
    ...

And the result:


C:\Users\Rahul Tiwari\test>python mouse.py
trans: 642
key: 0
ov: <_overlapped.Overlapped object at 0x000001F2F09DDD20>
ov.address: 2142930591024
conn: socket.socket fd=708, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.11.99.2', 64021), raddr=('10.11.99.1', 22)
----
['AcceptEx', 'ConnectEx', 'ConnectNamedPipe', 'DisconnectEx', 'ReadFile', 'ReadFileInto', 'TransmitFile', 'WSARecv', 'WSARecvFrom', 'WSARecvInto', 'WSASend', 'WSASendTo', 'WriteFile', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'address', 'cancel', 'error', 'event', 'getresult', 'pending']
----
trans: 0
key: 0
ov: <_overlapped.Overlapped object at 0x000001F2F09C5390>
ov.address: 2142930490272
conn: socket.socket fd=708, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.11.99.2', 64021), raddr=('10.11.99.1', 22)
----
['AcceptEx', 'ConnectEx', 'ConnectNamedPipe', 'DisconnectEx', 'ReadFile', 'ReadFileInto', 'TransmitFile', 'WSARecv', 'WSARecvFrom', 'WSARecvInto', 'WSASend', 'WSASendTo', 'WriteFile', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'address', 'cancel', 'error', 'event', 'getresult', 'pending']
----
trans: 642
key: 0
ov: <_overlapped.Overlapped object at 0x000001F2F09DDDB0>
ov.address: 2142930591168
conn: socket.socket fd=356, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.11.99.2', 64019), raddr=('10.11.99.1', 22)
----
['AcceptEx', 'ConnectEx', 'ConnectNamedPipe', 'DisconnectEx', 'ReadFile', 'ReadFileInto', 'TransmitFile', 'WSARecv', 'WSARecvFrom', 'WSARecvInto', 'WSASend', 'WSASendTo', 'WriteFile', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'address', 'cancel', 'error', 'event', 'getresult', 'pending']
----
trans: 642
key: 0
ov: <_overlapped.Overlapped object at 0x000001F2F09EF030>
ov.address: 2142930661440
conn: socket.socket fd=704, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.11.99.2', 64020), raddr=('10.11.99.1', 22)
----
['AcceptEx', 'ConnectEx', 'ConnectNamedPipe', 'DisconnectEx', 'ReadFile', 'ReadFileInto', 'TransmitFile', 'WSARecv', 'WSARecvFrom', 'WSARecvInto', 'WSASend', 'WSASendTo', 'WriteFile', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'address', 'cancel', 'error', 'event', 'getresult', 'pending']
----
Task exception was never retrieved
future:  exception=ConnectionResetError(22, 'The specified network name is no longer available', None, 64, None)>
Traceback (most recent call last):
  File "C:\Users\Rahul Tiwari\miniconda3\lib\asyncio\windows_events.py", line 461, in finish_recv
    return ov.getresult()
OSError: [WinError 64] The specified network name is no longer available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Rahul Tiwari\test\mouse_async.py", line 14, in stream_input
    async with client as conn:
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\misc.py", line 220, in __aenter__
    self._result = await self._coro
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 6685, in connect
    return await _connect(options, loop, flags, conn_factory,
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 228, in _connect
    await conn.wait_established()
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 2161, in wait_established
    await self._waiter
  File "C:\Users\Rahul Tiwari\miniconda3\lib\asyncio\proactor_events.py", line 280, in _loop_reading
    data = fut.result()
  File "C:\Users\Rahul Tiwari\miniconda3\lib\asyncio\windows_events.py", line 816, in _poll
    value = callback(transferred, key, ov)
  File "C:\Users\Rahul Tiwari\miniconda3\lib\asyncio\windows_events.py", line 465, in finish_recv
    raise ConnectionResetError(*exc.args)
ConnectionResetError: [WinError 64] The specified network name is no longer available

...
ronf commented 3 years ago

Looking at this more closely, it may not be an error on the connect() itself. It looks like maybe the error is recv(), after the connection is established, and the server is sending back a TCP connection reset. In some cases, that can happen if the server doesn't like what the client (AsyncSSH) is sending. Perhaps there are no matching algorithms, for instance.

Can you enable AsyncSSH debug logging to see what data is sent and received before the connection is reset? You'd need to add lines like:

    import logging
    logging.basicConfig(level='DEBUG')
    asyncssh.set_debug_level(2)
Evidlo commented 3 years ago

Since you have a Linux client host, can you try connecting with AsyncSSH from Linux instead of Windows and see if that works? That'd be closer to the OpenSSH example you are showing here which worked.

Yes, the script already works on the Linux host, which is where I was doing development.

Another thing you might try is to write a small Python program which does a connect() to the tablet on port 22 without AsyncSSH involved and see if you get the same error.

Here's a simple script to connect to the server:

#!/usr/bin/env python

import socket

TCP_IP = '10.11.99.1'
TCP_PORT = 22
BUFFER_SIZE = 1024
MESSAGE = b"Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

print("received data:", data)

Result on Windows

C:\Users\Rahul Tiwari\temp>python socktest.py
received data: b'SSH-2.0-dropbear_2017.75\r\n\x00\x00\x02d\n\x14\xe0\\\x1e\xe2\x0eMs\xd1\xc0\xf7\x1f\xd5\x15\x1f[T\x00\x00\x00\xa6curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,kexguess2@matt.ucc.asn.au\x00\x00\x00\x07ssh-rsa\x00\x00\x00gaes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc\x00\x00\x00gaes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc\x00\x00\x00;hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5\x00\x00\x00;hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5\x00\x00\x00\x15zlib@openssh.com,none\x00\x00\x00\x15zlib@openssh.com,none\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00(\x12\x1f>\x9a\xb3\xb3\xeb\x87u'
Evidlo commented 3 years ago

Can you enable AsyncSSH debug logging to see what data is sent and received before the connection is reset?

C:\Users\Rahul Tiwari\temp>python mouse.py
DEBUG:remouse:Chose monitor: Monitor(x=0, y=0, width=1366, height=768, width_mm=345, height_mm=194, name='\\\\.\\DISPLAY1')
DEBUG:asyncio:Using proactor: IocpProactor
INFO:asyncssh:Opening SSH connection to 10.11.99.1, port 22
INFO:asyncssh:Opening SSH connection to 10.11.99.1, port 22
INFO:asyncssh:Opening SSH connection to 10.11.99.1, port 22
INFO:asyncssh:[conn=0] Connection to 10.11.99.1, port 22 succeeded
INFO:asyncssh:[conn=0]   Local address: 10.11.99.2, port 55889
INFO:asyncssh:[conn=1] Connection to 10.11.99.1, port 22 succeeded
INFO:asyncssh:[conn=1]   Local address: 10.11.99.2, port 55890
INFO:asyncssh:[conn=2] Connection to 10.11.99.1, port 22 succeeded
INFO:asyncssh:[conn=2]   Local address: 10.11.99.2, port 55891
DEBUG:asyncssh:[conn=2] Requesting key exchange
DEBUG:asyncssh:[conn=2]   Key exchange algs: curve25519-sha256,curve25519-sha256@libssh.org,curve448-sha512,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,ecdh-sha2-1.3.132.0.10,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group15-sha512,diffie-hellman-group16-sha512,diffie-hellman-group17-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256@ssh.com,diffie-hellman-group14-sha1,rsa2048-sha256
DEBUG:asyncssh:[conn=2]   Host key algs: sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-ed448-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-1.3.132.0.10-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ssh-ed448,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,ecdsa-sha2-1.3.132.0.10,rsa-sha2-256,rsa-sha2-512,ssh-rsa-sha224@ssh.com,ssh-rsa-sha256@ssh.com,ssh-rsa-sha384@ssh.com,ssh-rsa-sha512@ssh.com,ssh-rsa
DEBUG:asyncssh:[conn=2]   Encryption algs: chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
DEBUG:asyncssh:[conn=2]   MAC algs: hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha256-2@ssh.com,hmac-sha224@ssh.com,hmac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
DEBUG:asyncssh:[conn=2]   Compression algs: none
DEBUG:asyncssh:[conn=2] Received key exchange request
DEBUG:asyncssh:[conn=2]   Key exchange algs: curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,kexguess2@matt.ucc.asn.au
DEBUG:asyncssh:[conn=2]   Host key algs: ssh-rsa
DEBUG:asyncssh:[conn=2]   Client to server:
DEBUG:asyncssh:[conn=2]     Encryption algs: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
DEBUG:asyncssh:[conn=2]     MAC algs: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
DEBUG:asyncssh:[conn=2]     Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=2]   Server to client:
DEBUG:asyncssh:[conn=2]     Encryption algs: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
DEBUG:asyncssh:[conn=2]     MAC algs: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
DEBUG:asyncssh:[conn=2]     Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=2] Beginning key exchange
DEBUG:asyncssh:[conn=2]   Key exchange alg: curve25519-sha256@libssh.org
DEBUG:asyncssh:[conn=1] Requesting key exchange
DEBUG:asyncssh:[conn=1]   Key exchange algs: curve25519-sha256,curve25519-sha256@libssh.org,curve448-sha512,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,ecdh-sha2-1.3.132.0.10,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group15-sha512,diffie-hellman-group16-sha512,diffie-hellman-group17-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256@ssh.com,diffie-hellman-group14-sha1,rsa2048-sha256
DEBUG:asyncssh:[conn=1]   Host key algs: sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-ed448-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-1.3.132.0.10-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ssh-ed448,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,ecdsa-sha2-1.3.132.0.10,rsa-sha2-256,rsa-sha2-512,ssh-rsa-sha224@ssh.com,ssh-rsa-sha256@ssh.com,ssh-rsa-sha384@ssh.com,ssh-rsa-sha512@ssh.com,ssh-rsa
DEBUG:asyncssh:[conn=1]   Encryption algs: chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
DEBUG:asyncssh:[conn=1]   MAC algs: hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha256-2@ssh.com,hmac-sha224@ssh.com,hmac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
DEBUG:asyncssh:[conn=1]   Compression algs: none
DEBUG:asyncssh:[conn=1] Received key exchange request
DEBUG:asyncssh:[conn=1]   Key exchange algs: curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,kexguess2@matt.ucc.asn.au
DEBUG:asyncssh:[conn=1]   Host key algs: ssh-rsa
DEBUG:asyncssh:[conn=1]   Client to server:
DEBUG:asyncssh:[conn=1]     Encryption algs: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
DEBUG:asyncssh:[conn=1]     MAC algs: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
DEBUG:asyncssh:[conn=1]     Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=1]   Server to client:
DEBUG:asyncssh:[conn=1]     Encryption algs: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
DEBUG:asyncssh:[conn=1]     MAC algs: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
DEBUG:asyncssh:[conn=1]     Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=1] Beginning key exchange
DEBUG:asyncssh:[conn=1]   Key exchange alg: curve25519-sha256@libssh.org
DEBUG:asyncssh:[conn=0] Requesting key exchange
DEBUG:asyncssh:[conn=0]   Key exchange algs: curve25519-sha256,curve25519-sha256@libssh.org,curve448-sha512,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,ecdh-sha2-1.3.132.0.10,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group15-sha512,diffie-hellman-group16-sha512,diffie-hellman-group17-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256@ssh.com,diffie-hellman-group14-sha1,rsa2048-sha256
DEBUG:asyncssh:[conn=0]   Host key algs: sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-ed448-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-1.3.132.0.10-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ssh-ed448,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,ecdsa-sha2-1.3.132.0.10,rsa-sha2-256,rsa-sha2-512,ssh-rsa-sha224@ssh.com,ssh-rsa-sha256@ssh.com,ssh-rsa-sha384@ssh.com,ssh-rsa-sha512@ssh.com,ssh-rsa
DEBUG:asyncssh:[conn=0]   Encryption algs: chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
DEBUG:asyncssh:[conn=0]   MAC algs: hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha256-2@ssh.com,hmac-sha224@ssh.com,hmac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
DEBUG:asyncssh:[conn=0]   Compression algs: none
DEBUG:asyncssh:[conn=0] Received key exchange request
DEBUG:asyncssh:[conn=0]   Key exchange algs: curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,kexguess2@matt.ucc.asn.au
DEBUG:asyncssh:[conn=0]   Host key algs: ssh-rsa
DEBUG:asyncssh:[conn=0]   Client to server:
DEBUG:asyncssh:[conn=0]     Encryption algs: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
DEBUG:asyncssh:[conn=0]     MAC algs: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
DEBUG:asyncssh:[conn=0]     Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=0]   Server to client:
DEBUG:asyncssh:[conn=0]     Encryption algs: aes128-ctr,aes256-ctr,aes128-cbc,aes256-cbc,twofish256-cbc,twofish-cbc,twofish128-cbc,3des-ctr,3des-cbc
DEBUG:asyncssh:[conn=0]     MAC algs: hmac-sha1-96,hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-md5
DEBUG:asyncssh:[conn=0]     Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=0] Beginning key exchange
DEBUG:asyncssh:[conn=0]   Key exchange alg: curve25519-sha256@libssh.org
INFO:asyncssh:[conn=2] Connection lost
INFO:asyncssh:[conn=2] Aborting connection
ERROR:asyncio:Task exception was never retrieved
future:  exception=ConnectionLost('Connection lost')>
Traceback (most recent call last):
  File "C:\Users\Rahul Tiwari\temp\mouse_async.py", line 14, in stream_input
    async with client as conn:
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\misc.py", line 220, in __aenter__
    self._result = await self._coro
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 6685, in connect
    return await _connect(options, loop, flags, conn_factory,
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 228, in _connect
    await conn.wait_established()
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 2161, in wait_established
    await self._waiter
asyncssh.misc.ConnectionLost: Connection lost
INFO:asyncssh:[conn=1] Connection lost
INFO:asyncssh:[conn=0] Connection lost
INFO:asyncssh:[conn=1] Aborting connection
ERROR:asyncio:Task exception was never retrieved
future:  exception=ConnectionLost('Connection lost')>
Traceback (most recent call last):
  File "C:\Users\Rahul Tiwari\temp\mouse_async.py", line 14, in stream_input
    async with client as conn:
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\misc.py", line 220, in __aenter__
    self._result = await self._coro
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 6685, in connect
    return await _connect(options, loop, flags, conn_factory,
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 228, in _connect
    await conn.wait_established()
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 2161, in wait_established
    await self._waiter
asyncssh.misc.ConnectionLost: Connection lost
INFO:asyncssh:[conn=0] Aborting connection
ERROR:asyncio:Task exception was never retrieved
future:  exception=ConnectionLost('Connection lost')>
Traceback (most recent call last):
  File "C:\Users\Rahul Tiwari\temp\mouse_async.py", line 14, in stream_input
    async with client as conn:
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\misc.py", line 220, in __aenter__
    self._result = await self._coro
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 6685, in connect
    return await _connect(options, loop, flags, conn_factory,
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 228, in _connect
    await conn.wait_established()
  File "C:\Users\Rahul Tiwari\miniconda3\lib\site-packages\asyncssh\connection.py", line 2161, in wait_established
    await self._waiter
asyncssh.misc.ConnectionLost: Connection lost
ronf commented 3 years ago

I see the server is based on dropbear. There's a known bug in that which AsyncSSH can trigger due to its large number of supported algorithms. Dropbear only looks at the first 20 algorithms in the list, and so it doesn't find a match before hitting the limit on what it will search. See #263 and #276 for more info, including a possible workaround.

ronf commented 3 years ago

Looks like dropbear did eventually fix this on their end in:

https://github.com/mkj/dropbear/commit/1a208c460b412113704c4d5f98585017ee0a6ccf

However, the tablet you have might be running an older version of dropbear without this fix.

Evidlo commented 3 years ago

Thanks, that was it.

For posterity, the tablet was running dropbear v2017.75

Evidlo commented 3 years ago

I know this isn't asyncssh's fault, but maybe an acceptable solution would be to list some of the most commonly used algorithms first when negotiating a key exchange method?

ronf commented 3 years ago

AsyncSSH (and other SSH clients/servers) typically list the strongest algorithms first, and by that measure ssh-rsa is near the end of the list (only ahead of ssh-dss, which is not even enabled by default any more). Unfortunately, older versions of dropbear only support ssh-rsa and ssh-dss if I remember right. Newer versions of dropbear have since added the stronger versions of RSA with SHA-2, as well as ECDSA. So, you wouldn't run into the issue with a version supporting those even if it didn't have the fix to support a larger number of algorithms. However, if the dropbear version is old enough, this can happen.