Python Netmiko error while making SSH connection to cucm server

1.9k views Asked by At

Just a beginner on python, need some expert advice. I am working on windows machine, have python 2.7 installed , trying to run one script which will connect to one of the cisco call manager CLI and run some commands (example -admin:utils create report hardware). I have attached screen shot of putty trough which I connect in normally, but would like to automate this. Any help on this will be appreciated.

I have tried using netmiko but it says below error - enter image description here

>>> from netmiko import ConnectHandler
>>> cisco_881 = {'device_type': 'cisco_ios','ip': '10.10.201.11','username': 
'Admin','password': 'admin123'}
>>> net_connect = ConnectHandler(**cisco_881)

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    net_connect = ConnectHandler(**cisco_881)
  File "build\bdist.win32\egg\netmiko\ssh_dispatcher.py", line 122, in 
ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "build\bdist.win32\egg\netmiko\base_connection.py", line 146, in 
__init__
    self.session_preparation()
  File "build\bdist.win32\egg\netmiko\cisco\cisco_ios.py", line 11, in 
session_preparation
    self.set_base_prompt()
  File "build\bdist.win32\egg\netmiko\base_connection.py", line 634, in 
set_base_prompt
    raise ValueError("Router prompt not found: {0}".format(prompt))
ValueError: Router prompt not found: admin:
3

There are 3 answers

0
Mert Kulac On

I recommend using the prompt feature below. With this method, you can quickly configure the device by exploring the prompt.

            prompt_find_fnk = config_commands.find_prompt()
            hostname_fnk = prompt_find_fnk.strip("<" + ">")
            print(hostname_fnk)
            config_commands.send_command_timing("enable")
            output = config_commands.send_command_timing("config")
            print("config mode entered")
            config_commands.send_command_timing("show version ")
            config_commands.send_command_timing("quit")
            config_commands.send_command_timing("save")
            
0
Arvind Rosunee On

The CUCM prompt is admin: (ends with a colon :) so you want to modify base_connection.py.

I got some success with the below:

near line 619, added the part in bold:

def set_base_prompt(self, pri_prompt_terminator='#', alt_prompt_terminator='>', cucm_prompt_terminator=':', delay_factor=1):

near line 633, added the part in bold:

if not prompt[-1] in (pri_prompt_terminator, alt_prompt_terminator, cucm_prompt_terminator):

I have not done extensive testing for any side effects when accessing other devices of type cisco_ios, but the above gives the gist of how you may workaround your issue.

0
Shervin Hariri On

You could check this code :

from netmiko import ConnectHandler

RTR_150 = {
    "device_type": "cisco_ios",
    "host": "your_router_ip",
    "username": "test",
    "password": "test",
    "secret": "enable_password"}

with ConnectHandler(**RTR_150) as net_connect:
    config_commands = [ "show env",
                        "show version"]

    output = net_connect.send_config_set(config_commands)
    net_connect.disconnect()
    
    print(output)