Skip to content

Strange behavior of connect function when implemented inside a class #397

@LLIETAER

Description

@LLIETAER

Hi, here a little test I made to implement a connection class.

import sys
import asyncio
import logging

from typing import List

import socket
import asyncssh

class Ovc:
    def __init__(self, ip: str):
        self.ip = ip
        self.status: str = "new"
        self.loop = asyncio.get_event_loop()
        self.one_connect()

    def one_connect(self):
        self.loop.create_task(self._one_connect())

    async def _one_connect(self: object) -> None:
        # try
        print("Inside class instance", self.ip)
        self.conn = await asyncssh.connect(self.ip, username="user", password="password",known_hosts=None)
        print('Inside class instance connected')

    def port_open(self, port) -> bool:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        test = sock.connect_ex((self.ip, port))
        result = False
        if test == 0:
            result = True
        sock.close()
        return result

    def __repr__(self):
        return self.ip

    def __str__(self):
        return self.ip


async def main():
    try:
        print('try connect')
        connexion = await \
            asyncssh.connect('172.21.0.12',
                             username='user',
                             password='password',
                             known_hosts=None)
        print('connected')
    except (OSError, asyncssh.Error) as exc:
        print("main exception transmited")
        raise exc
        # sys.exit('SSH connection failed: ' + str(exc))
    else:
        print("connection close")
        connexion.close()

    try:
        print('try connect')
        connexion: asyncssh.SSHClientConnection = await \
            asyncssh.connect('172.21.0.12',
                             username='user',
                             password='password',
                             known_hosts=None)
        print('connected')
    except (OSError, asyncssh.Error) as exc:
        print("main exception transmited")
        raise exc
        # sys.exit('SSH connection failed: ' + str(exc))
    else:
        print("connection close")
        connexion.close()

    print('Federation')
    one = Ovc('172.21.0.12')


