2

I am working as a network engineer and I have tried to automate routine tasks with some python scripts. We are using cisco so I thought it would be nice to implement netmiko library. Here is the part of the script which is used to connect to device and edit access-lists:

def Connection (DevParameters, device, HostAddress):
    try:
        net_connect = ConnectHandler(**DevParameters)
    except:
        print device[0] + ': Authentication failed or device unavailable'
        return
    access_class = net_connect.send_command("sh run | inc access-class")
    if access_class == '':
        print device[0] + ': No access-class configured.' 
        net_connect.disconnect()
        return
    access_class = access_class.splitlines()[0].split(' ')[-2]
    acl_type = net_connect.send_command("sh ip access-list " + access_class).splitlines()[0].split(' ')[0].lower()
    net_connect.send_command('configure terminal')
    net_connect.send_command('ip access-list ' + acl_type + access_class)
    if acl_type == 'extended':
        net_connect.send_command('permit tcp host' + HostAddress + ' any eq 22')
    elif acl_type == 'standard':
        net_connect.send_command('permit ' + HostAddress )
    else:
        print device[0] + ': Unexpected ACL type. Connection closed.'
       net_connect.disconnect()
        return
    print device[0] + ': Configured.'
    net_connect.disconnect
    return

It works nicely from IDLE writing command line by line but when executing script it fails with:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\MAT.py", line 145, in <module>
    Connection (DevParameters, device, HostAddress)
  File "C:\Users\User\Desktop\MAT.py", line 90, in Connection
    net_connect.send_command('configure terminal')
  File "C:\Program Files\Python27\lib\site-packages\netmiko\base_connection.py", line 827, in send_command
    search_pattern))
IOError: Search pattern never detected in send_command_expect: HQ\_F3\_2960\_D\-5\#

I tried to implement sleep() after send_command() to no avail. What could be the issue?

i80r
  • 21
  • 1
  • 3
  • Got the problem. To enter commands for configuration terminal send_config_set method must be used. – i80r Dec 21 '17 at 11:12

2 Answers2

0

I've got a similar problem, but I solved it with set length 0. I share my code, I hope to help you.

extrm_X460 = {
    'device_type': 'extreme',
    'ip':   ip,
    'username': 'username',
    'password': 'password',
    'port' : 22,          
}

try:
    # Connect
    print "Trying to connect to: " + ip
    net_connect = ConnectHandler(**extrm_X460)

    # Get info
    print "Connected to: " + ip
    net_connect.send_command('set length 0')
    output = net_connect.send_command('show mac port ge.*.*')
    print output

except (IOError, EOFError, SSHException, NetMikoTimeoutException, NetMikoAuthenticationException) as e:
    print e
    continue
0

Use send_command_timing instead of send_command.

  • send_command is detecting a device prompt (pattern-based) (it will work with "show" commands).
  • send_command_timing is delay-based and doesn't look for the device prompt (will work in configuration mode [conf t])

Example: net_connect.send_command_timing

karim_29
  • 1
  • 1