10 def __init__(self,host,port):
13 self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
14 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
15 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
16 self.socket.bind((host, port))
17 self.seq = random.randint(100,2000)
18 self.srcmac = 0x00218698da1e
20 def recv(self,recvfunc,maxlen=8192,timeout=0.005):
21 self.socket.settimeout(timeout)
23 message, address = self.socket.recvfrom(maxlen)
24 except socket.timeout:
26 self.recvfunc(message,address)
27 self.recv(recvfunc,maxlen,timeout)
29 def parse_packet(self,p,hex=0):
33 data["mymac"]= p[8:14]
34 data["theirmac"]= p[14:20]
37 data[a]=binascii.hexlify(data[a])
40 def recvfunc(self,m,a):
41 hexdata = self.parse_packet(m,1)
42 data = self.parse_packet(m)
44 if data["seq"] == "\x00\x01":
46 print hexdata["theirmac"]
48 def send(self,host,port,data):
49 self.socket.sendto(data,(host,port))
53 def baseudp(self,cmd,ctype=0x0101,destmac=0):
55 data = struct.pack(">h",ctype) + 6* reserved + struct.pack('>LHLH', self.srcmac >> 16, self.srcmac & 0xffff, destmac >> 16, destmac & 0xffff) + 2*reserved
56 data += struct.pack(">h",self.seq)
57 data += "NSDP" + 4 * reserved + cmd
64 data = self.baseudp(cmd)
65 data += 3 * "\x00" + "\x02"
66 data += 3 * "\x00" + "\x03" + 3 * "\x00" + "\x04" + 3* "\x00" + "\x05"
67 data += 3 * "\x00" + "\x06" + 3 * "\x00" + "\x07" + 3* "\x00" + "\x08"
68 data += 3 * "\x00" + "\x0b" + 3 * "\x00" + "\x0c" + 3* "\x00" + "\x0d"
69 data += 3 * "\x00" + "\x0e" + 3 * "\x00" + "\x0f" + 2 * "\x00"
70 data += "\x74" + 3 * "\x00"
71 data += 2* "\xff" + 2* "\x00"
72 # self.send("255.255.255.255",63322, data)
74 self.send("255.255.255.255",63322, data)
77 data = self.baseudp(cmd)
78 data += 3 * "\x00" + "\x02"
79 data += 3 * "\x00" + "\x03" + 3 * "\x00" + "\x04" + 3* "\x00" + "\x05"
80 data += 3 * "\x00" + "\x06" + 3 * "\x00" + "\x07" + 3* "\x00" + "\x08"
81 data += 3 * "\x00" + "\x0b" + 3 * "\x00" + "\x0c" + 3* "\x00" + "\x0d"
82 data += 3 * "\x00" + "\x0e" + 3 * "\x00" + "\x0f" + 2 * "\x00"
83 data += 2* "\xff" + 2* "\x00"
84 # self.send("255.255.255.255",63322, data)
87 def passwd(self, dest, oldpass, newpass):
91 data = self.baseudp(cmd, destmac=dest, ctype=0x0103)
92 data += struct.pack(">h", len(oldpass))
94 data += struct.pack(">h", 9)
95 data += struct.pack(">h", len(newpass))
97 data += 2* "\xff" + 2* "\x00"
98 self.send("255.255.255.255",63322, data)
104 g.passwd(0xe091f5936b94, 'kat', 'hemmeligt')