ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.49k stars 1.26k forks source link

Sending yes or no prompt for Cisco nexus device using send_command #3422

Closed adityamum closed 2 months ago

adityamum commented 2 months ago

Hi Kirk, I am trying to send the below command to the nexus 9k switch. Command: install all nxos bootflash:///nxos.7.0.3.I5.2.bin Installer will perform compatibility check first. And manually we have to send yes command after the prompt for installation. Process usually takes around 15 mins manually

I am trying to send y command using send_command for Do you want to continue with the installation? String. But it's going in next line and I tried to use send_command_timing but it's not working as it's giving half output

Please find the error below for % Invalid command at marker:

Installer will perform compatibility check first. Please wait. Installer is forced disruptive

Verifying image bootflash:/nxos.7.0.3.I5.2.bin for boot variable "nxos". [####################] 100% -- SUCCESS

Verifying image type. [####################] 100% -- SUCCESS

Preparing "nxos" version info using image bootflash:/nxos.7.0.3.I5.2.bin. [####################] 100% -- SUCCESS Images will be upgraded according to following table: Mod

Additional info for this installation:

Service "vpc" in vdc 1: Vpc is enabled, Please make sure both Vpc peer switches have same boot mode using 'show

Switch will be reloaded for disruptive upgrade.

Do you want to continue with the installation (y/n)? [n]

Hostname# y

% Invalid command at marker.

Hostname#

Please find the code below

device = {
"device_type": 'cisco_nxos',
"host":f" (host)",
"username": "user",
"password": "pass",
 "global delay factor":20,
 "fast cli": False
}
net_connect = ConnectionHandler(**device)
command= f"install all nxos bootflash: (filename)"

output=net_connect.send_command(command, read_timeout=20000, strip_prompt=False, strip_command-False)

#print (output)

If "Do you want to continue with the installation (y/n)?" in output:

          output+=net_connect.send_command (command_string="y", read_timeout=20000,strip_prompt=False, strip_command=False)

print(output)

Pls help.

ktbyers commented 2 months ago

You probably need to add an expect_string argument to this line:

output = net_connect.send_command(command,  expect_string=r"Do you want to continue", strip_prompt=False, strip_command=False)
output += net_connect.send_command("y", expect_string=r"Hostname#", read_timeout="VALUE", strip_prompt=False, strip_command=False)

You will need to set read_timeout to the appropriate value.

I would try something similar to the above.

adityamum commented 2 months ago

Hey @ktbyers thanks for the above command. I used send_command_multiline and it works!!

command = [[f"install all nxos bootflash:(filename)",r"Do you want to continue with the installation"],["y",""]] output=net_connect.send_multiline (command, read_timeout=20000,strip_prompt-false, strip_command=False)