-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathriot_finder.py
More file actions
98 lines (91 loc) · 4.66 KB
/
riot_finder.py
File metadata and controls
98 lines (91 loc) · 4.66 KB
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
import subprocess
from pythonosc import dispatcher
from pythonosc import osc_server
import netifaces
import sys, traceback, os, time, json
class riot_net_config():
def __init__(self, OS, log_level=1):
self.OS = OS
self.log_level = log_level
def debug_text(self, message):
if self.log_level is not None:
print (message)
else:
return
def detect_net_config(self, net_interface_type):
if net_interface_type is not None and net_interface_type is not "":
net_interface_type, ssid = self.detect_wireless_interface([net_interface_type], self.OS)
else:
self.debug_text ("detecting wireless interface... (this can be set manually with --net)")
net_interface_type, ssid = self.detect_wireless_interface(netifaces.interfaces(), self.OS)
if ssid is not None:
self.debug_text("Connected to wifi network: " + ssid)
return net_interface_type, ssid
def detect_wireless_interface(self, interface_list, OS):
det_interface = det_ssid = None
for interface in interface_list:
if ("linux" in OS):
det_interface = os.popen('iwgetid').read()[:-1].split()[0]
det_ssid = os.popen('iwgetid -r').read()[:-1]
break
elif ("windows" in OS):
det_interface = os.popen('netsh wlan show interfaces | findstr /r "^....Name"').read()[:-1].split()[-1]
det_ssid = os.popen('netsh wlan show interfaces | findstr /r "^....SSID"').read()[:-1].split()[-1]
break
else:
ssid = os.popen('networksetup -getairportnetwork ' + interface).read()[:-1]
if '** Error: Error obtaining wireless information' not in ssid:
if len(ssid.split(': ')) > 1:
det_ssid = ssid.split(': ')[1]
det_interface = interface
break
return det_interface, det_ssid
def detect_ipv4_address(self, net_interface_type):
if "windows" in self.OS:
ipv4_addr = os.popen('netsh interface ipv4 show config %s | findstr /r "^....IP Address"' % net_interface_type).read()[:-1].split()[-1]
self.debug_text("Network interface %s address: %s" % (net_interface_type, ipv4_addr))
else:
addrs = netifaces.ifaddresses(net_interface_type)
ipv4_addr = addrs[netifaces.AF_INET][0]['addr']
self.debug_text("Network interface %s address: %s" % (net_interface_type, ipv4_addr))
return ipv4_addr
def reconfigure_ipv4_address(self, riot_ip, ipv4_addr, net_interface_type):
print(riot_ip, ipv4_addr)
if riot_ip not in ipv4_addr:
print ("The computer's IPv4 address must be changed to match")
if "windows" in self.OS:
cmd = "netsh interface ip set address %s static %s 255.255.255.0 192.168.1.1" % (net_interface_type, riot_ip)
else:
# UNIX ifconfig command with sudo
cmd = "gksudo ifconfig %s %s netmask 255.255.255.0" % (net_interface_type, riot_ip)
if "linux" not in self.OS:
# request OSX root privilege with GUI promt
cmd = json.dumps(cmd)
cmd = "osascript -e 'do shell script %s with prompt %s with administrator privileges'" % (cmd.replace('gksudo', 'sudo'), '"ServerBIT requires root access."')
print(">>> paste the following command: ")
print ( cmd )
return cmd
def run_ifconfig_command(cmd):
try:
# wait unitl command has run before continuing
proc = subprocess.check_output(cmd, shell=True)
timer(3)
return "ServerBIT R-IoT server is ready"
except subprocess.CalledProcessError:
return ("There was an error running this command. You can also change the ipv4 address manually (see R-IoT guide) \
\nclose window and try again")
# sys.exit(1)
def update_progress(count, total, status=''):
bar_len = 20
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
# As suggested by Rom Ruben (see: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console/27871113#comment50529068_27871113)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush()
def timer(t, rate = 0.25, text=''):
tt=round((t+rate)/rate)
for i in range(tt):
update_progress(i, round(t/rate), text)
time.sleep(rate)
print("\n")