python与思科交换机交互提取交换机信息

8,050次阅读
没有评论

共计 1035 个字符,预计需要花费 3 分钟才能阅读完成。

上一篇博文写了关于提取思科交换机并分析处理的 python 脚本. 我们也还可以用 python 只提取交换机的配置用于备份交换机.

把上篇中的脚本做了一些简化, 只提取信息并保存, 每条命令保存为一个文件.

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Date:2018/4/27
#Created by Winters
#Blog:https://www.8win.net
from netmiko import ConnectHandler
import time
# 定义单次循环内容
def cisco(ip):
    " 思科交换机配置导出 "
    cisco_switch = {
        'device_type': 'cisco_ios', #设备类型
        'ip': ip, #设备 IP
        'username': 'user', #登陆用户名
        'password': 'password', #登陆密码
        'port': 22, #登陆端口, 默认 22
        'secret': 'enable', #enable 密码
        'verbose': False,# 是否详细报告, 默认否
    }
    net_connect = ConnectHandler(**cisco_switch) #连接到交换机
    net_connect.enable() #进入特权模式
    #交换机需要执行的命令
    commands = [
        'show interfaces',
        'show arp',
        'show run',
    ]
    #处理获取的命令结果,并保存为 txt 文件
    timestr = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    for cmd in commands:
        filename = u'%s_%s_%s.txt' % (ip, cmd.replace(' ', '_'), timestr)
        save = open(filename, 'w')
        result = net_connect.send_command(cmd)
        save.write(result)
    net_connect.disconnect()
if __name__ == '__main__':
    ips = [
        '192.168.0.1', #交换机 1IP 地址
        '192.168.0.2', #交换机 2IP 地址
    ]
    for ip in ips:
        cisco(ip)

正文完
 
Winter
版权声明:本站原创文章,由 Winter 2018-04-27发表,共计1035字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码