-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetioDiscover.py
executable file
·126 lines (112 loc) · 4.6 KB
/
NetioDiscover.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import netifaces
import sys
from socket import *
class NetioDiscover:
def __init__(self, interface=None):
self.devices = []
if interface is None:
self.interfaces = netifaces.interfaces()
else:
self.interfaces = interface
print(self.interfaces)
def getDevicesLinux(self, timeout=1):
"""
Discover NETIO devices on all available network interfaces.
Listen for defined timeout at avery interface and return Dictionary with found NETIO devices.
Specific method for Linux operating system.
"""
self.devices=[]
outsocket = socket(AF_INET, SOCK_DGRAM)
outsocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
outsocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
outsocket.bind(('', 62386))
outsocket.settimeout(timeout)
for keys in self.interfaces:
try:
print("sending to: %s" % keys)
outsocket.setsockopt(SOL_SOCKET, 25, str(keys + '\0').encode('utf-8')) # this is used to send and receive on all interfaces - need root privileges
if sys.version_info[0] < 3:
outsocket.sendto("01ec00".decode("hex"), ('255.255.255.255', 62387))
else:
outsocket.sendto(bytes.fromhex("01ec00"), ('255.255.255.255', 62387))
except:
print('Unable to send request')
try:
while True:
self.devices.append(self.parseDeviceInfo(outsocket.recvfrom(1024)[0]))
except:
continue
outsocket.close()
return self.devices
def parseDeviceInfo(self, data):
"""
Parse NETIO Device information from data payload
"""
binarydata = bytearray(data)
if binarydata[0] is not 2:
print('Data are not valid')
return
else:
pass
i=3
params = []
datalen=len(binarydata)
while i < datalen-1:
param = {'DATA': []}
param['FTYPE'] = binarydata[i]
i += 1
paramlen = binarydata[i]
i += 1
if (i+paramlen) < len(binarydata):
for j in range(0, paramlen):
param['DATA'].append(binarydata[i+j])
i += paramlen
params.append(param)
device = {}
for item in params:
if item.get('FTYPE') == 0x01: #FIRMWARE_VERSION
device['fwversion'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x02: #MAC
device['mac'] = ':'.join(format(i, '02x') for i in item.get('DATA')).upper()
continue
if item.get('FTYPE') == 0x03: #IP
device['ip'] = '.'.join(str(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x04: #NETMASK
device['mask'] = '.'.join(str(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x05: #HOSTNAME
device['hostname'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x06: #DHCP
continue
if item.get('FTYPE') == 0x07: #SETUP_STATE
continue
if item.get('FTYPE') == 0x08: #RESULT
continue
if item.get('FTYPE') == 0x09: #PRODUCT
device['model'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x0a: #MANUFACTURER
device['manufacturer'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x0b: #PLATFORM
device['platform'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x0c: #VARIANT
device['hostname'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x0d: #TIMEOUT
continue
if item.get('FTYPE') == 0x0e: #GATEWAY
continue
if item.get('FTYPE') == 0x0f: #DNS
continue
if item.get('FTYPE') == 0x12: #PRETTY_PLATFORM_NAME
device['platformname'] = ''.join(chr(i) for i in item.get('DATA'))
continue
if item.get('FTYPE') == 0x13: #DEVICE_NAME
device['devicename'] = ''.join(chr(i) for i in item.get('DATA'))
continue
return device