Skip to content

Cannot get shell welcome message and prompt after creating session #597

@netes040

Description

@netes040

I'm trying to establish an interactive SSH shell using the following code:

import asyncio, asyncssh, sys

async def run_client():
    async with asyncssh.connect(
        HOST, port=PORT, username=USER, password=PASSWORD,
        known_hosts=None) as conn:
        
        stdin, stdout, stderr = await conn.open_session()
        # Try to get the welcoming message and shell prompt
        welcome = await stdout.read(1024)
        print(welcome)

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

However, when running this program, the stdout.read(1024) never returns. I can obtain the desired behavior using paramiko with the following code:

from paramiko import SSHClient, AutoAddPolicy

with SSHClient() as client:
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(hostname, port, username, password)
    
    with client.invoke_shell() as chan:
        chan.settimeout(2)
        ## wait until some data is available
        while not channel.recv_ready():
            time.sleep(0.3)
        ## get the data
        welcome = b""
        while channel.recv_ready():
            welcome += channel.recv(2**13)
            time.sleep(delay)
        print(welcome.decode("utf-8"))

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