forked from KoreNBeatZ/braiins-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover.py
executable file
·430 lines (345 loc) · 13.3 KB
/
discover.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3
# Copyright (C) 2018 Braiins Systems s.r.o.
#
# This file is part of Braiins Build System (BB).
#
# BB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import itertools
import ipaddress
import asyncssh
import asyncio
import json
import sys
from asyncssh import create_connection
from asyncssh.misc import async_context_manager
ALL_HOSTS = '*'
class NetworkInfo:
PROTO_DHCP = 'dhcp'
PROTO_STATIC = 'static'
def __init__(self):
self.mac = None
self.ip = None
self.proto = None
self.hostname = None
async def refresh(self, conn):
self.mac = await self._get_mac(conn)
self.ip = await self._get_ip(conn)
return self
async def _get_mac(self, conn):
return await asyncssh_run(conn, "cat /sys/class/net/eth0/address")
async def _get_ip(self, conn):
return await asyncssh_run(conn, "/sbin/ip route get 1 | awk '{print $NF;exit}'")
class PoolInfo:
def __init__(self, url, user, pwd):
self.url = url
self.user = user
self.pwd = pwd
class DeviceInfo:
INFO_UNKNOWN = 'unknown'
def __init__(self):
self.os = None
self.net = None
self.version = None
self.mode = None
self.ram_size = None
self.pools = None
self.note = None
async def refresh(self, conn):
self.os = await self._get_os(conn)
self.net = await self._get_net_info_cls()().refresh(conn)
self.version = await self._get_version(conn)
self.mode = await self._get_mode(conn)
self.ram_size = await self._get_ram_size(conn)
self.pools = await self._get_pools(conn)
self.note = await self._get_note(conn)
return self
def _get_net_info_cls(self):
return NetworkInfo
async def _get_os(self, conn):
return DeviceInfo.INFO_UNKNOWN
async def _get_version(self, conn):
return DeviceInfo.INFO_UNKNOWN
async def _get_mode(self, conn):
return None
@staticmethod
def size2int(size, unit=None):
scale = {
'B': 1,
'kB': 1024,
'mB': 1024 * 1024
}.get(unit or 'B')
return int(size) * scale
@staticmethod
def int2size(size):
for unit in ['B', 'KiB', 'MiB']:
if size % 1024:
break
size //= 1024
else:
unit = 'GiB'
return '{} {}'.format(size, unit)
async def _get_ram_size(self, conn):
ram_size = await asyncssh_run(conn, "grep MemTotal /proc/meminfo | awk '{print $2\" \"$3}'")
return self.size2int(*ram_size.split())
async def _get_cgminer_conf(self, conn):
return await asyncssh_run(conn, "cat /etc/cgminer.conf")
async def _get_pools(self, conn):
cgminer_conf = await self._get_cgminer_conf(conn)
if not cgminer_conf:
return []
cgminer_conf = json.loads(cgminer_conf)
pools = []
for pool in cgminer_conf['pools']:
pools.append(PoolInfo(pool['url'], pool['user'], pool['pass']))
return pools
async def _get_note(self, conn):
return None
def get_short(self):
info = list()
info.append(self.os)
info.append(self.version)
self.mode and info.append('[{}]'.format(self.mode))
self.ram_size and info.append('{{{} RAM}}'.format(self.int2size(self.ram_size)))
if self.net.proto == NetworkInfo.PROTO_DHCP:
info.append('{}({})'.format(self.net.proto, self.net.hostname))
if self.pools:
info.append('@' + self.pools[0].user)
self.note and info.append('# {}'.format(self.note))
return '{} ({}) | {}'.format(self.net.mac, self.net.ip, ' '.join(info))
class OpenWrtNetInfo(NetworkInfo):
async def refresh(self, conn):
await super().refresh(conn)
config = await asyncssh_run(conn, 'uci show network.lan | sed "1d;s/network.lan.//;s/\'//g"')
config = dict(line.split('=') for line in config.splitlines())
self.proto = {
'dhcp': self.PROTO_DHCP,
'static': self.PROTO_STATIC
}.get(config['proto'])
if self.proto == self.PROTO_DHCP:
self.hostname = config.get('hostname') or \
await asyncssh_run(conn, 'cat /proc/sys/kernel/hostname')
return self
class AmNetInfo(NetworkInfo):
async def refresh(self, conn):
await super().refresh(conn)
config = await asyncssh_run(conn, 'cat /config/network.conf')
config = dict(line.split('=') for line in config.splitlines())
if config['dhcp'] == 'true':
self.proto = self.PROTO_DHCP
self.hostname = config['hostname']
else:
self.proto = self.PROTO_STATIC
self.hostname = None
return self
class DmNetInfo(NetworkInfo):
async def refresh(self, conn):
await super().refresh(conn)
config = await asyncssh_run(conn, 'cat /config/network/25-wired.network')
config = dict(line.split('=') for line in config.splitlines() if not line.startswith('['))
if config.get('DHCP') == 'yes':
self.proto = self.PROTO_DHCP
self.hostname = await asyncssh_run(conn, 'hostname')
else:
self.proto = self.PROTO_STATIC
self.hostname = None
return self
class BosInfo(DeviceInfo):
def __init__(self, board_name):
super().__init__()
self.board_name = board_name
@staticmethod
async def create(conn):
supported_names = [
'dm1-g9',
'dm1-g19',
'dm1-g29',
'am1-s9'
]
board_name = await asyncssh_run(conn, "cat /tmp/sysinfo/board_name")
return await BosInfo(board_name).refresh(conn) if board_name in supported_names else None
def _get_net_info_cls(self):
return OpenWrtNetInfo
async def _get_os(self, conn):
return 'bOS'
async def _get_version(self, conn):
version = await asyncssh_run(conn, "cat /etc/bos_version") or \
await asyncssh_run(conn, "opkg list-installed | sed -n '/firmware/s/.*- //p'") or \
DeviceInfo.INFO_UNKNOWN
return '{}_{}'.format(self.board_name, version)
@staticmethod
async def _determine_mode(conn):
if (await asyncssh_run(conn, "mount | grep -q '/dev/ubi0_2 on /overlay'", cat=False)).exit_status == 0:
return 'nand'
elif (await asyncssh_run(conn, "mount | grep -q '/dev/mmcblk0p2 on /overlay'", cat=False)).exit_status == 0:
return 'sd'
else:
return 'recovery'
async def _get_mode(self, conn):
return await asyncssh_run(conn, "cat /etc/bos_mode") or \
await self._determine_mode(conn) or \
DeviceInfo.INFO_UNKNOWN
async def _get_note(self, conn):
return await asyncssh_run(conn, 'cat /etc/bos_note') or None
class AmInfo(DeviceInfo):
def __init__(self, board_name):
super().__init__()
self.board_name = board_name
self.fs_version = None
self.miner_type = None
self.logic_version = None
@staticmethod
async def create(conn):
supported_names = [
'XILINX',
'C5'
]
board_name = await asyncssh_run(conn, "cat /usr/bin/ctrl_bd")
return await AmInfo(board_name).refresh(conn) if board_name in supported_names else None
def _get_net_info_cls(self):
return AmNetInfo
async def refresh(self, conn):
compile_time = await asyncssh_run(conn, "cat /usr/bin/compile_time")
self.fs_version, self.miner_type, self.logic_version = compile_time.splitlines()[:3]
return await super().refresh(conn)
async def _get_os(self, conn):
return 'Antminer'
async def _get_version(self, conn):
type = self.miner_type.split()[1]
return '{} {} ({})'.format(type, self.fs_version, self.logic_version)
async def _get_note(self, conn):
return await asyncssh_run(conn, 'cat /config/note') or None
async def _get_cgminer_conf(self, conn):
return await asyncssh_run(conn, "cat /config/bmminer.conf")
async def detect_ssh(hostname):
try:
_, writer = await asyncio.wait_for(asyncio.open_connection(hostname, 22),
timeout=0.5)
except OSError:
return False
except asyncio.futures.TimeoutError:
return False
writer.close()
return True
class DmInfo(DeviceInfo):
def __init__(self, board_name):
super().__init__()
self.board_name = board_name
self.hw_revision = None
@staticmethod
async def create(conn):
supported_names = [
'G9',
'G19',
'G29'
]
board_name = await asyncssh_run(conn, "cat /tmp/hwver")
return await DmInfo(board_name).refresh(conn) if board_name in supported_names else None
def _get_net_info_cls(self):
return DmNetInfo
async def refresh(self, conn):
self.hw_revision = await asyncssh_run(conn, "cat /etc/hwrevision")
return await super().refresh(conn)
async def _get_os(self, conn):
return 'DragonMint'
async def _get_version(self, conn):
return ' '.join(self.hw_revision.split()[1].split('.')).upper()
async def _get_note(self, conn):
return await asyncssh_run(conn, 'cat /config/note') or None
@async_context_manager
def asyncssh_connect(host, port, passwords):
last_error = None
for password in itertools.chain(passwords.get(host, []), passwords.get(ALL_HOSTS, [])):
try:
conn, _ = yield from create_connection(None, host, port, username='root', password=password,
known_hosts=None)
break
except asyncssh.misc.DisconnectError as e:
if e.code == asyncssh.DISC_NO_MORE_AUTH_METHODS_AVAILABLE:
last_error = e
continue
else:
raise last_error
return conn
async def asyncssh_run(conn, *args, cat=True):
result = await asyncio.wait_for(conn.run(*args), timeout=0.5)
return result.stdout.strip() if cat else result
async def detect_device(args, hostname):
if not await detect_ssh(hostname):
return
try:
async with asyncssh_connect(hostname, 22, args.passwords) as conn:
for info_cls in [BosInfo, AmInfo, DmInfo]:
device_info = await info_cls.create(conn)
if device_info:
# print information about detected device
print(device_info.get_short())
break
except asyncssh.misc.DisconnectError:
return
except asyncio.futures.TimeoutError:
return
except asyncssh.process.ProcessError:
return
async def detect_devices(args, hostnames):
for hostname in hostnames:
await detect_device(args, str(hostname))
async def discover(args, hostnames):
tasks = [detect_devices(args, hostnames) for _ in range(args.jobs)]
await asyncio.wait(tasks)
def get_hostnames(hostname_list):
hostnames_iters = []
hostnames = []
for hostname in hostname_list:
try:
ip_range = ipaddress.IPv4Network(hostname)
if hostnames:
hostnames_iters.append(tuple(hostnames))
hostnames = []
hostnames_iters.append(iter(ip_range))
except ipaddress.AddressValueError:
hostnames.append(hostname)
if hostnames:
hostnames_iters.append(tuple(hostnames))
return itertools.chain(*hostnames_iters)
def get_passwords(passwords_path):
passwords = {
ALL_HOSTS: [None, '', 'admin', '123']
}
if passwords_path:
with open(passwords_path, 'r') as passwords_file:
for line in passwords_file:
line = line.strip()
host_pwd = line.split(':')
host = ALL_HOSTS if len(host_pwd) == 1 else host_pwd[0]
passwords.setdefault(host, []).append(host_pwd[-1])
return passwords
def main(args):
hostnames = get_hostnames(args.hostname)
args.passwords = get_passwords(args.passwords)
loop = asyncio.get_event_loop()
loop.run_until_complete(discover(args, hostnames))
loop.close()
if __name__ == "__main__":
# execute only if run as a script
parser = argparse.ArgumentParser()
parser.add_argument('hostname', nargs='+',
help='list of hostnames or subnet range')
parser.add_argument('--passwords',
help='path to file with list of possible passwords for connection')
parser.add_argument('-j', '--jobs', type=int, default=50,
help='number of concurrent jobs to scan network')
# parse command line arguments
args = parser.parse_args(sys.argv[1:])
main(args)