Closed amarbaranwal closed 5 years ago
I can see that, due to mismatch in expected pattern, it's failing. Is there an option/solution to handle this situation?
You are maybe looking for set_base_prompt
-- in this example my hostname starts as "3560CX":
>>> conn.send_config_set(['hostname SOMETHING'])
'config term\nEnter configuration commands, one per line. End with CNTL/Z.\n3560CX(config)#hostname SOMETHING\nSOMETHING(config)#end\nSOMETHING#'
>>>
>>> conn.base_prompt
'3560CX'
>>> conn.set_base_prompt()
'SOMETHING'
>>> conn.base_prompt
'SOMETHING'
>>> conn.send_config_set(['hostname 3560CX'])
'config term\nEnter configuration commands, one per line. End with CNTL/Z.\nSOMETHING(config)#hostname 3560CX\n3560CX(config)#end\n3560CX#'
>>> conn.set_base_prompt()
'3560CX'
>>> conn.base_prompt
'3560CX'
>>>
You should be able to connect, connect to 'nxos a', then reset the base prompt. See also: #1226 which is similar. Let us know if that helps!
Hi Carl, thank you for your comment. I tried with "set_base_prompt()" but that didn't help. Exception is hit with execution of "send_command("connect nxos a")" itself. Below is the sequence of prompt change: 053119 12:14:12:INFO: Successfully logged into DXB-FI01-A(10.13.4.100) 053119 12:14:12:INFO: Find Prompt before 'connect nxos a': DXB-FI01-A# 053119 12:14:12:INFO: Base Prompt before 'connect nxos a': DXB-FI01-A
executing net_connect.send_command("connect nxos a")
053119 12:15:53:INFO: Find Prompt after hitting exception: DXB-FI01-A(nxos)# 053119 12:15:53:INFO: Base Prompt after hitting exception: DXB-FI01-A 053119 12:15:53:INFO: Hit issue with 'connect nxos a'
After executing net_connect.send_command("connect nxos a") it hangs for approx 100 secs. and hits exception concluding - 'Search pattern never detected in send_command_expect: DXB-FI01-A#'. With execution of "connect nxos a", the value of "find_prompt()" has changed. However, the code expects the previously recorded prompt.
I checked the code flow of "send_command()" where the expected pattern is stored before write_channel(command_string) and same is being used for lookup after execution.
As of now, I am accepting the exception and passing on. But that is costing me 100 seconds of wait time against one such CLI.
Try:
net_connect.send_command_timing("connect nxos a")
And then execute set_base_prompt()
after that.
Another option is:
net_connect.send_command("connect nxos a", expect_string=r"#")
Note, by default send_command is going to search for part of the current prompt which is why your command fails. The _timing method doesn't look for a pattern it is entirely delay based. Adding the expect_string argument changes the pattern that is looked for.
Hi Kirk, "expect_string" option did help. My mistake that I overlooked the available option with "send_command()" method. Thanks a lot, Amar
Please someone help me with this issue Code:
from netmiko import ConnectHandler from getpass import getpass import time from datetime import datetime start_time = datetime.now() start = time.time() host = input('Enter ip address: ') device = {'device_type': 'cisco_asa', 'host': host, 'username':'reuben', 'password':'cisco', 'port': '22', 'secret': 'cisco', 'verbose': True}
net_connect = ConnectHandler(**device) cmd = 'copy disk0:ospf7.txt disk0:ospf12.txt'
output = net_connect.send_command(cmd) if 'Source filename' in output: output += net_connect.send_command('\n') if 'Destination filename' in output: output += net_connect.send_command('\n')
print(output)
print(output)
Error:
C:\network_automation_2\venv\Scripts\python.exe C:/network_automation_2/testing_commands_netmiko.py
Enter ip address: 10.7.250.54
SSH connection established to 10.7.250.54:22
Interactive SSH session established
Traceback (most recent call last):
File "C:/network_automation_2/testing_commands_netmiko.py", line 19, in
Process finished with exit code 1
Hi Kirk, "expect_string" option did help. My mistake that I overlooked the available option with "send_command()" method. Thanks a lot, Amar
Hey , can you please help us with the solution?
@sachin0987 It is best if you open a new issue and include your failing code (simplified to just what fails) and the exception stack trace. The issue here is closed.
I am trying to automate below steps using netmiko APIs over UCS Fabric Interconnect Switches, where script is expected to login first on the Virtual Device IP - device_ip and then execute "connect nxos a" to access Active node and "connect nxos b" for Standby Node.
Below is the snippet of manual execution: sshpass -p $PASSWORDCFG ssh -oStrictHostKeyChecking=no -l $USER device_ip Cisco UCS 6200 Series Fabric Interconnect Cisco Nexus Operating System (NX-OS) Software .... AAN-FI01-A# AAN-FI01-A# connect nxos a <== CLI to execute Cisco Nexus Operating System (NX-OS) Software ... AAN-FI01-A(nxos)# <== Active Node mode, where prompt changes
log snippet from script executed in debug mode: INFO: Connected (version 2.0, client OpenSSH_6.2) INFO: Auth banner: b'Cisco UCS 6200 Series Fabric Interconnect\n' INFO: Authentication (password) successful! INFO: Successfully logged into AAN-FI01-A(10.15.4.100)