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.

Use of send_command to send multiple commands on one line separated by a semicolon

See original GitHub issue

Hi, 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
davejmyerscommented, Nov 11, 2021

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…

0reactions
ktbyerscommented, Nov 11, 2021

Okay, let me know if you are still seeing a problem here.

Just re-open the issue if you are.

Read more comments on GitHub >

github_iconTop Results From Across the Web

28.16. Separating Commands with Semicolons
When the shell sees a semicolon (;) on a command line, it's treated as a command separator -- basically like pressing the ENTER...
Read more >
How do you execute multiple commands in a single session in ...
You can run multiple command using the below technique. Use semicolon to separate the Linux commands Eg: chan.exec_command("date;ls;free -m").
Read more >
; (Command Separator) - Windows drivers | Microsoft Learn
The semicolon ( ; ) character is used to separate multiple commands on a single line.
Read more >
Send Remote Commands Via SSH
The command you're sending is contained in single quotes. wherein each command is separated by a semicolon.
Read more >
Semicolon or double ampersand for chaining commands?
Use ; if you want to run one command, then another. ... you could separate them with ; (or just put them on...
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