Use of send_command to send multiple commands on one line separated by a semicolon
See original GitHub issueHi, I need to send a “reload” (reboot) command over the SSH CLI to a network switch., however I need the Switch to delay sending the reload command for a minute. This is to allow all the switches in a resilient loop to receive the reboot command even if a switch earlier in the chain reboots first.
I’m using netmiko together with multiprocessing to send the command “ping ip 10.26.7.253 count 12 timeout 5; reload” So each switch should ping a non-existent address on the subnet and timeout for a minute before executing the reload command. So my reload function looks like this
def fReload(deviceip, user, password):
print("Reloading: "+user+"@"+deviceip)
try: # establish a connection to the device
ssh_connection = ConnectHandler(
device_type='linux',
ip=deviceip,
username=user,
password=password,
banner_timeout=10000,
auth_timeout=10000,
conn_timeout=10000
)
except Exception as err:
exception_type = type(err).__name__
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
write_file = open('Failed.txt','a')
write_file.write("\n"+dt_string+": Failed to send reload to: "+deviceip+" exception: "+exception_type)
print("Failed to connect to - ", deviceip+" exception: "+exception_type)
write_file.close()
else:
# delay reboot to allow all devices to receive command
try: # this command should timeout as reload command will not return to prompt
dotidx = deviceip.rfind('.')
badip = deviceip[0:dotidx] + '.253'
print ('badip = ', badip)
cmd_reload = 'ping ip ' + badip + ' count 10 timeout 5;reload'
print('sending ' + cmd_reload + ' to device ' + deviceip)
output = ssh_connection.send_command(cmd_reload)
except Exception as err:
exception_type1 = type(err).__name__
print("Exception sending reload command to - ", deviceip+" exception: "+exception_type1)
else:
print("sent reload command to device... ", deviceip)
ssh_connection.disconnect() # disconnect quickly to avoid error, as remote end will be rebooted
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
Results = dt_string+": Reloaded "+deviceip
print(Results)
write_file = open('Result.txt','a')
write_file.write("\n"+Results)
write_file.close()
The main program then calls this in a thread for each device in the list
for device in devices:
t = threading.Thread(target=fReload, args=(device, user, password,))
t.start()
threads.append(t)
I expect each command to fail with a timeout exception because the expected prompt string is never returned, but it doesn’t, it passes without exception. How will send_command treat the semicolon? I need it to be sent as one command. All I need to do is get the switch to delay rebooting itself for a minute. Is this possible?
Many thanks David
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Thanks again, Kirk, I think it’s working now that I cleaned up my test network. I’m getting an OSError exception rather than a specific Timeout error, but I guess that’s for the same reason ie no prompts returned after the reload command…
Okay, let me know if you are still seeing a problem here.
Just re-open the issue if you are.