[Python] Execute several commands on Cisco switch via Paramiko - improve performance

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to login into a cisco switch and do some stuff there (configure a port for example). I can't use exec_command(cmd1 \n cmd2 \n) on cisco devices apparently... So I'm using a channel to send my commands on like so:

    channel = ssh.invoke_shell()
    channel.send("show arp\n")
    while not channel.recv_ready():
    time.sleep(0.5)
    out = channel.recv(9999)


    I don't like the time.sleep(0.5) and want something to dynamicaly wait for the command to be executed because sometimes commands take even longer than 0.5sec and that would slow down everything a lot. With exec_command(cmd) I had exit_status = channel.recv_exit_status()for example.

    Is there a way around this?


    Could I somehow listen to the channel and wait for the output to be like

    CISCOSWITCH#


    so that I know that I'm ready to send more commands or exit?


    I cannot use exec_command, because that closes the channel and when my intent is to configure a port I have to send multiple commands in the same session. (I think that's how it works)

    Continue reading...

Compartilhe esta Página