zoukankan      html  css  js  c++  java
  • netmiko -- 网络设备运维库

    简介

    https://github.com/ktbyers/netmiko

    Multi-vendor library to simplify Paramiko SSH connections to network devices

    支持多设备厂商的库,简化SSH连接工作。

    https://pynet.twb-tech.com/blog/automation/netmiko.html

    The purposes of this library are the following:

    • Successfully establish an SSH connection to the device
    • Simplify the execution of show commands and the retrieval of output data
    • Simplify execution of configuration commands including possibly commit actions
    • Do the above across a broad set of networking vendors and platforms

    目的:

    成功建立SSH连接

    简化执行的show命令,和获取结果

    简化执行配置命令,包括可能的commit动作

    跨网络设备厂商和平台

    API

    https://ktbyers.github.io/netmiko/docs/netmiko/index.html#netmiko.ConnectHandler

    def send_command(self, command_string, expect_string=None, delay_factor=1, max_loops=500, auto_find_prompt=True, strip_prompt=True, strip_command=True, normalize=True, use_textfsm=False, textfsm_template=None, use_genie=False, cmd_verify=True)

    Execute command_string on the SSH channel using a pattern-based mechanism. Generally used for show commands. By default this method will keep waiting to receive data until the network device prompt is detected. The current network device prompt will be determined automatically.

    def send_config_set(self, config_commands=None, exit_config_mode=True, delay_factor=1, max_loops=150, strip_prompt=False, strip_command=False, config_mode_command=None, cmd_verify=True, enter_config_mode=True)

    Send configuration commands down the SSH channel.

    config_commands is an iterable containing all of the configuration commands. The commands will be executed one after the other.

    Automatically exits/enters configuration mode.

    Demo

    https://github.com/ktbyers/netmiko/blob/develop/EXAMPLES.md#connecting-to-multiple-devices

    show

    from netmiko import ConnectHandler
    from getpass import getpass
    
    cisco1 = { 
        "device_type": "cisco_ios",
        "host": "cisco1.lasthop.io",
        "username": "pyclass",
        "password": getpass(),
    }
    
    # Show command that we execute.
    command = "show ip int brief"
    
    with ConnectHandler(**cisco1) as net_connect:
        output = net_connect.send_command(command)
    
    # Automatically cleans-up the output so that only the show output is returned
    print()
    print(output)
    print()

    https://github.com/ktbyers/netmiko/blob/develop/EXAMPLES.md#configuration-changes

    #!/usr/bin/env python
    from netmiko import ConnectHandler
    from getpass import getpass
    
    device = {
        "device_type": "cisco_ios",
        "host": "cisco1.lasthop.io",
        "username": "pyclass",
        "password": getpass(),
    }
    
    commands = ["logging buffered 100000"]
    with ConnectHandler(**device) as net_connect:
        output = net_connect.send_config_set(commands)
        output += net_connect.save_config()
    
    print()
    print(output)
    print()

    Juniper设备的config模式需要特殊处理

    https://ultraconfig.com.au/blog/netmiko-automation-on-juniper-routers-full-tutorial-for-beginners/

    需要调用commit

    调用send_config_set需要添加exit_config_mode参数

    # Edit the candidate configuration
    config_commands = [ 'set interfaces ge-0/0/1 unit 0 family inet address 10.10.10.10/24',
                        'set interfaces ge-0/0/1 unit 0 description "Test Config"']
    output = net_connect.send_config_set(config_commands, exit_config_mode=False)
    print(output)



    # Commit the config changes
    output = net_connect.commit()
    print(output)
  • 相关阅读:
    vlc代码分析(3)——输入模块
    vlc学习计划(4)在EMACS中使用GDB调试
    There Are Free RTSP DirectShow Source Filters with full source code
    HDU 4283 You Are the One 第37届ACM/ICPC 天津赛区网络赛 1006题 (DP)
    HDU 4276 The Ghost Blows Light 第37届ACM/ICPC长春赛区1010题 (树形DP)
    POJ 1459 Power Network 最大流 dinic模板
    HDU 4273 Rescue 第37届ACM/ICPC 长春赛区网络赛1007题(三维凸包+重心+点面距离)
    HDU 4286 Data Handler 第37届ACM/ICPC 天津赛区网络赛1009题 (双向链表模拟)
    POJ 2246 ZOJ 1094 Matrix Chain Multiplication(简单题)
    HDU 4280 Island Transport 第37届ACM/ICPC 天津网络赛1003题 (最大流模板题,高效SAP模板)
  • 原文地址:https://www.cnblogs.com/lightsong/p/13495138.html
Copyright © 2011-2022 走看看