cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1473
Views
0
Helpful
0
Replies

APIC attach leaf Python Netmiko

O_H
Level 1
Level 1

Hello, i'm building a Python Netmiko code to login to APIC, collect list of nodes, then attach to each node and run some commands that need to be done on the leaf itself (not from Apic via fabric command).

 

Now, it seems the code can login (attach) to the leaf, but then gets stuck with netmiko.exceptions.ReadTimeout: which doesn't make sense to me because it is already logged in as shown in the log file.

 
switch = {
    'device_type': 'cisco_nxos',
    'host':   host_ip,
    'username': username,
    'password': password,
    'port' : 22,          # optional, defaults to 22
    'secret': 'admin',     # optional, defaults to ''
    'session_log': 'log.log',    #generate a log session for the code to teshoot the code
    'ssh_config_file': ssh_config,
    'banner_timeout': 3
}

try:        #Avoid Timeout & Auth errors and continuo for next switch
    net_connect = ConnectHandler(**switch)
except (NetMikoTimeoutException, NetMikoAuthenticationException):
    print ('\n' + 'Cannot connect to device: ' + host_ip)
    sys.exit()    

host_list.append(host_ip)      #Register IPs of connected hosts

timestamp = date.today()
#timestamp = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")       #Time including hours, minutes, seconds

hostname = net_connect.find_prompt()[:-1]   #Get the hostname

print ("Checking current status of ACI fabric ports for " + hostname + "_" + (host_ip))

command1 = "show switch"
#with ConnectHandler(**cisco1) as net_connect:
output1 = net_connect.send_command(command1)   
node_list = re.findall(r"(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)", output1, re.MULTILINE)

x=0
for i in node_list: 
    node_id = node_list[x][0]
    node_name = node_list[x][10]
    node_diffrentiator = node_list[x][8]
    x=x+1
    output2 = net_connect.send_command(command_string=f"attach {node_name}", expect_string=r"Password:")
    output2 += net_connect.send_command(command_string=f"{password}", expect_string=r"#", read_timeout=30)
    node_hostname = net_connect.find_prompt()[:-1]

error:
Traceback (most recent call last):
  File "ACI_Interfaces_Health_Check.py", line 132, in <module>
    output2 += net_connect.send_command(command_string=f"{password}")
  File "/usr/local/lib/python3.6/site-packages/netmiko/utilities.py", line 600, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 1644, in send_command
    new_data = self.command_echo_read(cmd=cmd, read_timeout=10)
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 1370, in command_echo_read
    pattern=re.escape(cmd), read_timeout=read_timeout
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 651, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:

Pattern not detected: '' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.

log output:
Last login: 2022-06-20T16:30:50.000+02:00 UTC
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

APIC# tterminal width 511
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

APIC# tterminal length 0
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

APIC# 
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

APIC# 
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

APIC# 
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

APIC# 
%                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
 

APIC# attach LEAF101
This command is being deprecated on APIC controller, please use NXOS-style equivalent command
# Executing command: ssh LEAF101 -b 10.4.0.1

Password:
Password:
Password:
Last login: Mon Jun 20 16:27:11 2022 from 10.4.0.1
Cisco Nexus Operating System (NX-OS) Software
TAC support: http://www.cisco.com/tac
Copyright (c) 2002-2018, Cisco Systems, Inc. All rights reserved.    
The copyrights to certain works contained in this software are
owned by other third parties and used and distributed under
license. Certain components of this software are licensed under
the GNU General Public License (GPL) version 2.0 or the GNU
Lesser General Public License (LGPL) Version 2.1. A copy of each
such license is available at
http://www.opensource.org/licenses/gpl-2.0.php and
http://www.opensource.org/licenses/lgpl-2.1.php
LEAF101# 


0 Replies 0
Review Cisco Networking for a $25 gift card