I need to ssh into a docker container (ubuntu/bash) and access stdout/stderr as it is emmitted (rather than wait for the entire command to complete then pick it up).
I found asyncssh
which appears to be asyncio-based. GPT4 suggests:
import asyncio
import asyncssh
async def run_command():
container = client.containers.run(
'my-ssh-image',
name='my-ssh-container',
detach=True,
remove=False,
auto_remove=False,
ports={'22/tcp': ('127.0.0.1', 2222)},
publish_all_ports=True,
)
async with asyncssh.connect('127.0.0.1', 2222, username='root', password='password') as conn:
async with conn.create_process('echo "foo"') as process:
while not process.stdout.at_eof():
stdout_line = await process.stdout.readline()
if stdout_line:
print(' stdout:')
print(stdout_line.strip())
stderr_line = await process.stderr.readline()
if stderr_line:
print('stderr:')
print(stderr_line.strip())
container.stop()
container.remove()
asyncio.run(run_command())
However I can't see any way to do this with trio, other than using asyncio_trio
to wrap the aio-flavoured calls. And I prefer to use trio over asyncio.
trio
seems to be lacking an ssh lib.
How to achieve an async ssh session with trio?