Question about usage with su/interactive session debugging #714
-
Hi! Thanks a lot for the library. import asyncio
import asyncssh
import sys
import time
async def run_client() -> None:
async with asyncssh.connect('192.168.50.11', username='lowpriv', password='trustno1') as conn:
try:
result = await conn.run('echo "Hello!"', check=True)
except asyncssh.ProcessError as exc:
print(exc.stderr, end='')
print(f'Process exited with status {exc.exit_status}',
file=sys.stderr)
else:
print(result.stdout, end='')
try:
async with conn.create_process('su root') as process:
print("Started process")
time.sleep(1)
# Send the password to `su` via stdin
process.stdin.write('aim8Du7h' + '\n') # Send password
# Read the output of the `su` command
result = await process.stdout.read()
print(f"su root result: {result.decode()}") # Decode and print output
print("Ended")
except asyncssh.ProcessError as exc:
print(f"Error running su command: {exc.stderr}", file=sys.stderr)
# If you're in an environment where an event loop is already running (like Jupyter or IPython),
# you can simply use 'await' instead of 'run_until_complete'
try:
# In Jupyter, use 'await' directly to run the coroutine
asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc)) but my code hangs in started process. My question is more what's a good way to debug this since I'm currently a bit stuck. And again thanks for the library and def feel free to close if there's a better place for questions like this! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think your problem here might be the use of In terms of debugging, I see you are already adding in print statements. You can add more to narrow down exactly which call it is blocking on, and then start to dig into why. You can also turn on AsyncSSH debugging to see the actual messages being sent and received on the network. Enabling that would look something like: import logging
logging.basicConfig(level='DEBUG')
asyncssh.set_debug_level(2) Debug level can be 1 (minimal logging) to 3 (verbose logging with raw packet dumps). In most cases, level 2 is enough. |
Beta Was this translation helpful? Give feedback.
I think your problem here might be the use of
read()
with no argument. That will block waiting for the remote system to return EOF, which won't happen on an interactive shell if you haven't closed stdin. If you pass in a maximum read size, it will return as soon as any data is sent (up to the max bytes you requested).In terms of debugging, I see you are already adding in print statements. You can add more to narrow down exactly which call it is blocking on, and then start to dig into why. You can also turn on AsyncSSH debugging to see the actual messages being sent and received on the network. Enabling that would look something like: