[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()