欢迎光临
我们一直在努力

Python模块-Netmiko入门

支持平台

Netmiko支持各种设备。这些设备分为三类:

  • 定期测试
  • 有限测试
  • 实验性

定期测试意味着我们尝试在每个Netmiko版本之前针对该套设备运行完整的测试套件。

有限测试意味着在某个时间点针对该平台上的测试通过的配置和显示操作系统测试,因此,我们可以合理地认为驱动程序通常应该可以工作。

实验意味着我们检查了PR,并且驱动程序似乎是合理的,但是关于驱动程序是否完全通过单元测试或其可靠性如何,我们没有足够的数据。

支持平台如下:

定期测试
  • Arista vEOS
  • Cisco ASA
  • Cisco IOS
  • Cisco IOS-XE
  • Cisco IOS-XR
  • Cisco NX-OS
  • Cisco SG300
  • HP ProCurve
  • Juniper Junos
  • Linux
有限测试
  • Alcatel AOS6/AOS8
  • Apresia Systems AEOS
  • Calix B6
  • Cisco AireOS (Wireless LAN Controllers)
  • CloudGenix ION
  • Dell OS9 (Force10)
  • Dell OS10
  • Dell PowerConnect
  • Extreme ERS (Avaya)
  • Extreme VSP (Avaya)
  • Extreme VDX (Brocade)
  • Extreme MLX/NetIron (Brocade/Foundry)
  • HPE Comware7
  • Huawei
  • IP Infusion OcNOS
  • Juniper ScreenOS
  • Mellanox
  • MikroTik RouterOS
  • MikroTik SwitchOS
  • NetApp cDOT
  • Nokia/Alcatel SR OS
  • OneAccess
  • Palo Alto PAN-OS
  • Pluribus
  • Ruckus ICX/FastIron
  • Ruijie Networks
  • Ubiquiti EdgeSwitch
  • Vyatta VyOS
实验室
  • A10
  • Accedian
  • Aruba
  • Ciena SAOS
  • Citrix Netscaler
  • Cisco Telepresence
  • Check Point GAiA
  • Coriant
  • Dell OS6
  • Dell EMC Isilon
  • Eltex
  • Enterasys
  • Endace
  • Extreme EXOS
  • Extreme Wing
  • Extreme SLX (Brocade)
  • F5 TMSH
  • F5 Linux
  • Fortinet
  • MRV Communications OptiSwitch
  • MRV LX
  • QuantaMesh
  • Rad ETX
  • Versa Networks FlexVNF

安装

要安装netmiko,只需使用我们的pip:

$ pip install netmiko

Netmiko具有以下要求

  • Paramiko >= 2.4.3
  • scp >= 0.13.2
  • pyserial
  • textfsm

创建代表设备的字典

支持的device_types可以在ssh_dispatcher.py中找到,请参阅CLASS_MAPPER键。

from netmiko import ConnectHandler

cisco_881 = {
    'device_type': 'cisco_ios',
    'host':   '10.10.10.10',
    'username': 'test',
    'password': 'password',
    'port' : 8022,          # optional, defaults to 22
    'secret': 'secret',     # optional, defaults to ''
}

通过传入设备字典与设备建立SSH连接。

net_connect = ConnectHandler(**cisco_881)

执行show命令。

output = net_connect.send_command('show ip int brief')
print(output)
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0              unassigned      YES unset  down                  down
FastEthernet1              unassigned      YES unset  down                  down
FastEthernet2              unassigned      YES unset  down                  down
FastEthernet3              unassigned      YES unset  down                  down
FastEthernet4              10.10.10.10     YES manual up                    up
Vlan1                      unassigned      YES unset  down                  down

执行配置更改命令(将自动进入配置模式)

config_commands = [ 'logging buffered 20000',
                    'logging buffered 20010',
                    'no logging console' ]
output = net_connect.send_config_set(config_commands)
print(output)
pynet-rtr1#config term
Enter configuration commands, one per line.  End with CNTL/Z.
pynet-rtr1(config)#logging buffered 20000
pynet-rtr1(config)#logging buffered 20010
pynet-rtr1(config)#no logging console
pynet-rtr1(config)#end
pynet-rtr1#

Netmiko常用方法:

  • net_connect.send_command()-在通道上发送命令,返回输出(基于模式)
  • net_connect.send_command_timing()-在通道上发送命令,返回输出(基于定时)
  • net_connect.send_config_set()-将配置命令发送到远程设备
  • net_connect.send_config_from_file()-发送从文件加载的配置命令
  • net_connect.save_config()-将运行配置保存到启动配置
  • net_connect.enable()-进入启用模式
  • net_connect.find_prompt()-返回当前路由器提示
  • net_connect.commit()-在Juniper和IOS-XR上执行提交操作
  • net_connect.disconnect()-关闭连接
  • net_connect.write_channel()-通道的低级写入
  • net_connect.read_channel()-通道的低级写入
赞(37)
未经允许不得转载:攻城狮阿龙 » Python模块-Netmiko入门
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址