[Python] paramiko excuting juniper command

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    for the below function code

    def bras_command(channel, command, max_wait=5, buffer_size=9999):
    channel.send(command + '\n')
    output = b''
    total_wait = 0

    # Wait up to `max_wait` seconds for data to appear
    while not channel.recv_ready() and total_wait < max_wait:
    sleep(1)
    total_wait += 1

    if not channel.recv_ready():
    print("⚠️ No output received from BRAS.")
    return ''

    # Once output starts arriving, keep reading until delay hits
    sleep(1) # let full command execute
    last_chunk_time = time()

    while True:
    if channel.recv_ready():
    chunk = channel.recv(buffer_size)
    output += chunk
    last_chunk_time = time()
    else:
    if time() - last_chunk_time > 1.5: # 1.5s of silence = end
    break
    sleep(0.2)

    decoded = output.decode("utf-8", errors="ignore")
    print(decoded)
    return decoded


    i am trying to excute the command command = 'show interfaces descriptions | match 13-3-13-90' but when the function excutes, it gives no output, after many trials i found that this is because when human do it, it should be like typing 'show interfaces desc' and when pressing space it will wait a while then the word desciptions appear, hence '| ' which will wait a second or so, then pressing on 'm' hence space and wait a shorter time than desc hence the word match appear then tryping the string '13-3-13-90' and pressing enter to excute

    i couldn't simulate it by all means. can anyone help me?

    here is the output @CMG03-ATO> show interfaces descriptions | match 13-3-13-90

    here is what should be expected when excuted

    @CMG03-ATO> show interfaces descriptions | match 13-3-13-90 ae1114.3182 up up 13-3-13-90-ZHRPC90-ZHRP-ATO-T3182-L1798-P33 ae1114.3186 up up 13-3-13-900-ZHRPC900-ZHRP-ATO-T3186-L1799-P33

    Continue reading...

Compartilhe esta Página