批量下发华为、思科设备配置

通常我们对较多交换机进行配置变更的时候,都是通过人为 Login 设备,将需要变更的指令输入进去。这不仅效率低,容易出错,而且对做重复事情的厌恶情绪会直接影响变更的过程。

由此本文将介绍如何使用 Python Netmiko 模块批量配置交换机的方法。

环境准备(本文采用 Windows 系统)

Python3 安装

打开 Python 下载链接:
https://www.python.org/downloads/release/python-373/

选择对应版本的安装包,点击下载

下载完成后,打开 Python 安装包
安装步骤如下:
一、如图勾选下方的两个选项框,点击 Customize installation 进入下一步安装步骤:

二、所有选项保持默认,点击 Next 进入下一步安装步骤:

三、如图勾选 Install for users 选项,点击 Install 开始安装:

四、等待安装程序完成

五、验证安装结果(以管理员身份运行 CMD)
C:\Users\Administrator> python -V
Python 3.7.3 #控制台输出表示安装成功
六、安装 netmiko 模块(系统自带getpass 模块)
安装 netmiko

C:\Users\Administrator> pip install netmiko
Collecting netmiko100% |█████████████████| 1.5MB 436kB/sSuccessfully installed bcrypt-3.1.7 cffi-1.14.0 cryptography-2.9.2 future-0.18.2 netmiko-3.1.1 paramiko-2.7.1 pycparser-
2.20 pynacl-1.4.0 pyserial-3.4 scp-0.13.2 six-1.15.0 textfsm-1.1.0验证
C:\Users\Administrator> python
>>> import netmiko
>>> #引入模块无报错,说明模块安装成功

Netmiko模块介绍

当前支持设备类型

Arista vEOS
Cisco ASA
Cisco IOS
Cisco IOS-XE
Cisco IOS-XR
Cisco NX-OS
Cisco SG300
HP ProCurve
Juniper Junos
LinuxAlcatel 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 VyOSA10
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

常用方法

net_connect.send_command() #向下发送命令,返回输出(基于模式)
net_connect.send_command_timing() #沿通道发送命令,返回输出(基于时序)
net_connect.send_config_set() #将配置命令发送到远程设备
net_connect.send_config_from_file() #发送从文件加载的配置命令
net_connect.save_config() #将running#config保存到startup#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() #通道的低级写入

参考链接

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

配置 Python 脚本

一、Cisco

编辑文件,文件命名Cisco_Autoconfig.py

#!/usr/bin/python3
#-*- coding:utf-8 -*-
# ScriptName:  Cisco_Autoconfig.py
# Create Date: 2020-07-08 16:35
# Modify Date: 2020-07-08 16:35
#***************************************************************#
from netmiko import ConnectHandler,NetmikoTimeoutException,NetmikoAuthenticationException #引入netmiko连接模块、报错模块
import getpass #引入密码模块
import time #引入时间模块
date = time.strftime('%Y%m%d', time.localtime()) #赋予date变量
password = getpass.getpass('Password:') #赋予password变量host={'192.168.1.10',
'192.168.1.11',
}; #定义需要下发配置的主机 IPfor ip in host: #定义循环#创建字典cisco_ios = {'device_type':"cisco_ios", #定义设备类型'ip':ip, #调用变量ip'port':'22', #指定端口,默认为22'username':'admin', #设备登录名'password':password, #调用getpass模块#'secret' : 'admin' #enable密码}try:cisco_connect = ConnectHandler(**cisco_ios) #传入设备字典与设备建立SSH连接。print ("Sucessfully Login to",ip)print ("Building configuration...")config = ['ntp server 192.168.1.1','ntp server 192.168.1.2','do show run | sec ntp'] #定义需要配置的命令input = cisco_connect.send_config_set(config) #执行命令print(input) #打印输出结果print(ip,'Was finished!\n',"-"*100)except NetmikoAuthenticationException : #认证失败报错记录e1 = open(f'{date}.txt','a')print(date,ip,'[Error 1] Authentication failed.\n',file = e1)e1.closeexcept NetmikoTimeoutException : #登录超时报错记录e2 = open(f'{date}.txt','a')print(date,ip,'[Error 2] Connection timed out.\n',file = e2)e2.closeexcept : #未知报错记录e3 = open(f'{date}.txt','a')print(date,ip,'[Error 3] Unknown error.\n',file = e3)e3.close
cisco_connect.disconnect() #断开SSH连接

