问题描述
我正在尝试使用 netmiko 和 Ansible 的 cli_command
模块一样工作。 cli_command
允许将任意字符串列表发送到设备(fe configure
、sh te 1/0/1
、exit
在单个多行字符串中)并且它可以正常工作。
Netmiko 需要单独的 send_config
和 send_command
,如果“configure”作为命令发送,则会导致连接挂起。
有什么办法可以强制 netmiko 在配置模式下像普通命令一样接受修改后的提示?
基本上,我希望它起作用:
connection.send_command('''
configure
shutdown te 1/0/1
end
show interface te 1/0/1
''')
解决方法
send_command
通过检测设备提示(基于模式)工作,然后当检测到提示时它知道命令已完成。当您使用 send_command
发送命令时,每个命令都会发生这种情况。
相反,使用 send_command_timing
它是 send_command
的另一个版本,但基于延迟并且不查找设备提示。
from netmiko.ssh_dispatcher import ConnectHandler
device = {
"device_type": "cisco_ios","ip": "","username": "","password": "","session_log": "log.log",}
with ConnectHandler(**device) as ssh_conn:
output = ssh_conn.send_command_timing( # Notice the send_command_timing()
command_string="""
configure terminal
interface loopback 925
shutdown
end
show interface loopback 925
"""
)
print(output)
还可以在 Python for Network Engineers 上查看此 blog post,我想您会发现它非常有用。