diff --git a/NetworkAnalog/SimObjs.py b/NetworkAnalog/SimObjs.py index bf08d68..073d43a 100644 --- a/NetworkAnalog/SimObjs.py +++ b/NetworkAnalog/SimObjs.py @@ -233,6 +233,7 @@ class SimPacket(): self.message = message self.up_jump = None # 上一跳连接对象 self.ping = ping + self.flag = False self.img = ImageTk.PhotoImage( Image.open(sys.path[0] + "/../datas/images/packet.png").resize((30, 30))) self.id = str(uuid4()) diff --git a/NetworkAnalog/__pycache__/NetworkAnalog.cpython-37.pyc b/NetworkAnalog/__pycache__/NetworkAnalog.cpython-37.pyc index 1f15bb1..53f3c49 100644 Binary files a/NetworkAnalog/__pycache__/NetworkAnalog.cpython-37.pyc and b/NetworkAnalog/__pycache__/NetworkAnalog.cpython-37.pyc differ diff --git a/NetworkAnalog/__pycache__/SimObjs.cpython-37.pyc b/NetworkAnalog/__pycache__/SimObjs.cpython-37.pyc index 451b022..82551ef 100644 Binary files a/NetworkAnalog/__pycache__/SimObjs.cpython-37.pyc and b/NetworkAnalog/__pycache__/SimObjs.cpython-37.pyc differ diff --git a/NetworkAnalog/__pycache__/SimObjs.cpython-39.pyc b/NetworkAnalog/__pycache__/SimObjs.cpython-39.pyc new file mode 100644 index 0000000..cc071d8 Binary files /dev/null and b/NetworkAnalog/__pycache__/SimObjs.cpython-39.pyc differ diff --git a/NetworkAnalog/__pycache__/SimUtil.cpython-37.pyc b/NetworkAnalog/__pycache__/SimUtil.cpython-37.pyc new file mode 100644 index 0000000..1a4fb48 Binary files /dev/null and b/NetworkAnalog/__pycache__/SimUtil.cpython-37.pyc differ diff --git a/NetworkAnalog/__pycache__/SimUtil.cpython-39.pyc b/NetworkAnalog/__pycache__/SimUtil.cpython-39.pyc new file mode 100644 index 0000000..13fa0c9 Binary files /dev/null and b/NetworkAnalog/__pycache__/SimUtil.cpython-39.pyc differ diff --git a/NetworkAnalog/__pycache__/dbUtil.cpython-39.pyc b/NetworkAnalog/__pycache__/dbUtil.cpython-39.pyc new file mode 100644 index 0000000..ff0a300 Binary files /dev/null and b/NetworkAnalog/__pycache__/dbUtil.cpython-39.pyc differ diff --git a/NetworkAnalog/data.csv b/NetworkAnalog/data.csv new file mode 100644 index 0000000..babe5a9 --- /dev/null +++ b/NetworkAnalog/data.csv @@ -0,0 +1,19 @@ +,组件名称,接口,ip,mac,端口,应用层地址 +0,SHO3,1,10.1.1.10,MAC1,80.0,10.1.2.10:10810:Name3 +1,SRO1,1,10.1.1.10,MAC11,, +2,SRO1,2,,,, +3,SRO2,1,,,, +4,SRO2,2,10.1.1.12,MAC13,, +5,SRO3,1,10.1.12.10,MAC31,, +6,SRO2,3,10.1.1.13,MAC14,, +7,SRO4,1,10.1.2.11,MAC21,, +8,SRO5,1,10.1.13.10,MAC41,, +9,SRO3,2,10.1.12.11,MAC32,, +10,SHO4,1,10.1.2.15,MAC3,80.0,10.1.2.10:10810:Name3 +11,SRO4,2,10.1.2.12,MAC22,, +12,SRO5,2,10.1.13.11,MAC42,, +13,SHO2,1,10.1.12.15,MAC5,80.0,10.1.2.10:10810:Name3 +14,SRO3,3,10.1.12.12,MAC33,, +15,SHO1,1,10.1.20.5,MAC4,80.0,10.1.20.5:10810:Name3 +16,SHO5,1,10.1.22.11,MAC2,80.0,10.1.2.10:10810:Name3 +17,SRO3,4,10.1.12.13,MAC34,, diff --git a/NetworkAnalog/data.xlsx b/NetworkAnalog/data.xlsx new file mode 100644 index 0000000..caedf1b Binary files /dev/null and b/NetworkAnalog/data.xlsx differ diff --git a/NetworkAnalog/database_wang.xlsx b/NetworkAnalog/database_wang.xlsx new file mode 100644 index 0000000..0e61b61 Binary files /dev/null and b/NetworkAnalog/database_wang.xlsx differ diff --git a/NetworkAnalog/function.py b/NetworkAnalog/function.py new file mode 100644 index 0000000..c7d926e --- /dev/null +++ b/NetworkAnalog/function.py @@ -0,0 +1,114 @@ +import re + +from NetworkAnalog.SimObjs import SimBase, AllSimConnect, SimRouter, SimHost + + + + +class SimPacket(): # 需要补充至编程13.7当中 + def __init__(self, source_ip, source_mac, destination_ip, message=None, ping=False): + """ + :param source_mac: 源主机mac地址 + :param source_ip: 源主机ip地址 + :param destination_mac: 目的主机mac地址 + :param destination_ip: 目的主机ip地址 + :param message: 数据 + """ + self.source_mac = source_mac + self.source_ip = source_ip + self.destination_ip = destination_ip + self.message = message + self.up_jump = None # 上一跳连接对象 + self.ping = ping + self.flag = False # 是否传输成功 + +class SimHost(SimBase): + def receive(self, packet: SimPacket): # 修改编程13.7中SimHost的receive方法 + # 判断目的IP是否与本机IP一致,以及判断当前主机是否联网 + if packet.destination_ip == self.interface[0]["ip"] and self.network_online: + print(f"主机{self.ObjLabel}接受到数据{packet.message}") + packet.flag = True # 将packet的传输状态改为True,视为传输成功 + else: + print(f"数据传输失败") + + +def validate_ip_address(ip_address):# 定义IP地址的正则表达式模式 + pattern_with_subnet = r'^(\d{1,3})\.(\d{1,3})\.(' \ + r'\d{1,3})\.(\d{1,3})/(\d{1,2})$' + pattern_without_subnet = r'^(\d{1,3})\.(\d{1,3})' \ + r'\.(\d{1,3})\.(\d{1,3})$' + match_with_subnet = re.match(pattern_with_subnet, + ip_address) # 使用re模块进行匹配 + 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 + subnet_mask = int(match_with_subnet.groups()[4]) + # 检查子网掩码的取值范围是否在 0-32 之间 + if not (0 <= subnet_mask <= 32): + return False + return True + elif match_without_subnet: # 不带子网掩码的IP地址 + for group in match_without_subnet.groups(): + if not (0 <= int(group) <= 255): + return False + return True + else: + return False # IP地址格式不正确 + +def is_same_subnet(ip1, ip2): # 判断两个IP地址是否属于同一网段 + # 将IP地址和子网掩码转换为整数列表 + ip1_parts = [int(part) for part in ip1.split('.')] + ip2_parts = [int(part) for part in ip2.split('.')] + subnet_mask_parts = [int(part) for part in "255.255.255.0".split('.')] + # 计算网络地址 + network1 = [ip1_parts[i] & subnet_mask_parts[i] for i in range(4)] + network2 = [ip2_parts[i] & subnet_mask_parts[i] for i in range(4)] + # 比较网络地址是否相同 + return network1 == network2 + +class NetworkAnalog(): + ... + def check_network(self): # 判断各网络节点是否配置正确 + flag = True + sim_host_list = [] + for key, tag in self.AllSimObjs.items(): # 遍历所有组件 + # todo: 判断主机的ip配置是否都合格 + if tag.ObjType == 1: # 如果该组件为主机 + for interface in tag.interface: # 遍历该主机下所有接口 + index = tag.interface.index(interface) + 1 # 获取当前接口号 + ip_ = interface["ip"] # 获取当前接口IP + if not validate_ip_address(ip_): # 判断主机配置的IP是否正确 + self.message.show_message(f"主机{tag.ObjLabel}的接口{index} IP 配置错误") + flag = False + sim_host_list.append(tag) # 将所有主机组件添加到列表中 + # todo: 判断路由器的四个接口是否都在同一网段下 + if tag.ObjType == 2: # 如果当前组件为路由器 + ip_ = tag.interface[0]["ip"] # 将第一个接口设为默认 + for interface in tag.interface: # 遍历所有接口 + index = tag.interface.index(interface) + 1 # 获取当前接口号 + if not is_same_subnet(ip_, interface["ip"]): # 判断所有接口是否在同一网段下 + self.message.show_message(f"路由器{tag.ObjLabel}中接口{index}不在同一网段下") + flag = False + # todo: 检查路由表是否能跳转到下一跳 + if tag.ObjType == 2: # 如果当前组件为路由器 + for conn in tag.connections: # 遍历该组件所有链接线 + next_jump = conn.NobjE # 获取下一跳组件 + if not is_same_subnet(tag.interface[conn.IfsS]["ip"], next_jump.interface[conn.IfsE]["ip"]): # 检查路由表是否能跳转到下一跳 + self.message.show_message(f"路由器组件{tag.ObjLabel}路由表配置错误,请检查") + flag = False + # todo: 判断从所有主机发送数据包到其他所有主机能否成功 + for sim_host in sim_host_list: # 遍历所有主机组件 + sim_host: SimHost = sim_host + for destination_sim_host in sim_host_list: # 遍历所有主机组件 + packet = SimPacket(sim_host.interface[0]['ip'], + sim_host.interface[0]['mac'], + destination_sim_host.interface[0]["ip"]) # 封装数据包,将发送主机信息和接收主机信息封装到数据包中 + sim_host.send(packet) # 模拟发送 + if not packet.flag: # 判断是否传输成功 + self.message.show_message(f"主机{sim_host.ObjLabel}传输至{destination_sim_host.ObjLabel}失败,请检查配置!") + return flag + + diff --git a/NetworkAnalog/network.db b/NetworkAnalog/network.db index e774764..45d6212 100644 Binary files a/NetworkAnalog/network.db and b/NetworkAnalog/network.db differ diff --git a/NetworkAnalog/x1_test.py b/NetworkAnalog/x1_test.py index 3fd1106..aa8c289 100644 --- a/NetworkAnalog/x1_test.py +++ b/NetworkAnalog/x1_test.py @@ -1,43 +1,19 @@ +import socket +def is_same_subnet(ip1, ip2): + # 将IP地址和子网掩码转换为整数列表 + ip1_parts = [int(part) for part in ip1.split('.')] + ip2_parts = [int(part) for part in ip2.split('.')] + subnet_mask_parts = [int(part) for part in "255.255.255.0".split('.')] -AllSimObj = [ - {"ObjID": "SHO1", "ObjType": 1, "ObjLabel": "A", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO2", "ObjType": 1, "ObjLabel": "B", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO3", "ObjType": 1, "ObjLabel": "C", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO4", "ObjType": 1, "ObjLabel": "D", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO5", "ObjType": 1, "ObjLabel": "E", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO6", "ObjType": 1, "ObjLabel": "F", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO7", "ObjType": 1, "ObjLabel": "G", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHO8", "ObjType": 1, "ObjLabel": "H", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO1", "ObjType": 2, "ObjLabel": "R1", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO2", "ObjType": 2, "ObjLabel": "R2", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO3", "ObjType": 2, "ObjLabel": "R3", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO4", "ObjType": 2, "ObjLabel": "R4", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO5", "ObjType": 2, "ObjLabel": "R5", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO6", "ObjType": 2, "ObjLabel": "R6", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SRO7", "ObjType": 2, "ObjLabel": "R7", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SWO1", "ObjType": 3, "ObjLabel": "S1", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, - {"ObjID": "SHub1", "ObjType": 4, "ObjLabel": "Hub1", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0}, -] + # 计算网络地址 + network1 = [ip1_parts[i] & subnet_mask_parts[i] for i in range(4)] + network2 = [ip2_parts[i] & subnet_mask_parts[i] for i in range(4)] -AllSimConn = [ - {"ConnObjID": "conn1", "NobjS": "SHO1", "IfsS": 1, "NobjE": "SRO1", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SHO2", "IfsS": 1, "NobjE": "SRO1", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO2", "IfsS": 1, "NobjE": "SRO1", "IfsE": 4, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SWO1", "IfsS": 1, "NobjE": "SRO1", "IfsE": 3, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SHO2", "IfsS": 1, "NobjE": "SWO1", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO2", "IfsS": 2, "NobjE": "SRO4", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO2", "IfsS": 3, "NobjE": "SRO3", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO3", "IfsS": 2, "NobjE": "SRO4", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO3", "IfsS": 3, "NobjE": "SRO5", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO4", "IfsS": 4, "NobjE": "SHO5", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO4", "IfsS": 3, "NobjE": "SRO5", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO5", "IfsS": 3, "NobjE": "SHO6", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO5", "IfsS": 4, "NobjE": "SRO6", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO6", "IfsS": 3, "NobjE": "SHO3", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO6", "IfsS": 1, "NobjE": "SRO7", "IfsE": 2, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO7", "IfsS": 1, "NobjE": "SHub1", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SRO7", "IfsS": 3, "NobjE": "SHO4", "IfsE": 1, "ConfigCorrect": 0}, - {"ConnObjID": "conn1", "NobjS": "SHub1", "IfsS": 2, "NobjE": "SHO4", "IfsE": 1, "ConfigCorrect": 0}, -] + # 比较网络地址是否相同 + return network1 == network2 + + + +print(is_same_subnet("192.168.10.23", "192.12.10.21")) \ No newline at end of file diff --git a/datas/images/false.png b/datas/images/false.png new file mode 100644 index 0000000..7bdb1f0 Binary files /dev/null and b/datas/images/false.png differ diff --git a/datas/images/true.png b/datas/images/true.png new file mode 100644 index 0000000..82459c7 Binary files /dev/null and b/datas/images/true.png differ