执行脚本

C:\Users\Administrator> python Cisco_Autoconfig.py
Password:   #输入密码
Sucessfully Login to 192.168.1.10
Building configuration...
config term
Enter configuration commands, one per line.
Cisco_C2960_01(config)#ntp server 192.168.1.1
Cisco_C2960_01(config)#ntp server 192.168.1.2
Cisco_C2960_01(config)#do sho run | in ntp
ntp server 192.168.1.1
ntp server 192.168.1.2
Cisco_C2960_01(config)#end
Cisco_C2960_01#
192.168.1.10 Was finished!
---------------------------------------------------------
Sucessfully Login to 192.168.1.11
Building configuration...
config term
Enter configuration commands, one per line.
Cisco_C2960_02(config)#ntp server 192.168.1.1
Cisco_C2960_02(config)#ntp server 192.168.1.2
Cisco_C2960_02(config)#do sho run | in ntp
ntp server 192.168.1.1
ntp server 192.168.1.2
Cisco_C2960_02(config)#end
Cisco_C2960_02#
192.168.1.11 Was finished!
---------------------------------------------------------
C:\Users\Administrator>

当主机出现不可达或者密码错误时,在执行过程中会自动创建以日期命名的 txt 文件,记录失败主机 IP 、时间和原因,如下:

C:\Users\Administrator> more 20200709.txt
20200708 192.168.1.11 [Error 1] Authentication failed.
20200708 192.168.1.12 [Error 2] Connection timed out.
20200708 192.168.1.13 [Error 3] Unknown error.

二、华为

编辑文件,文件命名Huawei_Autoconfig.py

#!/usr/bin/python3
#-*- coding:utf-8 -*-
# ScriptName:  Huawei_Autoconfig.py
# Create Date: 2020-07-08 16:35
# Modify Date: 2020-07-08 16:35
#***************************************************************#
from netmiko import ConnectHandler,NetmikoTimeoutException,NetmikoAuthenticationException #引入netmiko连接模块、报错模块
import getpass #引入密码模块
import time #引入时间模块
date = time.strftime('%Y%m%d', time.localtime()) #赋予date变量
password = getpass.getpass('Password:') #赋予password变量host={'192.168.1.10',
'192.168.1.11',
}; #定义需要下发配置的主机 IPfor ip in host: #定义循环#创建字典huawei = {'device_type':"huawei", #定义设备类型'ip':ip, #调用变量ip'port':'22', #指定端口,默认为22'username':'admin', #设备登录名'password':password, #调用getpass模块#'secret' : 'admin' #enable密码}try:huawei_connect = ConnectHandler(**huawei) #传入设备字典与设备建立SSH连接。print ("Sucessfully Login to",ip)print ("Building configuration...")config = ['命令一','命令二','命令三'] #定义需要配置的命令input = huawei_connect.send_config_set(config) #执行命令print(input) #打印输出结果print(ip,'Was finished!\n',"-"*100)except NetmikoAuthenticationException : #认证失败报错记录e1 = open(f'{date}.txt','a')print(date,ip,'[Error 1] Authentication failed.\n',file = e1)e1.closeexcept NetmikoTimeoutException : #登录超时报错记录e2 = open(f'{date}.txt','a')print(date,ip,'[Error 2] Connection timed out.\n',file = e2)e2.closeexcept : #未知报错记录e3 = open(f'{date}.txt','a')print(date,ip,'[Error 3] Unknown error.\n',file = e3)e3.close
huawei_connect.disconnect() #断开SSH连接

执行结果与 Cisco 类似,就不再演示

参考链接 :

https://mp.weixin.qq.com/s/sYvK86dM2dgYtsyCQw8wAg

