question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execute commands with paged/large outputs using asyncssh

See original GitHub issue

Hello Ronf, Greetings!

I have to access various vendor routers, execute some commands and process the output. Workflow is something like this - ssh to multiple devices in parallel in each device, run commands sequentially process the output of each command after execution disconnect the ssh connection

Problem is, when the output is large and paginated, I am unable to get the full output.

I went through https://github.com/ronf/asyncssh/issues/241. But couldn’t quiet figure it out.

I tried the suggestions and samples from the other discussions. I am stuck at a point. My code works fine when the output of the command is limited to a page. When the output is large and paginated, I am unable to get the complete output from the device. Can you please advise

Code snippet - ` import asyncio import sys import time import asyncssh

async def run_client(host, cmd):
    k_algs = ['ecdh-sha2-nistp521', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp256', 'diffie-hellman-group14-sha1',
              'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group1-sha1']
    e_algs = ['aes256-cbc', 'aes128-cbc', '3des-cbc', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr',
              'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com']
    m_algs = ['hmac-sha1']
    print('in run_client')
    async with asyncssh.connect(host, username=uname, password=pwd, port=22,
                                known_hosts=None, kex_algs=k_algs, encryption_algs=e_algs, mac_algs=m_algs,
                                compression_algs=None, ) as conn:
        async with conn.create_process(cmd) as process:

            print('after conn')
            print("**************************%s******************************" % cmd)
            process.stdin.write(f'{cmd}\r\n')
            process.stdin.write_eof()
            line = await process.stdout.read()
            print(str(line))
     

async def run_multiple_clients():
    
 
    host = host_ip
    cmd = 'display bgp peer'
    #cmd = 'display ospf interface all'
    await run_client(host, cmd)


start = time.time()


asyncio.get_event_loop().run_until_complete(run_multiple_clients())

end = time.time()

print(f'Time taken : {end - start}')

`

With the above code, display bgp peer works fine since its a 2 line output. display ospf interface all returns output only until — More —

Log - ` Backup Designated Router: 0.0.0.0 Timers: Hello 10 , Dead 40 , Poll 120 , Retransmit 5 , Transmit Delay 1 ---- More ---- Time taken : 1.447819709777832

` Thanks in advance

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ronfcommented, Jan 17, 2022

No - keep the code above basically the same, but don’t reuse “conn”. Open a new connection for each call to create_process(), and then send the paging disable, command, and exit on the process just like you’re doing here.

0reactions
mon-216commented, Jan 19, 2022

Closing this issue. As mentioned, paging issue resolved with disable paging commands.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AsyncSSH: Asynchronous SSH for Python — AsyncSSH 2.12 ...
The following example demonstrates setting environment variables for the remote session and displaying them by executing the 'env' command. import asyncio, ...
Read more >
Run commands on remote server with asyncssh
I'm trying to check how much space i have on my remote server, so i'm using df command. But i have no idea...
Read more >
Question about running multiple commands against network ...
I just have a fast question here, i am STRUGGGLING to get async ssh to run more than one command per session/connection/channel/anything i ......
Read more >
asyncssh - collecting continuous output from multiple pty ...
Hi Team, Looking for some guidance as a newbie with asyncssh where I want to maintain a large number of parallel SSH pty...
Read more >
A Tale of Five Python SSH Libraries - The Elegant Network
An evaluation of different SSH libraries in Python. ... Netmiko also comes with the ability to parse the command output and return it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found