@ -1,143 +0,0 @@
|
||||
# 导入模块
|
||||
import pymysql
|
||||
|
||||
|
||||
# 连接本地数据库
|
||||
db = pymysql.connect(
|
||||
host='localhost', # 例:127.0.0.1 本地数据库地址
|
||||
port=3306, # 端口号
|
||||
user='root', # 用户id
|
||||
passwd='123456', # 用户密码
|
||||
db='demodb' # 连接的数据库
|
||||
)
|
||||
cursor = db.cursor()
|
||||
# print(cursor)
|
||||
|
||||
|
||||
|
||||
# 设置查找simhost表的函数
|
||||
def get_simhost_alldata():
|
||||
# 获取simhost数据库中所有的数据
|
||||
sql = '''
|
||||
select * from simhost
|
||||
'''
|
||||
cursor.execute(sql)
|
||||
# 获取全部数据
|
||||
test = cursor.fetchall()
|
||||
# test = pd.read_sql(sql,db)
|
||||
host_data_all = []
|
||||
for i in test:
|
||||
# 在这里对数据进行整理...
|
||||
ID = i[0]
|
||||
ObjID = i[1]
|
||||
ObjType = i[2]
|
||||
ObjLable = i[3]
|
||||
IPAddr = i[4]
|
||||
ObjX = i[5]
|
||||
ObjY = i[6]
|
||||
MACAddr = i[7]
|
||||
ObjShow = i[8]
|
||||
host_data = {
|
||||
'ID': ID,
|
||||
'ObjID': ObjID,
|
||||
'ObjType': ObjType,
|
||||
'ObjLable': ObjLable,
|
||||
'IPAddr': IPAddr,
|
||||
'ObjX': ObjX,
|
||||
'ObjY': ObjY,
|
||||
'MACAddr': MACAddr,
|
||||
'ObjShow': ObjShow
|
||||
}
|
||||
host_data_all.append(host_data)
|
||||
# print(host_data)
|
||||
# print(test) # 错误
|
||||
# cursor.close() # 关闭游标
|
||||
# db.close() # 关闭数据库连接
|
||||
return host_data_all
|
||||
|
||||
# 建立条件查询将ObjShow==1的值全部获取
|
||||
def Select_ObjShow():
|
||||
sql = '''select * from simhost where ObjShow=1'''
|
||||
cursor.execute(sql)
|
||||
objshow_select = cursor.fetchall()
|
||||
# print(objshow_select)
|
||||
objshow_select_all = []
|
||||
for i in objshow_select:
|
||||
# 在这里对数据进行整理...
|
||||
ID = i[0]
|
||||
ObjID = i[1]
|
||||
ObjType = i[2]
|
||||
ObjLable = i[3]
|
||||
IPAddr = i[4]
|
||||
ObjX = i[5]
|
||||
ObjY = i[6]
|
||||
MACAddr = i[7]
|
||||
ObjShow = i[8]
|
||||
host_data = {
|
||||
'ID': ID,
|
||||
'ObjID': ObjID,
|
||||
'ObjType': ObjType,
|
||||
'ObjLable': ObjLable,
|
||||
'IPAddr': IPAddr,
|
||||
'ObjX': ObjX,
|
||||
'ObjY': ObjY,
|
||||
'MACAddr': MACAddr,
|
||||
'ObjShow': ObjShow
|
||||
}
|
||||
objshow_select_all.append(host_data)
|
||||
# print(objshow_select_all)
|
||||
return objshow_select_all
|
||||
|
||||
# 设置联合查询ipAddr的地址
|
||||
def select_and_ip(id):
|
||||
data_all = []
|
||||
sql = '''select ipaddr.ipaddr from ipaddr inner join simhost on ipaddr.sim_id = simhost.id where simhost.id=%s'''
|
||||
cursor.execute(sql,id)
|
||||
data = cursor.fetchall()
|
||||
for item in data:
|
||||
# print(item[0])
|
||||
data_all.append(item[0])
|
||||
|
||||
# print(data_all)
|
||||
return data_all
|
||||
|
||||
|
||||
# 创建查询ObjLable列的函数
|
||||
def select_ObjLable():
|
||||
data_all = []
|
||||
sql = '''
|
||||
select objlable from simhost
|
||||
'''
|
||||
cursor.execute(sql)
|
||||
test = cursor.fetchall()
|
||||
for item in test:
|
||||
data_all.append(item[0])
|
||||
# print(data_all)
|
||||
return data_all
|
||||
|
||||
|
||||
# 在这个位置添加一个修改objshow列的数据函数
|
||||
def reve_objshow():
|
||||
# 将所有的对象全转换为objshow = 0
|
||||
sql = '''update simhost set objshow = 0 where objshow = 1'''
|
||||
cursor.execute(sql)
|
||||
db.commit()
|
||||
return None
|
||||
|
||||
|
||||
def reve_after(after_data):
|
||||
# 将获取的数据进行循环
|
||||
for item in after_data:
|
||||
sql = '''update simhost set objshow = 1 where objlable = %s'''
|
||||
cursor.execute(sql, item)
|
||||
db.commit()
|
||||
return None
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# print(get_simhost_alldata())
|
||||
# print(select_ObjLable())
|
||||
reve_objshow()
|
||||
after_data = ['A','R1','B']
|
||||
reve_after(after_data)
|
@ -1,197 +0,0 @@
|
||||
# 尝试用sqlite3数据库试试
|
||||
import sqlite3
|
||||
conn = sqlite3.connect('internet.db')
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 设置创建数据库的函数
|
||||
def create_simhost():
|
||||
sql = '''DROP TABLE IF EXISTS `simhost`'''
|
||||
cursor.execute(sql)
|
||||
sql_simhost = '''CREATE TABLE simhost
|
||||
(
|
||||
id integer primary key ,
|
||||
ObjID varchar(20),
|
||||
ObjType int,
|
||||
Objlable varchar(20),
|
||||
IPAddr varchar(64),
|
||||
ObjX int,
|
||||
ObjY int,
|
||||
MACAddr varchar(64),
|
||||
ObjShow int
|
||||
);'''
|
||||
cursor.execute(sql_simhost)
|
||||
return None
|
||||
|
||||
#建立数据表ipaddr的函数
|
||||
def create_ipaddr():
|
||||
sql = '''DROP TABLE IF EXISTS `ipaddr`'''
|
||||
cursor.execute(sql)
|
||||
sql_ipaddr = '''CREATE TABLE ipaddr
|
||||
(
|
||||
id integer primary key ,
|
||||
ipaddr varchar(25),
|
||||
sim_id int,
|
||||
foreign key (sim_id) references simhost(id)
|
||||
);'''
|
||||
cursor.execute(sql_ipaddr)
|
||||
return None
|
||||
|
||||
# 设置两个插入数据函数
|
||||
def insert_simhost():
|
||||
# 需要插入的数据
|
||||
data_simhost = [
|
||||
(1, 'SH01', 1, 'A', '101.11.1.2', 170, 120, 'MAC01', 1),
|
||||
(2, 'SR01', 2, 'R1', '102.10.1.1', 250, 160, 'MAC02', 1),
|
||||
(3, 'SR02', 2, 'R2', '102.10.5.1', 370, 130, 'MAC03', 1),
|
||||
(4, 'SR03', 2, 'R4', '102.10.6.1', 400, 200, 'MAC04', 1),
|
||||
(5, 'SR04', 2, 'R5', '111.12.1.1', 480, 260, 'MAC05', 1),
|
||||
(6, 'SH02', 1, 'D', '201.21.1.2', 420, 330, 'MAC06', 1),
|
||||
(7, 'SHO3', 1, 'B', '101.11.1.8', 250, 240, 'MAC07', 0),
|
||||
(8, 'SR05', 2, 'R3', '102.10.6.2', 520, 160, 'MAC08', 0),
|
||||
(9, 'SH04', 1, 'C', '201.21.1.5', 540, 320, 'MAC09', 0),
|
||||
]
|
||||
sql = '''INSERT INTO simhost (id,ObjID,ObjType,Objlable,IPAddr,ObjX,ObjY,MACAddr,ObjShow)
|
||||
VALUES (?,?,?,?,?,?,?,?,?)'''
|
||||
cursor.executemany(sql,data_simhost)
|
||||
conn.commit()
|
||||
return None
|
||||
|
||||
def insert_ipaddr():
|
||||
# 需要插入的数据
|
||||
data_ipaddr = [
|
||||
(1, '101.11.1.1', 1),
|
||||
(2, '102.10.1.1', 2),
|
||||
(3, '102.10.5.1', 3),
|
||||
(4, '111.12.5.1', 4),
|
||||
(5, '201.21.1.1', 5),
|
||||
]
|
||||
sql = '''INSERT INTO ipaddr (id,ipaddr,sim_id) VALUES (?,?,?)'''
|
||||
cursor.executemany(sql,data_ipaddr)
|
||||
conn.commit()
|
||||
return None
|
||||
|
||||
# 设置获取数据库中所有数据的函数
|
||||
def get_simhost_alldata():
|
||||
sql = '''SELECT * FROM simhost'''
|
||||
cursor.execute(sql)
|
||||
data = cursor.fetchall()
|
||||
# print(data)
|
||||
all_data = []
|
||||
for item in data:
|
||||
ID = item[0]; ObjID = item[1]; ObjType = item[2]
|
||||
ObjLable = item[3]; IPAddr = item[4]; ObjX = item[5]
|
||||
ObjY = item[6]; MACAddr = item[7]; ObjShow = item[8]
|
||||
host_data = {
|
||||
'ID': ID,'ObjID': ObjID,'ObjType': ObjType,
|
||||
'ObjLable': ObjLable,'IPAddr': IPAddr,'ObjX': ObjX,
|
||||
'ObjY': ObjY,'MACAddr': MACAddr,'ObjShow': ObjShow
|
||||
}
|
||||
all_data.append(host_data)
|
||||
return all_data
|
||||
|
||||
# 建立条件查询将ObjShow==1的值全部取出
|
||||
def Select_ObjShow():
|
||||
sql = '''SELECT * FROM simhost WHERE ObjShow=1'''
|
||||
cursor.execute(sql)
|
||||
objshow_data = cursor.fetchall()
|
||||
# print(objshow_data)
|
||||
objshow_data_all = []
|
||||
for item in objshow_data:
|
||||
ID = item[0]; ObjID = item[1]; ObjType = item[2]
|
||||
ObjLable = item[3]; IPAddr = item[4]; ObjX = item[5]
|
||||
ObjY = item[6]; MACAddr = item[7]; ObjShow = item[8]
|
||||
host_data = {
|
||||
'ID': ID,'ObjID': ObjID,'ObjType': ObjType,
|
||||
'ObjLable': ObjLable,'IPAddr': IPAddr,'ObjX': ObjX,
|
||||
'ObjY': ObjY,'MACAddr': MACAddr,'ObjShow': ObjShow
|
||||
}
|
||||
objshow_data_all.append(host_data)
|
||||
return objshow_data_all
|
||||
|
||||
# 建立联合查询ipaddr的地址
|
||||
def select_and_ip(id):
|
||||
data_all = []
|
||||
sql = '''SELECT ipaddr.ipaddr FROM ipaddr INNER JOIN simhost ON ipaddr.sim_id = simhost.id WHERE simhost.id=?'''
|
||||
cursor.execute(sql, (id,))
|
||||
data = cursor.fetchall()
|
||||
for item in data:
|
||||
# print(item[0])
|
||||
data_all.append(item[0])
|
||||
return data_all
|
||||
|
||||
# 建立查询ObjLable列的函数
|
||||
def select_ObjLable():
|
||||
data_all = []
|
||||
sql = '''SELECT objlable FROM simhost'''
|
||||
cursor.execute(sql)
|
||||
data = cursor.fetchall()
|
||||
for item in data:
|
||||
data_all.append(item[0])
|
||||
# print(data_all)
|
||||
return data_all
|
||||
|
||||
# 建立这个位置添加一个修改objshow列的数据函数
|
||||
def reve_objshow():
|
||||
sql = '''UPDATE simhost SET objshow=0 WHERE objshow=1'''
|
||||
cursor.execute(sql)
|
||||
conn.commit()
|
||||
return None
|
||||
|
||||
# 建立一个修改指定数据的函数objshow
|
||||
def reve_after(after_data):
|
||||
# 将获取的数据进行循环
|
||||
for item in after_data:
|
||||
sql = '''UPDATE simhost SET objshow=1 WHERE objlable=?'''
|
||||
cursor.execute(sql, (item,))
|
||||
conn.commit()
|
||||
return None
|
||||
|
||||
|
||||
# 建立一个通过查询ObjLable并返回ObjID的函数
|
||||
def select_ObjLable_ObjID(objlable):
|
||||
sql = '''SELECT objx,objy,objid,objshow FROM simhost WHERE objlable=?'''
|
||||
cursor.execute(sql, (objlable,))
|
||||
old_data = cursor.fetchall()
|
||||
new_data = old_data[0]
|
||||
# print(new_data)
|
||||
return new_data
|
||||
|
||||
# 建立函数通过指定的ObjID来查找所有的数据
|
||||
def Select_ObjID(array):
|
||||
array_all_data = [] # 用来获取所有的对应的数据并放入其中
|
||||
sql = '''SELECT * FROM simhost WHERE objlable=?'''
|
||||
for item in array:
|
||||
cursor.execute(sql, (item,))
|
||||
data = cursor.fetchall()
|
||||
# print(data)
|
||||
ID = data[0][0];ObjID = data[0][1];ObjType = data[0][2]
|
||||
ObjLable = data[0][3];IPAddr = data[0][4];ObjX = data[0][5]
|
||||
ObjY = data[0][6];MACAddr = data[0][7];ObjShow = data[0][8]
|
||||
host_data = {
|
||||
'ID': ID, 'ObjID': ObjID, 'ObjType': ObjType,
|
||||
'ObjLable': ObjLable, 'IPAddr': IPAddr, 'ObjX': ObjX,
|
||||
'ObjY': ObjY, 'MACAddr': MACAddr, 'ObjShow': ObjShow
|
||||
}
|
||||
array_all_data.append(host_data)
|
||||
return array_all_data
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# create_simhost()
|
||||
# insert_simhost()
|
||||
# create_ipaddr()
|
||||
# insert_ipaddr()
|
||||
# get_data_sinhost()
|
||||
# print(get_simhost_alldata())
|
||||
# print(Select_ObjShow())
|
||||
# print(select_and_ip(1))
|
||||
# print(select_ObjLable())
|
||||
# reve_objshow()
|
||||
# after_data = ['A', 'R1', 'B']
|
||||
# reve_after(after_data)
|
||||
# select_ObjLable_ObjID('A')
|
||||
# array = ['A','B']
|
||||
# print(Select_ObjID(array))
|
||||
pass
|
||||
|
@ -1,117 +0,0 @@
|
||||
# 在这个文件里画出详情
|
||||
|
||||
|
||||
# 创建函数用来画出详情
|
||||
import time
|
||||
|
||||
|
||||
class Detail_Show():
|
||||
def __init__(self, cv, img_detail, move_data):
|
||||
self.cv = cv
|
||||
self.move_data = move_data
|
||||
self.img_detail = img_detail
|
||||
|
||||
def show_detail(self):
|
||||
# time.sleep(5)
|
||||
self.detail_data_all = []
|
||||
# 通过获取的move_data数据来画出详情表
|
||||
for item in self.move_data:
|
||||
detail_data_x = item[0]+20
|
||||
detail_data_y = item[1]
|
||||
detail_type = item[3]
|
||||
detail_data = (detail_data_x,detail_data_y,detail_type)
|
||||
self.detail_data_all.append(detail_data)
|
||||
self.cv.create_image(detail_data_x,detail_data_y, image=self.img_detail, tags='detail') # 完成
|
||||
|
||||
return self.detail_data_all
|
||||
|
||||
|
||||
# 建立一个用来画出详情的类
|
||||
|
||||
class Show_green_detail():
|
||||
def __init__(self, cv, item):
|
||||
self.cv = cv
|
||||
self.item = item
|
||||
|
||||
# 建立画出对象详情的函数
|
||||
def draw_detail(self):
|
||||
# 判断type对象来画出详情表
|
||||
if self.item[2] == 1: # 当type对象为1 画出主机详情图
|
||||
# 在这里画出详情图
|
||||
frist_x = self.item[0] # 获取frist_x
|
||||
frist_y = self.item[1] # 获取frist_y
|
||||
# 这里缺少一个删除其他详情的步骤
|
||||
# 画出连接详情表的线
|
||||
self.cv.delete('line')
|
||||
self.cv.create_line((frist_x,frist_y),(frist_x+40,frist_y),(frist_x+45,frist_y-30),fill='#94D050',tags='line')
|
||||
# 画出详情表
|
||||
self.detail_true = self.cv.create_rectangle(frist_x+30,frist_y-120,frist_x+180,frist_y-30,outline='#94D050',tags='line')
|
||||
# 画出相应的绿条数据
|
||||
self.cv.create_text(frist_x+50,frist_y-110,text='应用层',tags='line')
|
||||
self.cv.create_text(frist_x+50,frist_y-87,text='传输层',tags='line')
|
||||
self.cv.create_text(frist_x+50,frist_y-64,text='IP 层',tags='line')
|
||||
self.cv.create_text(frist_x+50,frist_y-42,text='链路层',tags='line')
|
||||
# 画出 右侧绿色和蓝色的类进度条
|
||||
# 应用层
|
||||
self.cv.create_rectangle(frist_x+177,frist_y-115,frist_x+150,frist_y-100,fill='#94D050',outline='#94D050',tags='line')
|
||||
self.cv.create_rectangle(frist_x+148,frist_y-115,frist_x+128,frist_y-100,fill='#00B0F0',outline='#00B0F0',tags='line')
|
||||
# 传输层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 95, frist_x + 140, frist_y - 80, fill='#94D050',
|
||||
outline='#94D050', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 138, frist_y - 95, frist_x + 113, frist_y - 80, fill='#00B0F0',
|
||||
outline='#00B0F0', tags='line')
|
||||
# IP 层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 73, frist_x + 130, frist_y - 58, fill='#94D050',
|
||||
outline='#94D050', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 128, frist_y - 73, frist_x + 98, frist_y - 58, fill='#00B0F0',
|
||||
outline='#00B0F0', tags='line')
|
||||
# 传输层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 50, frist_x + 113, frist_y - 35, fill='#94D050',
|
||||
outline='#94D050', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 111, frist_y - 50, frist_x + 78, frist_y - 35, fill='#00B0F0',
|
||||
outline='#00B0F0', tags='line')
|
||||
elif self.item[2] == 2: # 当type对象为2 画出路由器详情图
|
||||
# print(2)
|
||||
frist_x = self.item[0] # 获取frist_x
|
||||
frist_y = self.item[1] # 获取frist_y
|
||||
# 这里缺少一个删除其他详情的步骤
|
||||
# 画出连接详情表的线
|
||||
self.cv.delete('line')
|
||||
self.cv.create_line((frist_x, frist_y), (frist_x + 40, frist_y), (frist_x + 45, frist_y - 30),
|
||||
fill='#94D050', tags='line')
|
||||
# 画出详情表 如果需要全覆盖则需要添加一个fill
|
||||
self.detail_true = self.cv.create_rectangle(frist_x + 30, frist_y - 120, frist_x + 180, frist_y - 30,
|
||||
outline='#94D050', tags='line')
|
||||
# 画出相应的绿条数据
|
||||
self.cv.create_text(frist_x + 50, frist_y - 110, text='应用层', tags='line')
|
||||
self.cv.create_text(frist_x + 50, frist_y - 87, text='传输层', tags='line')
|
||||
self.cv.create_text(frist_x + 50, frist_y - 64, text='IP 层', tags='line')
|
||||
self.cv.create_text(frist_x + 50, frist_y - 42, text='链路层', tags='line')
|
||||
# 画出 右侧绿色和蓝色的类进度条
|
||||
# 应用层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 115, frist_x + 150, frist_y - 100, fill='#D3D3D3',
|
||||
outline='#D3D3D3', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 148, frist_y - 115, frist_x + 128, frist_y - 100, fill='#D3D3D3',
|
||||
outline='#D3D3D3', tags='line')
|
||||
# 传输层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 95, frist_x + 140, frist_y - 80, fill='#D3D3D3',
|
||||
outline='#D3D3D3', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 138, frist_y - 95, frist_x + 113, frist_y - 80, fill='#D3D3D3',
|
||||
outline='#D3D3D3', tags='line')
|
||||
# IP 层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 73, frist_x + 130, frist_y - 58, fill='#94D050',
|
||||
outline='#94D050', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 128, frist_y - 73, frist_x + 98, frist_y - 58, fill='#00B0F0',
|
||||
outline='#00B0F0', tags='line')
|
||||
# 传输层
|
||||
self.cv.create_rectangle(frist_x + 177, frist_y - 50, frist_x + 113, frist_y - 35, fill='#94D050',
|
||||
outline='#94D050', tags='line')
|
||||
self.cv.create_rectangle(frist_x + 111, frist_y - 50, frist_x + 78, frist_y - 35, fill='#00B0F0',
|
||||
outline='#00B0F0', tags='line')
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
@ -1,70 +0,0 @@
|
||||
# 导入函数
|
||||
from data_crud_sqlite import *
|
||||
|
||||
|
||||
'''创建类来处理函数'''
|
||||
class Show_Right_All():
|
||||
def __init__(self,cv,all_data):
|
||||
self.cv = cv
|
||||
self.all_data = all_data
|
||||
|
||||
|
||||
# 创建函数 来处理获取的数据
|
||||
def Show_img(self,img_host,img_router):
|
||||
self.loca_all = [] # 定义一个数组用来存数据
|
||||
# 将获取的所有数据处理
|
||||
for item in self.all_data:
|
||||
ObjX = item['ObjX']
|
||||
ObjY = item['ObjY']
|
||||
ObjID = item['ObjID']
|
||||
loca = (ObjX, ObjY)
|
||||
self.loca_all.append(loca)
|
||||
# 显示图片
|
||||
if item['ObjType'] == 1:
|
||||
self.cv.create_image(ObjX, ObjY, image=img_host, tags=ObjID)
|
||||
# elif item['ObjType'] == 2:
|
||||
else:
|
||||
self.cv.create_image(ObjX, ObjY, image=img_router, tags=ObjID)
|
||||
# 显示ObjLable
|
||||
if item['ObjLable'] == 'A':
|
||||
self.cv.create_text(ObjX, ObjY+27, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX, ObjY-32, text=item['IPAddr'], font=('蔚然雅黑',10,'bold'),fill='#C00000', tags=item['IPAddr'])
|
||||
elif item['ObjLable'] == 'R1':
|
||||
self.cv.create_text(ObjX+15, ObjY+25, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX-67, ObjY, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=item['IPAddr'])
|
||||
# 通过联表查询获取IP地址
|
||||
ip_data_R1 = select_and_ip(2)
|
||||
# print(ip_data_R1)
|
||||
self.cv.create_text(ObjX+30, ObjY-30, text= ip_data_R1, font=('蔚然雅黑',10,'bold'),fill='#C00000', tags=ip_data_R1)
|
||||
elif item['ObjLable'] == 'R2':
|
||||
self.cv.create_text(ObjX-17, ObjY+25, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX, ObjY - 32, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=item['IPAddr'])
|
||||
elif item['ObjLable'] == 'R4':
|
||||
self.cv.create_text(ObjX-35, ObjY, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX+20, ObjY - 29, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=item['IPAddr'])
|
||||
ip_data_R4 = select_and_ip(4)
|
||||
self.cv.create_text(ObjX - 10, ObjY + 26, text=ip_data_R4, font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=ip_data_R4)
|
||||
elif item['ObjLable'] == 'R5':
|
||||
self.cv.create_text(ObjX, ObjY+27, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX-67, ObjY - 12, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=item['IPAddr'])
|
||||
ip_data_R5 = select_and_ip(5)
|
||||
self.cv.create_text(ObjX - 67, ObjY + 3, text=ip_data_R5, font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=ip_data_R5)
|
||||
elif item['ObjLable'] == 'D':
|
||||
self.cv.create_text(ObjX-31, ObjY+10, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX, ObjY + 27, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=item['IPAddr'])
|
||||
elif item['ObjLable'] == 'C':
|
||||
self.cv.create_text(ObjX+25, ObjY-27, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
self.cv.create_text(ObjX, ObjY + 27, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000',
|
||||
tags=item['IPAddr'])
|
||||
elif item['ObjLable'] == 'R3':
|
||||
self.cv.create_text(ObjX, ObjY-30, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
elif item['ObjLable'] == 'B':
|
||||
self.cv.create_text(ObjX+30, ObjY+10, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable'])
|
||||
return None
|
@ -1,76 +0,0 @@
|
||||
|
||||
|
||||
# 创建一个画线的类
|
||||
class Line_Right():
|
||||
def __init__(self, cv, line_data):
|
||||
self.cv = cv
|
||||
self.line_data = line_data
|
||||
|
||||
# 创建函数来画出需要的线
|
||||
def line_test(self):
|
||||
# 将数据通过循环取出,使用下标的形式
|
||||
for i in range(0, len(self.line_data)):
|
||||
'''需要进行判断下标i+1存在数据'''
|
||||
if i + 1 != len(self.line_data):
|
||||
x = self.line_data[i]['ObjX']
|
||||
y = self.line_data[i]['ObjY']
|
||||
next_x = self.line_data[i + 1]['ObjX']
|
||||
next_y = self.line_data[i + 1]['ObjY']
|
||||
self.cv.create_line(x, y, next_x, next_y, width=3, fill='#5B9BD5')
|
||||
# print(i)
|
||||
return None
|
||||
|
||||
|
||||
# 创建一个用于画出永久固定的无用的线的类
|
||||
class Useless_line():
|
||||
def __init__(self, cv, Useless_line_data):
|
||||
self.cv = cv
|
||||
self.Useless_line_data = Useless_line_data
|
||||
|
||||
|
||||
# 创建函数用来画出线
|
||||
def draw_line_less(self):
|
||||
data_all_zero = self.data_handle()
|
||||
# 通过循环判断获取的对象是谁,在给该对象画出线条
|
||||
for item in data_all_zero:
|
||||
# 在这里添加判断
|
||||
if item['ID'] == 7: # 这个以后换一个判断信息
|
||||
# 在这里画出线
|
||||
self.cv.create_line(item['ObjX'],item['ObjY'],250,160,width=3, fill='#00B050')
|
||||
if item['ID'] == 8:
|
||||
# 在这里画出连接三条线
|
||||
self.cv.create_line(item['ObjX'],item['ObjY'],370,130,width=1, fill='#5B9BD5')
|
||||
self.cv.create_line(item['ObjX'], item['ObjY'], 400, 200, width=1, fill='#5B9BD5')
|
||||
self.cv.create_line(item['ObjX'], item['ObjY'], 480, 260, width=1, fill='#5B9BD5')
|
||||
if item['ID'] == 9:
|
||||
self.cv.create_line(item['ObjX'], item['ObjY'], 480, 260, width=2, fill='#5B9BD5')
|
||||
return None
|
||||
|
||||
# 建立函数用来处理获取的数据
|
||||
def data_handle(self):
|
||||
data_ObjShow_zero_all = []
|
||||
for item in self.Useless_line_data:
|
||||
# print(item)
|
||||
if item['ObjShow'] == 0:
|
||||
# print(item)
|
||||
data_ObjShow_zero_all.append(item)
|
||||
# print(data_ObjShow_zero_all)
|
||||
return data_ObjShow_zero_all
|
||||
|
||||
|
||||
|
||||
# 对获取的line_data进行处理
|
||||
def line_draw(ShowData):
|
||||
line_data_all = [] # 定义一个数组用来存储所有数据
|
||||
# 将获取的数据循环获取需要的数据
|
||||
for item in ShowData:
|
||||
# print(item)
|
||||
ObjX = item['ObjX']
|
||||
ObjY = item['ObjY']
|
||||
line_data = {
|
||||
'ObjX': ObjX,
|
||||
'ObjY': ObjY
|
||||
}
|
||||
line_data_all.append(line_data)
|
||||
# print(line_data_all)
|
||||
return line_data_all
|
@ -1,189 +0,0 @@
|
||||
# 这个函数用来实现模拟解包过程
|
||||
|
||||
|
||||
# 创建类用来处理解包函数
|
||||
class Unpack_All():
|
||||
# 获取需要的数据
|
||||
def __init__(self,MacPacketString, IPPacketString, TransPacketString, AppPacketString):
|
||||
self.MacPacketString = MacPacketString
|
||||
self.IPPacketString = IPPacketString
|
||||
self.TransPacketString = TransPacketString
|
||||
self.AppPacketString = AppPacketString
|
||||
|
||||
# 建立函数用来处理MacPacketString中的数据
|
||||
def unpack_mac_data(self):
|
||||
# print(self.MacPacketString)
|
||||
MacData_All = []
|
||||
# 将获取的数据分解成macdata中
|
||||
for item in self.MacPacketString:
|
||||
ObjID = item['ObjID']
|
||||
ObjLable = item['ObjLable']
|
||||
MacPackId = item['MacPackId']
|
||||
IPPackID = item['IPPackID']
|
||||
sentmac = item['sentmac']
|
||||
rcptmac = item['rcptmac']
|
||||
sourceip = item['sourceip']
|
||||
targetip = item['targetip']
|
||||
sourceAppAddr = item['sourceAppAddr']
|
||||
targetAppAddr = item['targetAppAddr']
|
||||
MacData = {
|
||||
'ObjID': ObjID,
|
||||
'ObjLable': ObjLable,
|
||||
'MacPackId': MacPackId,
|
||||
'IPPackID': IPPackID,
|
||||
'sentmac': sentmac,
|
||||
'rcptmac': rcptmac,
|
||||
'sourceip': sourceip,
|
||||
'targetip': targetip,
|
||||
'sourceAppAddr': sourceAppAddr,
|
||||
'targetAppAddr': targetAppAddr,
|
||||
'MacPacketString': item
|
||||
}
|
||||
MacData_All.append(MacData)
|
||||
return MacData_All
|
||||
|
||||
# 建立函数用来处理IPPacket中的数据
|
||||
def unpack_ip_data(self):
|
||||
IPData_All = []
|
||||
for item in self.IPPacketString:
|
||||
ObjID = item['ObjID']
|
||||
ObjLable = item['ObjLable']
|
||||
IPPackID = item['IPPackID']
|
||||
TransPackID = item['TransPackID']
|
||||
sourceip = item['sourceip']
|
||||
targetip = item['targetip']
|
||||
sourceAppAddr = item['sourceAppAddr']
|
||||
targetAppAddr = item['targetAppAddr']
|
||||
IPData = {
|
||||
'ObjID': ObjID,
|
||||
'ObjLable': ObjLable,
|
||||
'IPPackID': IPPackID,
|
||||
'TransPackID': TransPackID,
|
||||
'sourceip': sourceip,
|
||||
'targetip': targetip,
|
||||
'sourceAppAddr': sourceAppAddr,
|
||||
'targetAppAddr': targetAppAddr,
|
||||
'IPPacketString': item
|
||||
}
|
||||
IPData_All.append(IPData)
|
||||
return IPData_All
|
||||
|
||||
|
||||
# 建立一个函数用来处理Trans的解包
|
||||
def unpack_trans_data(self):
|
||||
TransData_All = []
|
||||
for item in self.TransPacketString:
|
||||
ObjID = item['ObjID']
|
||||
ObjLable = item['ObjLable']
|
||||
TransPackID = item['TransPackID']
|
||||
TransSeq = item['TransSeq']
|
||||
TransTag = item['TransTag']
|
||||
AppPackId = item['AppPackId']
|
||||
sentport = item['sentport']
|
||||
rpctport = item['rpctport']
|
||||
sourceAppAddr = item['sourceAppAddr']
|
||||
targetAppAddr = item['targetAppAddr']
|
||||
TransData = {
|
||||
'ObjID': ObjID,
|
||||
'ObjLable': ObjLable,
|
||||
'TransPackID': TransPackID,
|
||||
'TransSeq': TransSeq,
|
||||
'TransTag': TransTag,
|
||||
'AppPackId': AppPackId,
|
||||
'sentport': sentport,
|
||||
'rpctport': rpctport,
|
||||
'sourceAppAddr': sourceAppAddr,
|
||||
'targetAppAddr': targetAppAddr,
|
||||
'TransPacketString': item
|
||||
}
|
||||
TransData_All.append(TransData)
|
||||
return TransData_All
|
||||
|
||||
# 建立一个函数用来处理App的解包
|
||||
def unpack_app_data(self):
|
||||
ObjID = self.AppPacketString['ObjID']
|
||||
ObjLable = self.AppPacketString['ObjLable']
|
||||
AppPackId = self.AppPacketString['AppPackId']
|
||||
AppPackSize = self.AppPacketString['AppPackSize']
|
||||
AppPackTag = self.AppPacketString['AppPackTag']
|
||||
sourceAppAddr = self.AppPacketString['sourceAppAddr']
|
||||
targetAppAddr = self.AppPacketString['targetAppAddr']
|
||||
AppData = {
|
||||
'ObjID': ObjID,
|
||||
'ObjLable': ObjLable,
|
||||
'AppPackId': AppPackId,
|
||||
'AppPackSize': AppPackSize,
|
||||
'AppPackTag': AppPackTag,
|
||||
'sourceAppAddr': sourceAppAddr,
|
||||
'targetAppAddr': targetAppAddr,
|
||||
'AppPacketString': self.AppPacketString
|
||||
}
|
||||
# print(AppData)
|
||||
return AppData
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
MacPacketString = [
|
||||
{'ObjID': 'SHO1',
|
||||
'ObjLable': 'A',
|
||||
'MAC_number': 1,
|
||||
'MacPackId': 'MacPackID1',
|
||||
'IPPackID': 'IPPack1',
|
||||
'sentmac': '00:1a:8c:10:ad:30',
|
||||
'rcptmac': '00:1a:8c:10:ad:20',
|
||||
'sourceip': '196.168.1.1',
|
||||
'targetip': '196.168.3.1',
|
||||
'sourceAppAddr': '196.168.1.1:63179',
|
||||
'targetAppAddr': '196.168.3.1:17242'
|
||||
}]
|
||||
|
||||
IPPacketString = [
|
||||
{'ObjID': 'SHO1',
|
||||
'ObjLable': 'A',
|
||||
'number': 1,
|
||||
'IPPackID': 'IPPack1',
|
||||
'TransPackID': 'TransPack1',
|
||||
'sourceip': '196.168.1.1',
|
||||
'targetip': '196.168.3.1',
|
||||
'sourceAppAddr': '196.168.1.1:37486',
|
||||
'targetAppAddr': '196.168.3.1:13191',
|
||||
'Mac_data': '00:1a:8c:10:ad:30'}]
|
||||
|
||||
TransPacketString = [
|
||||
{'ObjID': 'SHO1',
|
||||
'ObjLable': 'A',
|
||||
'TransPackID': 'TransPack1',
|
||||
'TransSeq': '1',
|
||||
'TransTag': 'A1',
|
||||
'AppPackId': '1',
|
||||
'sentport': '41749',
|
||||
'rpctport': '20712',
|
||||
'sourceAppAddr': '196.168.1.1:41749',
|
||||
'targetAppAddr': '196.168.3.1:20712',
|
||||
'timestamp': '15:25:59',
|
||||
'Mac_data': '00:1a:8c:10:ad:30'}]
|
||||
|
||||
AppPacketString = {
|
||||
'ObjID': 'SHO1',
|
||||
'ObjLable': 'A',
|
||||
'AppPackId': '1',
|
||||
'AppPackSize': 1,
|
||||
'AppPackTag': '1',
|
||||
'sourceAppAddr': '196.168.1.1:47325',
|
||||
'timeStamp': '15:22:28',
|
||||
'Mac_data': '00:1a:8c:10:ad:30',
|
||||
'targetAppAddr': '196.168.3.1:14035'}
|
||||
|
||||
test_line = Unpack_All(MacPacketString,IPPacketString,TransPacketString,AppPacketString)
|
||||
# test_line.unpack_mac_data()
|
||||
test = test_line.unpack_app_data()
|
||||
print(test)
|
||||
print(test_line.unpack_trans_data())
|
||||
print(test_line.unpack_ip_data())
|
||||
print(test_line.unpack_mac_data())
|
||||
'''
|
||||
{'ObjID': 'SHO1', 'ObjLable': 'A', 'AppPackId': '1', 'AppPackSize': 1, 'AppPackTag': '1', 'sourceAppAddr': '196.168.1.1:47325', 'targetAppAddr': '196.168.3.1:14035', 'AppPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'AppPackId': '1', 'AppPackSize': 1, 'AppPackTag': '1', 'sourceAppAddr': '196.168.1.1:47325', 'timeStamp': '15:22:28', 'Mac_data': '00:1a:8c:10:ad:30', 'targetAppAddr': '196.168.3.1:14035'}}
|
||||
[{'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack1', 'TransSeq': '1', 'TransTag': 'A1', 'AppPackId': '1', 'sentport': '41749', 'rpctport': '20712', 'sourceAppAddr': '196.168.1.1:41749', 'targetAppAddr': '196.168.3.1:20712', 'TransPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack1', 'TransSeq': '1', 'TransTag': 'A1', 'AppPackId': '1', 'sentport': '41749', 'rpctport': '20712', 'sourceAppAddr': '196.168.1.1:41749', 'targetAppAddr': '196.168.3.1:20712', 'timestamp': '15:25:59', 'Mac_data': '00:1a:8c:10:ad:30'}}]
|
||||
[{'ObjID': 'SHO1', 'ObjLable': 'A', 'IPPackID': 'IPPack1', 'TransPackID': 'TransPack1', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:37486', 'targetAppAddr': '196.168.3.1:13191', 'IPPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'number': 1, 'IPPackID': 'IPPack1', 'TransPackID': 'TransPack1', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:37486', 'targetAppAddr': '196.168.3.1:13191', 'Mac_data': '00:1a:8c:10:ad:30'}}]
|
||||
[{'ObjID': 'SHO1', 'ObjLable': 'A', 'MacPackId': 'MacPackID1', 'IPPackID': 'IPPack1', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:20', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:63179', 'targetAppAddr': '196.168.3.1:17242', 'MacPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'MAC_number': 1, 'MacPackId': 'MacPackID1', 'IPPackID': 'IPPack1', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:20', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:63179', 'targetAppAddr': '196.168.3.1:17242'}}]
|
||||
'''
|
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 248 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 4.9 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 2.6 KiB |
After Width: | Height: | Size: 7.2 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 112 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 5.5 KiB |
After Width: | Height: | Size: 4.9 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 7.2 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 96 KiB |
After Width: | Height: | Size: 39 KiB |
After Width: | Height: | Size: 111 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 169 KiB |
After Width: | Height: | Size: 180 KiB |
After Width: | Height: | Size: 388 KiB |
After Width: | Height: | Size: 187 KiB |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 6.1 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 4.7 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 150 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 5.7 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 199 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 9.4 KiB |
After Width: | Height: | Size: 726 B |
@ -0,0 +1,268 @@
|
||||
import ipaddress
|
||||
import re
|
||||
import sqlite3
|
||||
from tkinter import messagebox
|
||||
|
||||
import ttkbootstrap as tk
|
||||
from ttkbootstrap import *
|
||||
from ttkbootstrap import ttk
|
||||
import pandas as pd
|
||||
|
||||
from SimObjs import SimRouter
|
||||
|
||||
|
||||
def round_rectangle(cv, x1, y1, x2, y2, radius=30, **kwargs):
|
||||
"""
|
||||
绘制圆角矩形
|
||||
:param cv: canvas对象
|
||||
:param radius: 圆角值
|
||||
:return:
|
||||
"""
|
||||
points = [x1 + radius, y1,
|
||||
x1 + radius, y1,
|
||||
x2 - radius, y1,
|
||||
x2 - radius, y1,
|
||||
x2, y1,
|
||||
x2, y1 + radius,
|
||||
x2, y1 + radius,
|
||||
x2, y2 - radius,
|
||||
x2, y2 - radius,
|
||||
x2, y2,
|
||||
x2 - radius, y2,
|
||||
x2 - radius, y2,
|
||||
x1 + radius, y2,
|
||||
x1 + radius, y2,
|
||||
x1, y2,
|
||||
x1, y2 - radius,
|
||||
x1, y2 - radius,
|
||||
x1, y1 + radius,
|
||||
x1, y1 + radius,
|
||||
x1, y1]
|
||||
|
||||
return cv.create_polygon(points, **kwargs, smooth=True)
|
||||
|
||||
def validate_ip_address(ip_address):
|
||||
"""
|
||||
匹配ip地址格式是否规范
|
||||
:param ip_address: IP地址
|
||||
:return: Boolean
|
||||
"""
|
||||
# 定义IP地址的正则表达式模式
|
||||
pattern_with_subnet = r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})$'
|
||||
pattern_without_subnet = r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$'
|
||||
# 使用re模块进行匹配
|
||||
match_with_subnet = re.match(pattern_with_subnet, ip_address)
|
||||
match_without_subnet = re.match(pattern_without_subnet, ip_address)
|
||||
if match_with_subnet:
|
||||
# 带有子网掩码的IP地址
|
||||
# 检查每个组件的取值范围是否在 0-255 之间
|
||||
for group in match_with_subnet.groups()[:4]:
|
||||
if not (0 <= int(group) <= 255):
|
||||
return False
|
||||
# 检查子网掩码的取值范围是否在 0-32 之间
|
||||
subnet_mask = int(match_with_subnet.groups()[4])
|
||||
if not (0 <= subnet_mask <= 32):
|
||||
return False
|
||||
return True
|
||||
elif match_without_subnet:
|
||||
# 不带子网掩码的IP地址
|
||||
# 检查每个组件的取值范围是否在 0-255 之间
|
||||
for group in match_without_subnet.groups():
|
||||
if not (0 <= int(group) <= 255):
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
# IP地址格式不正确
|
||||
return False
|
||||
|
||||
|
||||
class ExportUtil():
|
||||
def __init__(self, path):
|
||||
self.conn = sqlite3.connect('./network.db')
|
||||
self.path = path
|
||||
|
||||
def get_table_names(self):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") # 如果你使用SQLite数据库
|
||||
tables = cursor.fetchall()
|
||||
cursor.close()
|
||||
return [table[0] for table in tables]
|
||||
|
||||
def export(self):
|
||||
tables = self.get_table_names()
|
||||
with pd.ExcelWriter(self.path, engine='openpyxl') as writer:
|
||||
for table in tables:
|
||||
table_name = table
|
||||
# a. 从数据库中获取表的数据并存储在DataFrame中
|
||||
query = f"SELECT * FROM {table_name}"
|
||||
df = pd.read_sql(query, self.conn)
|
||||
# b. 使用Pandas将数据写入Excel文件的不同sheet中
|
||||
df.to_excel(writer, sheet_name=table_name, index=False)
|
||||
|
||||
def import_data(self):
|
||||
excel_file = pd.ExcelFile(self.path)
|
||||
sheet_names = excel_file.sheet_names
|
||||
print(sheet_names)
|
||||
cursor = self.conn.cursor()
|
||||
for sheet_name in sheet_names:
|
||||
# 4. 使用 Pandas 读取工作表数据
|
||||
df = pd.read_excel(excel_file, sheet_name=sheet_name)
|
||||
# 5. 获取工作表的列名
|
||||
columns = df.columns.tolist()
|
||||
# 6. 构造插入语句
|
||||
columns_str = ', '.join(columns)
|
||||
placeholders = ', '.join(['?' for _ in range(len(columns))])
|
||||
sql = f"INSERT INTO {sheet_name} ({columns_str}) VALUES ({placeholders})"
|
||||
# 7. 将数据插入数据库
|
||||
for index, row in df.iterrows():
|
||||
# 8. 使用动态生成的 SQL 语句将数据插入数据库
|
||||
cursor.execute(sql, tuple(row))
|
||||
self.conn.commit()
|
||||
|
||||
|
||||
class RouterConfigWindow(tk.Toplevel):
|
||||
def __init__(self, parent, router_obj):
|
||||
super().__init__(parent)
|
||||
self.geometry("435x433+350+200")
|
||||
self.title(f"{router_obj.ObjLabel}路由表配置")
|
||||
self.router_obj = router_obj
|
||||
self.interface_entries = []
|
||||
self.router_table = {}
|
||||
self.create_interface_inputs()
|
||||
self.create_router_table()
|
||||
|
||||
def create_interface_inputs(self):
|
||||
label_text = ["接口1", "接口2", "接口3", "接口4"]
|
||||
for i in range(4):
|
||||
label = tk.Label(self, text=label_text[i], font=("黑体", 16))
|
||||
label.grid(row=i, column=0, padx=10, pady=5, sticky="w")
|
||||
entry = tk.Entry(self, width=20, font=("黑体", 16),)
|
||||
entry.grid(row=i, column=1, padx=10, pady=5, sticky="w")
|
||||
self.interface_entries.append(entry)
|
||||
button = tk.Button(self, text="添加", font=("黑体", 16), command=lambda index=i: self.add_router_entry(index))
|
||||
button.grid(row=i, column=2, padx=10, pady=5)
|
||||
lab = LabelFrame(self, text="示例")
|
||||
lab.grid(row=4, column=0, columnspan=3, sticky=W, padx=20)
|
||||
Label(lab, text="10.1.2.0/24 或者 10.1.2.12" if self.router_obj.ObjType == 2 else "MAC11", font=("黑体", 16)).pack()
|
||||
|
||||
def create_router_table(self):
|
||||
def on_right_click(event):
|
||||
row = self.router_treeview.identify_row(event.y) # 获取鼠标位置的行索引
|
||||
if row:
|
||||
self.router_treeview.selection_set(row) # 选中该行
|
||||
delete_menu.post(event.x_root, event.y_root) # 在鼠标位置弹出删除菜单
|
||||
|
||||
def delete_row():
|
||||
selected_items = self.router_treeview.selection() # 获取选中的行
|
||||
for item in selected_items:
|
||||
ifs, network = int(self.router_treeview.item(item)["values"][0][-1:]), self.router_treeview.item(item)["values"][1]
|
||||
self.router_obj.delete_config(ifs, network)
|
||||
self.router_treeview.delete(item)
|
||||
self.router_table_frame = tk.Frame(self)
|
||||
self.router_table_frame.grid(row=5, column=0, columnspan=3, padx=10, pady=5)
|
||||
style = ttk.Style()
|
||||
style.configure("Custom.Treeview.Heading", font=("宋体", 15))
|
||||
style.configure("Custom.Treeview", rowheight=30, font=("宋体", 15))
|
||||
self.router_treeview = ttk.Treeview(self.router_table_frame, style="Custom.Treeview", columns=("Interface", "Route"), show="headings")
|
||||
self.router_treeview.heading("Interface", text="接口")
|
||||
self.router_treeview.heading("Route", text="网段")
|
||||
self.router_treeview.pack(side="left", fill="both")
|
||||
scrollbar = ttk.Scrollbar(self.router_table_frame, orient="vertical", command=self.router_treeview.yview)
|
||||
scrollbar.pack(side="right", fill="y")
|
||||
self.router_treeview.configure(yscrollcommand=scrollbar.set)
|
||||
self.router_table = self.router_obj.router_table
|
||||
self.router_treeview.bind("<Button-3>", on_right_click)
|
||||
# 创建删除菜单
|
||||
delete_menu = tk.Menu(self.master, tearoff=False)
|
||||
delete_menu.add_command(label="删除", command=delete_row)
|
||||
self.update_router_table()
|
||||
|
||||
def add_router_entry(self, index):
|
||||
entry_text = self.interface_entries[index].get()
|
||||
try:
|
||||
ipaddress.ip_network(entry_text)
|
||||
if isinstance(self.router_obj, SimRouter):
|
||||
if not validate_ip_address(entry_text):
|
||||
messagebox.showerror("注意", message="添加的网段信息格式不合格")
|
||||
self.interface_entries[index].delete(0, tk.END)
|
||||
self.focus_set()
|
||||
return
|
||||
if entry_text:
|
||||
if index + 1 in self.router_table:
|
||||
self.router_table[index + 1].append(entry_text)
|
||||
else:
|
||||
self.router_table[index + 1] = [entry_text]
|
||||
self.interface_entries[index].delete(0, tk.END)
|
||||
self.router_obj.add_config(entry_text, index + 1)
|
||||
self.update_router_table()
|
||||
self.master.message.show_message(f"{self.router_obj.ObjLabel}添加配置{entry_text}成功!")
|
||||
except:
|
||||
messagebox.showerror("注意", message="网段格式错误!网段示例如下:\n10.1.2.0/24\n10.1.2.12")
|
||||
return
|
||||
|
||||
def update_router_table(self):
|
||||
self.router_treeview.delete(*self.router_treeview.get_children())
|
||||
for i, entrys in self.router_table.items():
|
||||
for entry in entrys:
|
||||
self.router_treeview.insert("", "end", values=(f"接口{i}", entry))
|
||||
|
||||
|
||||
class SwitchConfigWindow(RouterConfigWindow):
|
||||
def __init__(self, parent, router_obj):
|
||||
super().__init__(parent, router_obj)
|
||||
self.geometry("435x433+350+200")
|
||||
self.title(f"{router_obj.ObjLabel}交换表配置")
|
||||
self.router_obj = router_obj
|
||||
self.interface_entries = []
|
||||
self.router_table = {}
|
||||
self.create_interface_inputs()
|
||||
self.create_router_table()
|
||||
|
||||
def create_router_table(self):
|
||||
def on_right_click(event):
|
||||
row = self.router_treeview.identify_row(event.y) # 获取鼠标位置的行索引
|
||||
if row:
|
||||
self.router_treeview.selection_set(row) # 选中该行
|
||||
delete_menu.post(event.x_root, event.y_root) # 在鼠标位置弹出删除菜单
|
||||
|
||||
def delete_row():
|
||||
selected_items = self.router_treeview.selection() # 获取选中的行
|
||||
for item in selected_items:
|
||||
ifs, network = int(self.router_treeview.item(item)["values"][0][-1:]), self.router_treeview.item(item)["values"][1]
|
||||
self.router_obj.delete_config(ifs, network)
|
||||
self.router_treeview.delete(item)
|
||||
|
||||
self.router_table_frame = tk.Frame(self)
|
||||
self.router_table_frame.grid(row=5, column=0, columnspan=3, padx=10, pady=5)
|
||||
self.router_treeview = ttk.Treeview(self.router_table_frame, columns=("Interface", "Route"), show="headings")
|
||||
self.router_treeview.heading("Interface", text="接口")
|
||||
self.router_treeview.heading("Route", text="mac")
|
||||
self.router_treeview.pack(side="left", fill="both")
|
||||
scrollbar = ttk.Scrollbar(self.router_table_frame, orient="vertical", command=self.router_treeview.yview)
|
||||
scrollbar.pack(side="right", fill="y")
|
||||
self.router_treeview.configure(yscrollcommand=scrollbar.set)
|
||||
self.router_table = self.router_obj.mac_table
|
||||
self.router_treeview.bind("<Button-3>", on_right_click)
|
||||
# 创建删除菜单
|
||||
delete_menu = tk.Menu(self.master, tearoff=False)
|
||||
delete_menu.add_command(label="删除", command=delete_row)
|
||||
self.update_router_table()
|
||||
|
||||
|
||||
def add_router_entry(self, index):
|
||||
entry_text = self.interface_entries[index].get()
|
||||
if isinstance(self.router_obj, SimRouter):
|
||||
if not validate_ip_address(entry_text):
|
||||
messagebox.showerror("注意", message="添加的网段信息格式不合格")
|
||||
self.interface_entries[index].delete(0, tk.END)
|
||||
self.focus_set()
|
||||
return
|
||||
if entry_text:
|
||||
if index + 1 in self.router_table:
|
||||
self.router_table[index + 1].append(entry_text)
|
||||
else:
|
||||
self.router_table[index + 1] = [entry_text]
|
||||
self.interface_entries[index].delete(0, tk.END)
|
||||
self.router_obj.add_config(entry_text, index + 1)
|
||||
self.master.message.show_message(f"{self.router_obj.ObjLabel}添加配置{entry_text}成功!")
|
||||
self.update_router_table()
|
@ -0,0 +1,104 @@
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
import pandas as pd
|
||||
from pandas import DataFrame
|
||||
|
||||
conn = sqlite3.connect(sys.path[0]+"/network.db")
|
||||
|
||||
def execute_sql(sql):
|
||||
"""
|
||||
执行sql语句
|
||||
:param sql:
|
||||
:return:
|
||||
"""
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(sql)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def search(sql) -> DataFrame:
|
||||
return pd.read_sql(sql, conn)
|
||||
|
||||
|
||||
def delete_obj(obj_id):
|
||||
cursor = conn.cursor()
|
||||
delete_obj_sql = f"delete from sim_objs where ObjID='{obj_id}'"
|
||||
cursor.execute(delete_obj_sql)
|
||||
delete_conn_sql = f"delete from sim_conn where conn_id in (select conn_id from conn_config where node_id='{obj_id}')"
|
||||
cursor.execute(delete_conn_sql)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def truncate_db():
|
||||
init_database()
|
||||
|
||||
def init_database():
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
DROP TABLE IF EXISTS `conn_config`;
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE `conn_config` (
|
||||
`conn_id` varchar(55) NULL DEFAULT NULL,
|
||||
`node_id` varchar(55) NULL DEFAULT NULL,
|
||||
`node_ifs` int(0) NULL DEFAULT NULL,
|
||||
`ip` varchar(55) NULL DEFAULT NULL,
|
||||
`mac` varchar(128) NULL DEFAULT NULL,
|
||||
`conn_port` varchar(32) NULL DEFAULT NULL,
|
||||
`addr` varchar(255) NULL DEFAULT NULL,
|
||||
CONSTRAINT `conn_config_sim_conn_conn_id_fk` FOREIGN KEY (`conn_id`) REFERENCES `sim_conn` (`conn_id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ;
|
||||
""")
|
||||
cursor.execute("""
|
||||
DROP TABLE IF EXISTS `mac_table`;
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE `mac_table` (
|
||||
`obj_id` varchar(55) NULL DEFAULT NULL,
|
||||
`node_ifs` int(0) NULL DEFAULT NULL,
|
||||
`mac` varchar(55) NULL DEFAULT NULL,
|
||||
CONSTRAINT `mac_table_sim_objs_ObjID_fk` FOREIGN KEY (`obj_id`) REFERENCES `sim_objs` (`ObjID`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ;
|
||||
""")
|
||||
cursor.execute("""
|
||||
DROP TABLE IF EXISTS `router_table`;
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE `router_table` (
|
||||
`obj_id` varchar(55) NULL DEFAULT NULL,
|
||||
`node_ifs` int(0) NULL DEFAULT NULL,
|
||||
`segment` varchar(55) NULL DEFAULT NULL,
|
||||
CONSTRAINT `router_table_sim_objs_ObjID_fk` FOREIGN KEY (`obj_id`) REFERENCES `sim_objs` (`ObjID`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ;
|
||||
|
||||
""")
|
||||
cursor.execute("""
|
||||
DROP TABLE IF EXISTS `sim_conn`;
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE `sim_conn` (
|
||||
`conn_id` varchar(255) NOT NULL ,
|
||||
`ConfigCorrect` int(0) NULL DEFAULT NULL ,
|
||||
PRIMARY KEY (`conn_id`)
|
||||
) ;
|
||||
|
||||
""")
|
||||
cursor.execute("""
|
||||
DROP TABLE IF EXISTS `sim_objs`;
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE `sim_objs` (
|
||||
`ObjID` varchar(50) NOT NULL,
|
||||
`ObjType` int(0) NULL DEFAULT NULL,
|
||||
`ObjLabel` varchar(20) NULL DEFAULT NULL,
|
||||
`ObjX` int(0) NULL DEFAULT NULL,
|
||||
`ObjY` int(0) NULL DEFAULT NULL,
|
||||
`ConfigCorrect` int(0) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`ObjID`)
|
||||
) ;
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_database()
|
@ -0,0 +1,9 @@
|
||||
import random
|
||||
|
||||
def random_mac():
|
||||
# 生成六组两位的十六进制数
|
||||
return ":".join(["%02x" % random.randint(0, 255) for _ in range(6)])
|
||||
|
||||
# 生成MAC地址
|
||||
mac_address = random_mac()
|
||||
print(mac_address)
|