Dealing with commands that do not return a prompt
See original GitHub issueI have looked all over and can’t seem to find an answer that works consistently.
I have a script that will run iperf tests from router to router and I am having issues getting it to be consistent. I am running at the enable prompt with “guestshell run sudo iperf3 -s”. It either says it is running and does not return a prompt or gives an error and returns a prompt.
My issue comes into play when the script issues the above command but it doesn’t return the output I am looking for to continue or not before it moves to the next command. I have tried different variations on send_commands_timing with delay_factor and max_loops set but it isn’t consistent.
Below are snips from both the debug log and my code. Let me know if I need to submit more detail.
----------------------Relevant Code--------------------
iperf_status = att_connection.send_command_timing('guestshell run sudo iperf3 -s',
delay_factor=8, max_loops=600) # initial run of iperf
if 'Server listening' in iperf_status: # check if it's running
return (iperf_status, iperf_server, att_connection, a_host_name)
else:
# server wasn't listening, stop and then restart guest shell and try again
print(f'Restarting guestshell on {a_host_name}, please wait.')
att_connection.send_command_timing('guestshell disable', delay_factor=2) #force close guestshell
time.sleep(5)
att_connection.send_command_timing('guestshell enable', delay_factor=2) # restart guestshell
time.sleep(5)
iperf_status = att_connection.send_command_timing('guestshell run sudo iperf3 -s', delay_factor=2) # try again
if 'Server listening' in iperf_status: # see if it's running and exit function if it isn't
print(f'Successfull restart of guestshell on {a_host_name} and iperf successfully started')
return (iperf_status, iperf_server, att_connection, a_host_name)
else:
return(f'Error encountered after attempted restart of guestshell on: {iperf_status}',iperf_server, a_host_name)
-----------------------Debug log ---------------------- DEBUG:netmiko:write_channel: b’guestshell run sudo iperf3 -s\n’ DEBUG:netmiko:read_channel: guestshell run sudo iperf3 -s # initial run sets variable to null
DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b’guestshell disable\n’ # fails iperf_status check - null DEBUG:netmiko:read_channel: ^@----------------------------------------------------------- # output from initial run Server listening on 5201
guestshell disable # script thinks it didn’t run
DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel:
repeat of initial run where it fails check and thinks it didn’t run a second time so it exits script
As you can see, the command did run as expected initially but output was returned after issuing the command to forcefully exit the guestshell because the value of iperf_status is nothing (debug shows iperf_status = ‘’).
I have also tried send_command with expect_string=‘[expected output]’ but since I could get one of three outputs (null, running, unable to run) if it gets anything but what I specify it raises an exception, which is expected I would think.
Is there a way to make the send_command_timing wait just a bit longer aside from delay_factor and/or max_loops to actually capture the output I am looking for? Running manually, the command takes 2-5 seconds to complete so I think a delay of 8 would be sufficient.
What am I missing?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
That was the answer. I kinda figured I was missing something somewhere.
Regular expressions are not my forte.
Thanks again for the assistance
Thank you for the response, I will try it and let you know.