Netmiko 模块 4 种设备配置函数:
(1)send_command():向设备发送⼀条命令,通常⽤于查询、排错、保存等命令。
(2)send_config_set():向设备发送⼀条或多条配置命令,会⾃动适配到配置模式,通常配合指令列表。
(3)send_config_from_file():如 send_config_set() 指令列表过⼤,可单独放⼊⽂本中配合 send_config_from_file() 调⽤。
(4)send_command_timing():主动延迟等待,主要⽤于应对设备交互管理平台回显量⼤出现有意延迟或异常卡顿的现象。
netmiko 离线安装
from netmiko import ConnectHandler
sw1 = {'device_type':'huawei', 'ip':'192.168.1.9', 'username':'python', 'password':'123asd'}
commands = ['interface GigabitEthernet 0/0/1', 'description descby_send_config_set()']
with ConnectHandler(**sw1) as connect:
print ("已经成功登陆交换机" + sw1['ip'])
# 公众号:释然IT杂谈
print('===实验⽬的(1),交互形式推送⼀条指令。')
output = connect.send_command('display interface description | include GE0/0/[12][^0-9]')
print(output)
print('===实验⽬的(2),列表形式推送多条指令。')
output = connect.send_config_set(commands)
print(output)
print('===实验⽬的(3),⽂件形式推送多条指令。')
output = connect.send_config_from_file('Demo08.txt')
print(output)
print('===最后再检查配置')
output = connect.send_command('display interface description | include GE0/0/[12][^0-9]')
print(output)
# 华为设备的保存配置save后需要输⼊y进⾏确认,后⾯实验再演示。
Demo08.txt
interface GigabitEthernet 0/0/2
description descby_send_config_set()
C:\Users\82127\.conda\envs\pythonProject\python.exe E:/log/python/pythonProject/Demo08.py
已经成功登陆交换机192.168.1.9
===实验⽬的(1),交互形式推送⼀条指令。
PHY: Physical
*down: administratively down
(l): loopback
(s): spoofing
(b): BFD down
(e): ETHOAM down
(dl): DLDP down
(d): Dampening Suppressed
Interface PHY Protocol Description
GE0/0/1 up up descby_send_config_set()
GE0/0/2 down down
===实验⽬的(2),列表形式推送多条指令。
system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]interface GigabitEthernet 0/0/1
[Huawei-GigabitEthernet0/0/1]description descby_send_config_set()
[Huawei-GigabitEthernet0/0/1]return
<Huawei>
===实验⽬的(3),⽂件形式推送多条指令。
system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]interface GigabitEthernet 0/0/2
[Huawei-GigabitEthernet0/0/2]description descby_send_config_set()
[Huawei-GigabitEthernet0/0/2]return
<Huawei>
===最后再检查配置
PHY: Physical
*down: administratively down
(l): loopback
(s): spoofing
(b): BFD down
(e): ETHOAM down
(dl): DLDP down
(d): Dampening Suppressed
Interface PHY Protocol Description
GE0/0/1 up up descby_send_config_set()
GE0/0/2 down down descby_send_config_set()
进程已结束,退出代码0