Python 网络自动化: 使用 Netmiko 模块批量下发华为、思科交换机配置相关推荐

  1. paramiko模块_玩转网络自动化之Netmiko模块

    我们知道,服务器的硬件和操作系统软件已经完全解耦,硬件和软件可以分开购买,并进行按需安装. 但是网络设备专用性强,硬件和操作系统软件高度耦合,即使相同厂商的设备,不同类型网络设备间也是拥有不同的CLI ...

  2. centos7自动化备份华为思科交换机配置

    1.安装tftp-server. xinetd.expect yum install tftp-server xinetd expect systemctl restart xinetd.servic ...

  3. 「Python 网络自动化」目录汇总

    目录 Netmiko NETCONF Nornir Paramiko Napalm NetBox TextFSM 其他 关于文章 关于我 Netmiko 「Python 网络自动化」Netmiko - ...

  4. 网络运维工具--shell批量下发

    网络运维工具–shell批量下发 此功能针对大批量设备下发相同命令比较实用,适配多个厂商,但多个厂商不能混用. 1.命令语法介绍: 1)采集命令一般无需处理,直接下发输入后下发即可.(实际为省略模式和 ...

  5. python备份cisco交换机_1.自动备份思科交换机配置

    自动备份思科交换机配置 2017-10-11 Python 宅必备 这个专题主要是一些日常用到的Python程序,不定期更新~~ 备份原理 首先读取txt文档中的ip地址 然后循环地址,通过是我提出函 ...

  6. 「Python 网络自动化」Nornir—— Inventory(主机清单)介绍

    Nornir 是一个非常好用的网络自动化的框架,最近我输出了一份 Nornir 中文手册,欢迎大家阅读指正. 主机清单 主机清单(Inventory) 是 nornir 最重要的部分,它由 hosts ...

  7. 华为 Python网络自动化

    哈喽,大家好!我是艺博东 ,是一个思科出身.专注于华为的网工:好了,话不多说,我们直接进入正题. 光棍二十年,不知道情人节是什么鬼东西.还是好好学技术吧!努力.奋斗吧!为了早日走向人生巅峰,迎娶白富美 ...

  8. python网络爬虫之requests模块

    什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的地位. 因为在使用ur ...

  9. python网络协议编辑器_python模块:网络协议和支持

    python模块:网络协议和支持 webbrowser 调用浏览器显示html文件 webbrowser.open('map.html') uuid/hmac/hashlib生成唯一ID 在有些情况下 ...

最新文章

  1. java保留两位小数_java使double保留两位小数的多方法 java保留两位小数
  2. WordCount案例
  3. LeetCode Text Justification(贪心)
  4. 在java中下列描述错误的是_在 JAVA 中 , 关于类的方法 , 下列描述错误的是 ()._学小易找答案...
  5. Spring MVC Hello World 例子
  6. CodeForces - 1311D Three Integers(暴力)
  7. [vue] vue的属性名称与method的方法名称一样时会发生什么问题?
  8. 【开源项目】EasyCmd命令图形化软件
  9. while0表示什么意思_什么是意思表示,有哪些形式?
  10. hnu暑期实训之日历问题
  11. linux怎么查看进程的套接字,linux – 如何找到哪个进程绑定套接字而不是监听?...
  12. 2016.01.18 UILabel
  13. argparse及logging的相关用法
  14. [转]如何有效地记忆与学习
  15. 1500个前端开发常用JavaScript特效
  16. 计算机丢失d3dx935.dll,d3dx943.dll丢失的解决方法
  17. UT单元测试(一)——基础流程总结篇
  18. 亚马逊英国站产品审核要求英国代理人英代办理英国授权代表英代DOC符合性声明
  19. Java面试-001
  20. 网络层(2.网际协议IP)

热门文章

  1. spring security4 cas4 整合 j_spring_cas_security_check 404
  2. 如何用python自动签到
  3. 汇川PLC和PLC之间ModebusTCP通讯
  4. linux解压gcc,Linux,GCC安装(解压版)
  5. AIX系统下修改SNMP默认连接串名public为其他字符串方法
  6. Java动态执行代码字符串
  7. CS61A Proj 2
  8. C++Primer第五版——习题答案+详解(完整版)
  9. VUE任何路径都能访问到主页面
  10. 新的教学模式、教学改革思路探讨