Python Netmiko库返回发送命令中从未检测到的模式
我是一名网络工程师,我试图用一些python脚本自动化日常任务。我们正在使用cisco,所以我认为实现netmiko库会很好。 以下是用于连接到设备和编辑访问列表的脚本部分:Python Netmiko库返回发送命令中从未检测到的模式,python,paramiko,cisco,Python,Paramiko,Cisco,我是一名网络工程师,我试图用一些python脚本自动化日常任务。我们正在使用cisco,所以我认为实现netmiko库会很好。 以下是用于连接到设备和编辑访问列表的脚本部分: def Connection (DevParameters, device, HostAddress): try: net_connect = ConnectHandler(**DevParameters) except: print device[0] + ': Authe
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
它可以在空闲时逐行写入命令,但在执行脚本时失败:
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\#
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\Desktop\MAT.py”,第145行,在
连接(DevParameters、设备、主机地址)
文件“C:\Users\User\Desktop\MAT.py”,第90行,连接中
net\u connect.send\u命令(“配置终端”)
文件“C:\Program Files\Python27\lib\site packages\netmiko\base\u connection.py”,第827行,在send\u命令中
搜索(U模式)
IOError:在send_命令中从未检测到搜索模式\u expect:HQ\\u F3\\u 2960\\u D \-5\#
我尝试在发送命令()后实现sleep(),但没有成功。有什么问题吗?我遇到了类似的问题,但我用
设置长度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
有问题了。要输入配置终端的命令,必须使用send\u config\u set方法。