the code below:
import socket
import struct
import time
import randomdef create_dns_query(domain="example123.com"):# DNS HeaderID = random.randint(0, 65535) # 随机查询IDFLAGS = 0x0100 # Standard queryQDCOUNT = 1 # One questionANCOUNT = 0 # No answersNSCOUNT = 0 # No authority recordsARCOUNT = 0 # No additional records# 构建DNS头部header = struct.pack('!HHHHHH', ID, FLAGS, QDCOUNT, ANCOUNT, NSCOUNT, ARCOUNT)# 构建查询部分query_parts = []for part in domain.split('.'):query_parts.append(bytes([len(part)]))query_parts.append(part.encode())query_parts.append(b'\x00') # 终止符# 查询类型(A记录)和类(IN)query_type = struct.pack('!HH', 1, 1) # TYPE=A, CLASS=IN# 组合所有部分query = header + b''.join(query_parts) + query_typereturn querydef send_dns_query(target_ip="192.168.202.254", port=53, interval=1):# 创建UDP socketsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)try:print(f"开始向 {target_ip}:{port} 发送DNS查询...")count = 0while True:try:# 创建并发送DNS查询query = create_dns_query()sock.sendto(query, (target_ip, port))count += 1print(f"已发送 {count} 个DNS查询包")time.sleep(interval) # 等待指定的间隔时间except KeyboardInterrupt:print("\n停止发送...")breakexcept Exception as e:print(f"发送出错: {e}")breakfinally:sock.close()if __name__ == "__main__":# 可以修改目标IP、端口和发送间隔(秒)send_dns_query("192.168.1.1", 53, 1)