-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppev3dev.py
executable file
·147 lines (134 loc) · 4.26 KB
/
cppev3dev.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
#!/usr/bin/python3
##########################
# TODO #
##########################
#
# echo self.ssh['password'] | sudo -S sh -c "echo 'robot ALL=(ALL:ALL) NOPASSWD: ALL' >> /etc/sudoers"
# into sudoers file!
#
##########################
import os
import subprocess
from getpass import getpass
import functions as func
import libbrick
import libconfig
config_files = {
"error": "./errors.log",
"config": "./config.cfg",
"logs": './logs.log'
}
config = libconfig.Config(config_files['config'])
tmp_dir = config.get('tmp')
brick = libbrick.Brick(config.get('username'), config.get('password'), config.get('ip'))
options = {
"menu": [
['connect', 'Check Connection'],
['customCommand', 'Send Custom Command'],
['compile2', 'Compile & upload'],
['compile1', 'Only Compile'],
['extra', "Extra commands"],
['config', 'Configure'],
['exit', 'Exit']
],
"config": [
['updatel', "Update Login"],
['updatep', "Update Password"],
['updateai', "Update Address IP"],
['menu', "Back"]
],
"extra": [
['addSudoers', "Add 'NOPASSWD' to /etc/sudoers file"],
['menu', "Back"]
],
}
selected = 'menu'
out = ""
if __name__ == "__main__":
menu_pos = 0
try:
while True:
func.cls()
print("---------------------------")
print("| EV3DEV LINKER |")
print("---------------------------")
print("Login:", brick.ssh['username'])
print("Address IP:", brick.ssh['ip'])
print("Running as Admin:", func.isAdmin())
print("Brick output:", out)
print("---------------------------")
print("| MENU |")
print("---------------------------")
for option in range(len(options[selected])):
if menu_pos == option:
print(options[selected][option][1], "<<")
else:
print(options[selected][option][1])
print("---------------------------")
try:
key = func.getch()
if key == b'\xe0' or key == "[":
key = func.getch()
if key == b'P' or key == "B":
menu_pos += 1
elif key == b'H' or key == "A":
menu_pos -= 1
elif key == b'\x03' or key == b'\x1b':
raise(KeyboardInterrupt)
elif key == b'\r' or key == "\n":
#Logic
if options[selected][menu_pos][0] == 'exit':
raise(KeyboardInterrupt)
elif options[selected][menu_pos][0] == "config":
selected = options[selected][menu_pos][0]
menu_pos = 0
elif options[selected][menu_pos][0] == "menu":
selected = options[selected][menu_pos][0]
menu_pos = 0
elif options[selected][menu_pos][0] == "extra":
selected = options[selected][menu_pos][0]
menu_pos = 0
elif options[selected][menu_pos][0] == "updatel":
inp = input("Enter new login: ")
config.update('username', inp)
brick.update('username', inp)
elif options[selected][menu_pos][0] == "updatep":
inp = getpass("Enter new password: ")
config.update('password', inp)
brick.update('password', inp)
elif options[selected][menu_pos][0] == "updateai":
inp = input("Enter new Adress IP: ")
config.update('ip', inp)
brick.update('ip', inp)
elif options[selected][menu_pos][0] == "connect":
out = func.checkConnection(brick)
elif options[selected][menu_pos][0] == "compile1":
func.compiling(config)
input("Finished! Click enter...")
elif options[selected][menu_pos][0] == "compile2":
filename = func.compiling(config)
brick.send_file(filename)
input("Finished! Click enter...")
elif options[selected][menu_pos][0] == "customCommand":
command = input("Insert command: ")
try:
out = brick.send(command)
except libbrick.BrickCannotSendException:
out = False
elif options[selected][menu_pos][0] == "addSudoers":
if(func.checkConnection(brick)):
echo_command = "'" + brick.ssh['username'] + " ALL=(ALL:ALL) NOPASSWD: ALL'"
password = brick.ssh["password"]
out = brick.send(f'echo {password} | sudo -S sh -c "echo {echo_command} >> /etc/sudoers"')
else:
out = "Cannot add 'NOPASSWD' to /etc/sudoers file!"
except OverflowError:
raise(KeyboardInterrupt)
if menu_pos >= len(options[selected]):
menu_pos = 0
if menu_pos < 0:
menu_pos = len(options[selected])-1
#print(key)
except KeyboardInterrupt:
print("Exitting...")
exit()