/
persistent_registers.py
253 lines (183 loc) · 5.28 KB
/
persistent_registers.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
import paramiko
from time import sleep
import pigpio
from os.path import exists
def main():
user = "root"
password = "woot"
host = ""
localpi = pigpio.pi()
power_pin = 0
serial_passes = 0
plugin_passes = 0
serial_fails = 0
plugin_fails = 0
A_firmware = ""
B_firmware = ""
initial_run = True
initial_plugins = 0
current_plugins = 0
trial_number = 0
plugin_trial_number = 0
returned_serial = ""
#while A_firmware and B_firmware == "":
#A_firmware = input("What's the normal firmware's git hash?:\n")
#B_firmware = input("What's the alternate firmware's git hash?: \n")
while host == "":
host = input("What's the IP?\n")
expected_serial = input("and what's the serial?\n")
power_pin = int(input("What's the power pin to use?"))
filename = (expected_serial + "logfile.txt")
if exists(filename) != True:
print("Creating a logfile...")
target = open(filename, 'x')
target.close()
print("Logfile created!")
elif exists(filename) == True:
print("Appending to existing logfile.")
while True:
print("Trial #", trial_number)
# Boot the oven
localpi.write(power_pin, 1)
# Wait for a ssh connection
connected = False
while connected == False:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username = user, password = password)
print("connected!")
connected = True
except:
sleep(1)
print("connecting...")
# When connected, query the serial number and plug in events.
returned_serial = check_serial(host)
print("the serial is", returned_serial)
#read the plugins
current_plugins = check_plugins(host)
print("the plugins are ", current_plugins)
# If this is the initial run of the test, store the plug in events.
if initial_run == True:
print("this is an initial run of this test.\n")
initial_plugins = current_plugins
initial_run = False
# Wait 1 minute.
for i in range(1,90):
print(i, "...")
sleep(1)
# On five second intervals, query the serial number and plug in events. Do this 6 times
for i in range(1,2):
print("Check #", i, "checking the serial...")
check = check_serial(host)
if check in expected_serial:
print("The serial is correct")
serial_passes += 1
# If the serial is corrupted, fail
elif check != expected_serial:
print(check)
print("Wrong serial! fail:(")
serial_fails += 1
# If the serial is blank, fail
elif check == "":
print("no serial. Fail:(")
serial_fails += 1
# On the 6th query, check the plugin events is correct.
print("checking plugins...")
check = check_plugins(host)
sleep(5)
# If plugin is incorrect, fail and restart the initial plugin events check
if check == (initial_plugins + plugin_trial_number):
print("there are ", check, "plugins, which is expected. Pass!")
plugin_passes += 1
elif check != (initial_plugins + plugin_trial_number):
print("there are ", check, "plugins. There should be: ", initial_plugins + plugin_trial_number, "fail:(")
plugin_fails += 1
#reset the plugins
initial_plugins = 0
plugin_trial_number = 0
initial_run = True
else:
print("whoops. Check the initial plugin stuff")
# Close the SSH connection
ssh.close()
# Print test conditions
serial = "serial fails: ", serial_fails, "and passes: ", serial_passes
print(serial)
plugins = "plugin fails: ", plugin_fails, "and passes: ", plugin_passes
print(plugins)
# Goto 1
trial_number += 1
print("Turning off the oven!")
localpi.write(power_pin, 1)
localpi.write(power_pin, 0)
print("logging results...")
log_file = open(filename, 'a+')
log_file.write(str(serial))
log_file.write(str(plugins))
log_file.write("\n")
log_file.close()
for i in range(1,15):
print(i, "...")
sleep(1)
def check_serial(host):
user = "root"
password = "woot"
host = host
connected = False
while connected == False:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username = user, password = password)
print("connected!")
connected = True
except:
sleep(1)
print("connecting...")
stdin, stdout, stderr = ssh.exec_command("echo 'serial' | nc -q 1 -U /var/run/bosh")
output = stdout.readlines()
#print(output)
output = output[2]
#print(output)
output = output[35:49]
print(output)
ssh.close()
return(output)
def check_plugins(host):
user = "root"
password = "woot"
host = host
connected = False
while connected == False:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username = user, password = password)
print("connected!")
connected = True
except:
sleep(1)
print("connecting...")
good_output = False
while good_output == False:
stdin, stdout, stderr = ssh.exec_command("echo 'reg 262' | nc -q 2 -U /var/run/bosh")
output = stdout.readlines()
#print(output)
output = output[3]
#print(output)
output = output[38:46]
print(output, type(output)
if "meout" in output:
good_output = False
print(output)
else:
print(output)
good_output = True
clear_leading = output[0]
while clear_leading == 0:
output = output[1:]
clear_leading = output[0]
ssh.close()
return(int(output))
main()