-
Notifications
You must be signed in to change notification settings - Fork 112
/
connect2.py
70 lines (56 loc) · 2.07 KB
/
connect2.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import types
import platform
import ctypes
import binascii
import re
import requests
import urllib
import xmir_base
from gateway import *
# Devices:
# R2100 FW v2.0.722 Router AC2100
# R2350 FW v1.3.8 AIoT Router AC2350
# R3600 FW v1.0.17 AIoT Router AX3600
# RM2100 FW v2.0.23 Router Redmi AC2100
# RM1800 FW v1.0.336 AX1800 Wi-Fi 6 Mesh Router
# RA67 FW v1.0.33 AX5 Router
gw = Gateway(timeout = 4, detect_ssh = False)
if gw.status < 1:
die("Xiaomi Mi Wi-Fi device not found (IP: {})".format(gw.ip_addr))
print("device_name =", gw.device_name)
print("rom_version = {} {}".format(gw.rom_version, gw.rom_channel))
print("mac address = {}".format(gw.mac_address))
dn = gw.device_name
gw.ssh_port = 22
ret = gw.detect_ssh(verbose = 1, interactive = True)
if ret > 0:
die(0, "SSH server already installed and running")
stok = gw.web_login()
ext_name = 'misystem/set_config_iotdev'
user_id = '_username_'
def exec_cmd(cmd):
params = { 'bssid': 'Xiaomi', 'user_id': user_id, 'ssid': ('-h' + '\n' + cmd + '\n') }
res = requests.get(gw.apiurl + ext_name, params = params)
return res.text
res = exec_cmd('nvram set bootdelay=3; set boot_wait=on; nvram set ssh_en=1; nvram commit;')
if res != '{"code":0}':
die('Extension "/api/{}" not working!'.format(ext_name))
cmd = ''
cmd += 'echo -e "root\\nroot" | passwd root' + '\n'
#cmd += 'sed -i \'s/channel=.*/channel="debug"/g\' /etc/init.d/dropbear' + '\n'
cmd += 'sed -i \'s/"$flg_ssh" != "1" -o "$channel" = "release"/-n ""/g\' /etc/init.d/dropbear' + '\n'
cmd += "/etc/init.d/dropbear enable" + '\n'
cmd += "/etc/init.d/dropbear restart" + '\n'
cmd += 'logger -p err -t XMiR "completed!"' + '\n'
res = exec_cmd(cmd)
#if res != '{"code":0}':
# die('Extension "/api/misystem/set_config_iotdev" not working!!!')
time.sleep(0.5)
gw.passw = 'root'
gw.ping(contimeout = 32) # RSA host key generate very slow!
print("")
print("#### Connection to device {} is OK ####".format(gw.device_name))