Netmiko库返回在send_command_expect中从未检测到的模式

时间:2017-12-21 08:30:43

标签: python paramiko cisco

我是一名网络工程师,我尝试使用一些python脚本自动执行例行任务。我们正在使用思科,所以我认为实现netmiko库会很好。 以下是脚本的一部分,用于连接设备和编辑访问列表:

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

它可以很好地从IDLE逐行编写命令,但在执行脚本时它失败了:

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\#

我尝试在send_command()之后实现sleep()无效。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,但我用set length 0.解决了这个问题 我分享我的代码,希望能帮到你。

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