-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_local_connect.py
281 lines (233 loc) · 9.69 KB
/
action_local_connect.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
import sys
sys.dont_write_bytecode = True # No '*.pyc' precompiled files
from rich import print
from time import sleep
import os
import socket
import paramiko
from paramiko.ssh_exception import AuthenticationException
from fabric import Connection
from invoke import Responder
from lib_main import findkey
from lib_main import transform_string
from lib_local_factory import factory
from lib_parameters import lenny_dir
banned_start_of_commands = ('cd', 'alias', 'asterisk', 'ping')
banned_commands = ('python',)
class FabricConnection:
''' Takes a known host and tries to connect to it with known credentials.
Also manual connection is possible.
'''
dest_dir = lenny_dir
def __init__(self,
host, user, password, mac_addr='', hostname='', timeout=1
):
self.host = host
self.user = user
self.password = password
self.mac_addr = mac_addr
self.hostname = hostname
self.timeout = timeout # [s]
self.conn_object = Connection(
host=self.host,
user=self.user,
connect_kwargs={"password": self.password, "timeout": timeout}
)
self.host_info = (
'IP:' + self.host +
'\t\tMAC addr:' + self.mac_addr +
'\thostname:' + self.hostname
)
self.locals_vs_remotes = []
self.locals_vs_remotes.append([self.exit, factory(klass='Exit')])
self.locals_vs_remotes.append([self.shutdown, factory(klass='Shutdown')])
self.locals_vs_remotes.append([self.reboot, factory(klass='Reboot')])
self.locals_vs_remotes.append([self.init1, factory(klass='Init1')])
self.locals_vs_remotes.append([self.init2, factory(klass='Init2')])
self.locals_vs_remotes.append([self.init3, factory(klass='Init3')])
self.locals_vs_remotes.append([self.ipauto, factory(klass='IpAuto')])
self.locals_vs_remotes.append([self.ipstatic, factory(klass='IpStatic')])
self.locals_vs_remotes.append([self.tshoot, factory(klass='Tshoot')])
self.locals_vs_remotes.append([self.sendaudios, factory(klass='SendAudios')])
self.locals_vs_remotes.append([self.makeulaws, factory(klass='MakeUlaws')])
self.locals_vs_remotes.append([self.addcustoms, factory(klass='AddCustoms')])
self.locals_vs_remotes.append([self.delcustoms, factory(klass='DelCustoms')])
self.locals_vs_remotes.append([self.getrecordings, factory(klass='GetRecordings')])
self.locals_vs_remotes.append([self.delrecordings, factory(klass='DelRecordings')])
self.locals_vs_remotes.append([self.printmenu, factory(klass='PrintMenu')])
self.exiting_commands = ()
self.actions_vs_triggers = {} # Maps function names to the list of triggers
self.all_triggers = []
for item in self.locals_vs_remotes:
self.actions_vs_triggers[item[0]] = item[1].triggers
for trigger in item[1].triggers:
self.all_triggers.append(trigger)
def init1(self):
factory(klass='Init1')().action(conn_object=self.multirun)
def init2(self):
factory(klass='Init2')().action(conn_object=self.multirun)
def init3(self):
factory(klass='Init3')().action(
conn_object=self.conn_object, singlerun=self.singlerun)
def exit(self):
self.conn_object = factory(klass='Exit')().action(conn_object=self.conn_object)
def shutdown(self):
self.conn_object = factory(klass='Shutdown')().action(conn_object=self.conn_object)
self.conn_object = None
def reboot(self):
self.conn_object = factory(klass='Reboot')().action(conn_object=self.conn_object)
self.conn_object = None
def ipauto(self):
self.conn_object = factory(klass='IpAuto')().action(conn_object=self.conn_object)
self.conn_object = None
def ipstatic(self):
self.conn_object = factory(klass='IpStatic')().action(conn_object=self.conn_object)
self.conn_object = None
def tshoot(self):
factory(klass='Tshoot')().action(
conn_object=self.conn_object, singlerun=self.singlerun)
def sendaudios(self):
factory(klass='SendAudios')().action(
conn_object=self.conn_object, singlerun=self.singlerun)
def makeulaws(self):
factory(klass='MakeUlaws')().action(conn_object=self.conn_object)
def addcustoms(self):
factory(klass='AddCustoms')().action(conn_object=self.conn_object)
self.conn_object = None
def delcustoms(self):
factory(klass='DelCustoms')().action(conn_object=self.conn_object)
self.conn_object = None
def getrecordings(self):
factory(klass='GetRecordings')().action(conn_object=self.conn_object)
def delrecordings(self):
factory(klass='DelRecordings')().action(conn_object=self.conn_object)
def printmenu(self):
factory(klass='PrintMenu')().action()
def open(self, verbose=False):
exit_code = None
try:
self.conn_object.open()
except (
NameError,
paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.SSHException,
socket.gaierror,
socket.timeout,
ConnectionResetError,
OSError
) as error_code:
exit_code = error_code
else:
if verbose is True:
print('Connected to:', self.host,
'MAC addr:', self.mac_addr,
'hostname:', self.hostname,
)
return exit_code
def keep_connection(self):
print('-' * 96)
print('-' * 96)
exit_code = self.open()
if exit_code is not None:
print('Failed connecting to:', self.host_info)
print('Error code:', exit_code)
self.conn_object = None
command = None
while (
self.conn_object is not None
):
print('-' * 96)
print('-' * 96)
print('-' * 96)
print('-' * 96)
print('You are connected to the remote host.', self.host_info)
print(
'Type "?" for help, "exit" to disconnect, '
'"shutdown" to shutdown or enter a regular Linux command.'
)
command = input('command:>')
try:
sleep(0.1)
if str(command).lower() in ('?', 'help'):
self.printmenu()
elif (str(command).lower().startswith(banned_start_of_commands)
or str(command).lower() in banned_commands
):
print('The entered command is banned:', command,
' It will not work with fabric.')
elif str(command).lower() in self.all_triggers:
result = findkey(
item=str(command).lower(),
dict_of_seqs=self.actions_vs_triggers)
if result is not None:
result()
else:
command_result = self.singlerun(command)
except (
socket.timeout,
# NameError,
paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.SSHException,
socket.gaierror,
socket.timeout
# ConnectionResetError,
# OSError
) as exit_code:
print('Connection closing. Exit code:', exit_code)
print('You are now disconnected. Good bye!')
self.conn_object = None
def singlerun(self, command, watcher=None, hide=False, warn=True):
''' Opens connection and runs one command.
'''
result = None
exit_code = self.open()
sleep(0.1)
if exit_code is None and self.conn_object is not None:
if watcher is None:
result = self.conn_object.run(
command, hide=hide, warn=warn)
else:
result = self.conn_object.run(
command, hide=hide, warn=warn, watchers=[watcher])
elif len(command) == 0 and self.conn_object is not None:
sleep(0.1)
else:
print('Cannot re-establish connection and run the command.')
self.conn_object = None
exit_code = 'timeout'
return result
def multirun(self, commands, hide=False, warn=True):
'''
Args:
commands (list of lists):
'''
for command in commands:
result = None
print(('-' * 128 + '\n') * 4)
print('executing command:', command[0])
watcher = None
if len(command) in (1, 3):
if len(command) == 3:
pattern = transform_string(
input=command[1],
)
response = command[2] + '\n'
watcher = Responder(pattern=pattern, response=response)
try:
self.singlerun(command[0], watcher, hide=hide, warn=warn)
except (
NameError,
paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.SSHException,
socket.gaierror,
socket.timeout,
ConnectionResetError,
OSError
) as error_code:
exit_code = error_code
print('Error executing command(s).')
break
def main():
print('Library for connecting the RPi to your local PC.')
if __name__ == '__main__':
main()