forked from waderwu/wsniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
73 lines (54 loc) · 1.34 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import time
import subprocess,re
def str2hex(packet):
'''
b'aa' => '6161'
'aa' => '6161'
'''
if (isinstance(packet,str)):
return "".join("{:02x}".format(ord(c)) for c in packet)
if (isinstance(packet,bytes)):
return packet.hex()
def str2byte(string):
'''
'6161' => ['61','61']
'''
return [string[i:i+2] for i in range(0,len(string),2)]
def byte2bin(hexstr):
'''
03 => '00000011'
'''
length = len(hexstr)*4
formats = '{:0%db}'%length
return formats.format(int(hexstr,16))
def hexstr2unicode(hexstr):
'''
'61' -> b'a'->'a'
'''
hexlist = str2byte(hexstr)
hexlist = [int(i,16) for i in hexlist]
byte = bytes(hexlist)
return byte.decode()
def hexstr2bytes(hexstr):
'''
'61' -> b'a'
'''
hexlist = str2byte(hexstr)
hexlist = [int(i, 16) for i in hexlist]
byte = bytes(hexlist)
return byte
def get_mac(strbyte):
return ":".join(str2byte(strbyte))
def get_ip(strbyte):
ip = str2byte(strbyte)
ip = '.'.join([str(int(i,16)) for i in ip])
return ip
def get_timestamp(strbyte):
'''
2017-11-28 10:00:00
'''
strtime = ''
for i in range(len(strbyte)//2-1,-1,-1):
strtime += strbyte[2*i:2*i+2]
st = time.localtime(int(strtime,16))
return time.strftime('%Y-%m-%d %H:%M:%S',st)