if __name__ == "__main__":
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    asyncssh.set_debug_level(2)
    asyncio.get_event_loop().run_until_complete(main())`

Basically, I implemented a Ovc class to handle a SSH client connection.
In my main() function, I call twice asyncssh.connect on the same server (those 2 calls working as expected) then I create an Ovc instance that call also asyncssh.connect function. At this point the program stop without any errors.
You will find just below the debug 2 run output.

DEBUG:asyncio:Using proactor: IocpProactor
try connect
INFO:asyncssh:Opening SSH connection to 172.21.0.12, port 22
INFO:asyncssh:[conn=0] Connected to SSH server at 172.21.0.12, port 22
INFO:asyncssh:[conn=0]   Local address: 10.232.176.144, port 50513
INFO:asyncssh:[conn=0]   Peer address: 172.21.0.12, port 22
DEBUG:asyncssh:[conn=0] Sending version SSH-2.0-AsyncSSH_2.7.0
DEBUG:asyncssh:[conn=0] Received version SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
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@s
sh.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-nistp52
1-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.c
om,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-sh
a256@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,h
mac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
DEBUG:asyncssh:[conn=0]   Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=0] Received key exchange request
DEBUG:asyncssh:[conn=0]   Key exchange algs: diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,curve25519-sha256@libssh.org
DEBUG:asyncssh:[conn=0]   Host key algs: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
DEBUG:asyncssh:[conn=0]   Client to server:
DEBUG:asyncssh:[conn=0]     Encryption algs: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=0]     MAC algs: hmac-sha2-256,hmac-sha2-512,umac-128@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com
DEBUG:asyncssh:[conn=0]     Compression algs: none,zlib@openssh.com
DEBUG:asyncssh:[conn=0]   Server to client:
DEBUG:asyncssh:[conn=0]     Encryption algs: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=0]     MAC algs: hmac-sha2-256,hmac-sha2-512,umac-128@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com
DEBUG:asyncssh:[conn=0]     Compression algs: none,zlib@openssh.com
DEBUG:asyncssh:[conn=0] Beginning key exchange
DEBUG:asyncssh:[conn=0]   Key exchange alg: curve25519-sha256@libssh.org
DEBUG:asyncssh:[conn=0]   Client to server:
DEBUG:asyncssh:[conn=0]     Encryption alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=0]     MAC alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=0]     Compression alg: zlib@openssh.com
DEBUG:asyncssh:[conn=0]   Server to client:
DEBUG:asyncssh:[conn=0]     Encryption alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=0]     MAC alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=0]     Compression alg: zlib@openssh.com
DEBUG:asyncssh:[conn=0] Requesting service ssh-userauth
DEBUG:asyncssh:[conn=0] Completed key exchange
DEBUG:asyncssh:[conn=0] Received extension info
DEBUG:asyncssh:[conn=0]   server-sig-algs: ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
DEBUG:asyncssh:[conn=0] Request for service ssh-userauth accepted
INFO:asyncssh:[conn=0] Beginning auth for user iusr_simplivitymonitor@vsphere.local
DEBUG:asyncssh:[conn=0] Received authentication banner
DEBUG:asyncssh:[conn=0] Remaining auth methods: publickey,password
DEBUG:asyncssh:[conn=0] Preferred auth methods: gssapi-keyex,gssapi-with-mic,hostbased,publickey,keyboard-interactive,password
DEBUG:asyncssh:[conn=0] Trying public key auth with rsa-sha2-256 key
DEBUG:asyncssh:[conn=0] Remaining auth methods: publickey,password
DEBUG:asyncssh:[conn=0] Preferred auth methods: gssapi-keyex,gssapi-with-mic,hostbased,publickey,keyboard-interactive,password
DEBUG:asyncssh:[conn=0] Trying password auth
INFO:asyncssh:[conn=0] Auth for user iusr_simplivitymonitor@vsphere.local succeeded
connected
connection close
INFO:asyncssh:[conn=0] Closing connection
INFO:asyncssh:[conn=0] Sending disconnect: Disconnected by application (11)
try connect
INFO:asyncssh:Opening SSH connection to 172.21.0.12, port 22
INFO:asyncssh:[conn=0] Connection closed
INFO:asyncssh:[conn=1] Connected to SSH server at 172.21.0.12, port 22
INFO:asyncssh:[conn=1]   Local address: 10.232.176.144, port 50514
INFO:asyncssh:[conn=1]   Peer address: 172.21.0.12, port 22
DEBUG:asyncssh:[conn=1] Sending version SSH-2.0-AsyncSSH_2.7.0
DEBUG:asyncssh:[conn=1] Received version SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
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@s
sh.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-nistp52
1-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.c
om,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-sh
a256@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,h
mac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
DEBUG:asyncssh:[conn=1]   Compression algs: zlib@openssh.com,none
DEBUG:asyncssh:[conn=1] Received key exchange request
DEBUG:asyncssh:[conn=1]   Key exchange algs: diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,curve25519-sha256@libssh.org
DEBUG:asyncssh:[conn=1]   Host key algs: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
DEBUG:asyncssh:[conn=1]   Client to server:
DEBUG:asyncssh:[conn=1]     Encryption algs: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=1]     MAC algs: hmac-sha2-256,hmac-sha2-512,umac-128@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com
DEBUG:asyncssh:[conn=1]     Compression algs: none,zlib@openssh.com
DEBUG:asyncssh:[conn=1]   Server to client:
DEBUG:asyncssh:[conn=1]     Encryption algs: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=1]     MAC algs: hmac-sha2-256,hmac-sha2-512,umac-128@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com
DEBUG:asyncssh:[conn=1]     Compression algs: none,zlib@openssh.com
DEBUG:asyncssh:[conn=1] Beginning key exchange
DEBUG:asyncssh:[conn=1]   Key exchange alg: curve25519-sha256@libssh.org
DEBUG:asyncssh:[conn=1]   Client to server:
DEBUG:asyncssh:[conn=1]     Encryption alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=1]     MAC alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=1]     Compression alg: zlib@openssh.com
DEBUG:asyncssh:[conn=1]   Server to client:
DEBUG:asyncssh:[conn=1]     Encryption alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=1]     MAC alg: chacha20-poly1305@openssh.com
DEBUG:asyncssh:[conn=1]     Compression alg: zlib@openssh.com
DEBUG:asyncssh:[conn=1] Requesting service ssh-userauth
DEBUG:asyncssh:[conn=1] Completed key exchange
DEBUG:asyncssh:[conn=1] Received extension info
DEBUG:asyncssh:[conn=1]   server-sig-algs: ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
DEBUG:asyncssh:[conn=1] Request for service ssh-userauth accepted
INFO:asyncssh:[conn=1] Beginning auth for user iusr_simplivitymonitor@vsphere.local
DEBUG:asyncssh:[conn=1] Received authentication banner
DEBUG:asyncssh:[conn=1] Remaining auth methods: publickey,password
DEBUG:asyncssh:[conn=1] Preferred auth methods: gssapi-keyex,gssapi-with-mic,hostbased,publickey,keyboard-interactive,password
DEBUG:asyncssh:[conn=1] Trying public key auth with rsa-sha2-256 key
DEBUG:asyncssh:[conn=1] Remaining auth methods: publickey,password
DEBUG:asyncssh:[conn=1] Preferred auth methods: gssapi-keyex,gssapi-with-mic,hostbased,publickey,keyboard-interactive,password
DEBUG:asyncssh:[conn=1] Trying password auth
INFO:asyncssh:[conn=1] Auth for user iusr_simplivitymonitor@vsphere.local succeeded
connected
connection close
INFO:asyncssh:[conn=1] Closing connection
INFO:asyncssh:[conn=1] Sending disconnect: Disconnected by application (11)
Federation
INFO:asyncssh:[conn=1] Connection closed
Inside class instance 172.21.0.12
INFO:asyncssh:Opening SSH connection to 172.21.0.12, port 22

Process finished with exit code 0

It looks very strange to me, have you any ideas to move forward ? thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions