吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4206|回复: 40
收起左侧

[Python 转载] 第一个Python程序~自动备份交换机配置

   关闭 [复制链接]
xiaomingtt 发表于 2022-8-23 17:14
单位几十台交换机需要管理,于是决定学习一下Python,自动管理交换机。
第一个Python程序,自动备份交换机配置到文件。
使用netmiko模块。
[Python] 纯文本查看 复制代码
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog

import csv
from netmiko import ConnectHandler

sw=[]

class Win_l74e5b8m:
    def __init__(self):
        self.root = self.__win()
        self.tk_label_l74f3h6v = self.__tk_label_l74f3h6v()
        self.tk_input_l74f42i6 = self.__tk_input_l74f42i6()
        self.tk_button_l74f47vz = self.__tk_button_l74f47vz()
        self.tk_select_box_l74f5c9s = self.__tk_select_box_l74f5c9s()
        self.tk_button_l74f7boe = self.__tk_button_l74f7boe()
        self.tk_label_l75t60zj = self.__tk_label_l75t60zj()

    def __win(self):
        root = Tk()
        root.title("交换机自动备份")
        width = 420
        height = 200
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(geometry)
        root.resizable(width=False, height=False)
        return root

    def __tk_label_l74f3h6v(self):
        label = Label(self.root,text="交换机列表文件")
        label.place(x=0, y=20, width=120, height=24)
        return label

    def __tk_input_l74f42i6(self):
        ipt = Entry(self.root)
        ipt.place(x=130, y=20, width=200, height=24)
        return ipt

    def __tk_button_l74f47vz(self):
        btn = Button(self.root, text="选择文件", command=self.dissh)
        btn.place(x=340, y=20, width=80, height=24)
        return btn

    def __tk_select_box_l74f5c9s(self):
        cb = Combobox(self.root, state="readonly")
        cb.place(x=60, y=70, width=299, height=26)
        return cb

    def __tk_button_l74f7boe(self):
        btn = Button(self.root, text="开始备份", command=self.ksgan)
        btn.place(x=160, y=120, width=109, height=49)
        return btn
    def __tk_label_l75t60zj(self):
        label = Label(self.root,text="")
        label.place(x=0, y=170, width=419, height=25)
        return label
    def dissh(self):
        f_path=filedialog.askopenfilename(title='选择CSV文件', filetypes=[('CSV', '*.csv')])
        self.tk_input_l74f42i6.insert(END,f_path)
        if f_path:
            i=0
            comlist=[]
            with open(f_path, mode="r") as f:
                r = csv.reader(f)
                for row in r:
                    print(i," ",row[0],row[2])
                    c_list = '{}、{}_{}'.format(i,row[0],row[2])
                    comlist.append(c_list)
                    i=i+1
                    sw.append([row[2],row[4],row[5],row[8]])
            self.tk_select_box_l74f5c9s['values']=comlist
            self.tk_select_box_l74f5c9s.current(0)
    def ksgan(self):
        self.tk_button_l74f7boe.config(state = 'disabled')
        j = self.tk_select_box_l74f5c9s.current()
        print('选中的数据:{}'.format(self.tk_select_box_l74f5c9s.get()))
        print('选中的值:{}'.format(self.tk_select_box_l74f5c9s.current()))
        print(sw[j])
        pynet1 = {'device_type': sw[j][3],'ip': sw[j][0],'username': sw[j][1],'password': sw[j][2],'port':22,'timeout':180}
        with ConnectHandler(**pynet1) as connect:
            self.tk_label_l75t60zj['text']="成功登录到交换机:" + sw[j][0]
            print ("成功登录到交换机:" + sw[j][0])
            output = connect.send_command_timing('display current-configuration')
            print(output)
            self.tk_label_l75t60zj['text']="正在备份中" 
            file_name = '{}.txt'.format(pynet1['ip'])
            with open(file_name, mode='w', encoding='utf8') as f:
                f.write(output)
                self.tk_label_l75t60zj['text']="备份完成" 
                print('{}执行备份成功'.format(pynet1['ip']))
            connect.disconnect()
        self.tk_button_l74f7boe.config(state = 'normal')
def run():
    win = Win_l74e5b8m()
    win.root.mainloop()

if __name__ == "__main__":
    run()


2022-08-23_171105.png
交换机列表需要使用CSV文件导入,格式如下:
2022-08-23_171016.png
第一列:设备名,第二列:登录方式,目前仅支持SSH,第三列:IP地址,第四列:端口,第五列:用户名,第六列:密码,第九列:交换机类型,华三H3C用hp_comware,华为用huawei,第十列:备注

免费评分

参与人数 5吾爱币 +4 热心值 +3 收起 理由
maker001 + 1 netmiko是支持telnet的,只需要将huawei改成huawei_telnet就行了,其他品牌.
不是浮夸 + 1 + 1 我很赞同!
bladexd88 + 1 谢谢@Thanks!
jefflo + 1 谢谢@Thanks!
mengqiantang + 1 + 1 大佬,这是软硬通吃啊

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

maker001 发表于 2023-5-24 15:44
如果有需要我能分享一个自动备份支持多品牌多登录方式“ssh、telnet”以及telnet单密码登录,且和头一天配置文件对比的脚本。
braxiong 发表于 2022-8-23 17:24
牛,但是只支持华三华为吗,几十台交换机也挺厉害的,我这3层写字楼有线+无线20多台交换机,有统控平台,webui比较方便
a397555462 发表于 2022-8-23 17:32
vethenc 发表于 2022-8-23 17:38
感谢分享,实用至上。
homehome 发表于 2022-8-23 17:39
接着,请用Python做一个交换机的流量监测图
紅樓夢遺 发表于 2022-8-23 17:48
telnet支持一下
ideasoft 发表于 2022-8-23 17:53
锐捷的可支持不?
Hacking2heart 发表于 2022-8-23 17:54
感谢分享,比较实用
choujie1689 发表于 2022-8-23 18:09
这一句代码什么意思,没看出来
win.root.mainloop()
haobazhs 发表于 2022-8-23 18:15
感谢分享,比较实用
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-11 18:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表