-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceInterface.py
213 lines (198 loc) · 6.84 KB
/
deviceInterface.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
'''This script establishes ssh connections from the control device to the Device s on the Roombas. Written by Cael Shoop.'''
# This version includes untested changes that should allow it to work with any number of devices.
import paramiko
import time
import configparser as cp
import os
# Attempt to create SSH connection to all devices in cfg file
# Can be run by passing in a list of device IPs
def Connect(deviceIPs=[]):
if len(deviceIPs) == 0:
print('No devices passed in to connect to. Exiting.')
exit(-1)
deviceCfg = cp.ConfigParser()
if os.path.exists('deviceConfig.ini'):
deviceCfg.read_file(open('deviceConfig.ini'))
else:
print('Error: No config file. Please run DeviceSetup.py.')
exit(-1)
global sshs
global channels
sshs = []
channels = []
try:
print('Establishing connections...')
for ip in deviceCfg.sections():
if ip in deviceIPs:
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
curUser = None
curPw = None
try:
ssh.connect(ip, username=curUser, password=curPw, look_for_keys=False)
sshs.append(ssh)
channel = ssh.invoke_shell()
channels.append(channel)
print(str(ip) + ' connected.')
except:
print(str(ip) + ' failed to connect.')
else:
print(str(ip) + ' not in passed IP list, excluding.')
if len(sshs) == len(deviceIPs):
print('Success.')
global initialized
initialized = []
for ii in range(len(sshs)):
initialized.append(False)
else:
print('Failed to connect to the specified number of devices.')
exit(-1)
deviceCfg.close()
except:
print('Failed to set up SSH. Exiting.')
exit()
# Send parameter commands to a device
def Send(com, deviceNum):
try:
print(f'Sending command \'{com}\' to Device {deviceNum}...')
sshs[deviceNum].exec_command(com)
print(f'Command sent to Device {deviceNum}.')
except:
print(f'Command failed to send to Device {deviceNum}.')
# Send parameter commands to all devices
def SendAll(com):
for ii in range(len(sshs)):
try:
print(f'Sending command \'{com}\' to Device {ii}...')
ssh[ii].exec_command(com)
print(f'Command sent to Device {ii}.')
except:
print(f'Command failed to send to Device {ii}.')
# Sends messages in shell
def Shell(com, deviceNum):
if len(com) > 0 and com[-1] != '\n':
com = com + '\n'
if not initialized[deviceNum]:
out = channel[deviceNum].recv(9999)
initialized[deviceNum] = True
channel[deviceNum].send(com)
while not channel[deviceNum].recv_ready():
time.sleep(.1)
out = channel[deviceNum].recv(9999)
result = out.decode("ascii")
return result
# Sends a message to all shells, returns list of all outputs
def ShellAll(com):
if len(com) > 0 and com[-1] != '\n':
com = com + '\n'
results = []
for ii in range(len(sshs)):
if not initialized[ii]:
out = channel[ii].recv(9999)
initialized[ii] = True
channel[ii].send(com)
while not channel[ii].recv(9999):
time.sleep(.1)
out = channel[ii].recv(9999)
result = out.decode('ascii')
results.append(result)
return results
# Check if a file is on a device
def Check(localpath, deviceNum):
localpath = str(localpath)
if not initialized[deviceNum]:
out = channel[deviceNum].recv(9999)
initialized[deviceNum] = True
channel[deviceNum].send('ls | grep ' + localpath + '\n')
while not channel[deviceNum].recv_ready():
time.sleep(.1)
out = channel[deviceNum].recv(9999)
results = out.decode("ascii").split('\n')
results = results[1:]
if localpath in results:
return True
else:
return False
# Check if a file is on all devices
def CheckAll(localpath):
localpath = str(localpath)
allResults = []
onAllDevices = True
for ii in range(len(sshs)):
if not initialized[ii]:
out = channel[ii].recv(9999)
initialized[ii] = True
channel[ii].send('ls | grep ' + localpath + '\n')
while not channel[ii].recv_ready():
time.sleep(.1)
out = channel[0].recv(9999)
results = out.decode('ascii').split('\n')
if localpath in results[1:]:
allResults.append(True)
print(f'{localpath} found on Device {ii}.')
else:
onAllDevices = False
print(f'{localpath} not found on Device {ii}.')
if onAllDevices == True:
print(f'{localpath} found on all devices.')
return True
else:
print(f'{localpath} not found on all devices.')
return False
# Retrieve a file from one Device
def Retrieve(remotepath, deviceNum):
localpath = remotepath[:-4] + str(deviceNum) + remotepath[-4:]
try:
sftp = sshs[deviceNum].open_sftp()
sftp.get(remotepath, localpath)
sftp.close()
print(f'{localpath} successfully retrieved from Device {deviceNum}.')
except:
print(f'File retrieval failed from Device {deviceNum}.')
# Retrieve a file from all devices
def RetrieveAll(remotepath):
localpaths = []
for ii in range(len(sshs)):
localpath = remotepath[:-4] + str(ii) + remotepath[-4:]
localpaths.append(localpath)
counter = 0
for ssh in sshs:
try:
sftp = ssh.open_sftp()
sftp.get(remotepath, localpaths[counter])
sftp.close()
print(f'{localpaths[counter]} successfully retrieved from Device {counter}.')
except:
print(f'File retrieval failed from Device {counter}.')
# Send file to one device
def Transfer(localpath, deviceNum):
remotepath = localpath
try:
print(f'Sending {localpath} to Device {deviceNum}.')
sftp = sshs[deviceNum].open_sftp()
sftp.put(localpath, remotepath)
sftp.close()
print('Success.')
except:
print(f'Failed to send {localpath} to Device {deviceNum}.')
# Send file to all devices
def TransferAll(localpath):
remotepath = localpath
for ssh in sshs:
print(f'Sending {localpath}...')
try:
sftp = ssh.open_sftp()
sftp.put(localpath, remotepath)
sftp.close()
print('Success.')
except:
print(f'Failed to send {localpath}.')
# Close all SSH connections
def Disconnect():
print('Closing connections...')
for ssh in sshs:
try:
ssh.close()
print('Connection closed successfully.')
except:
print('Connection already closed.')