forked from rockus/VE.direct-OSX-Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_miner_data.py
374 lines (301 loc) · 13.9 KB
/
get_miner_data.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import requests
import paramiko
import json
import subprocess
import time
#from get_miner_data import get_miner_power
def get_json_output(ip_address, port, command):
try:
full_command = f'echo \'{json.dumps(command)}\' | nc {ip_address} {port} | jq .'
output = subprocess.check_output(full_command, shell=True, stderr=subprocess.STDOUT, text=True)
try:
parsed_output = json.loads(output)
return parsed_output
except json.JSONDecodeError as json_error:
print("Error parsing JSON:", json_error)
return None
except subprocess.CalledProcessError as e:
print("Error executing command:", e)
return None
#from get_miner_data import get_miner_power
#from get_pannel_data import
import subprocess
"""
def check_paramiko_installed():
try:
subprocess.run(["pip3", "show", "paramiko"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False
def install_paramiko():
if not check_paramiko_installed():
try:
subprocess.run(["pip3", "install", "paramiko"], check=True)
print("paramiko installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing paramiko:", e)
else:
print("paramiko is already installed.")
# Call the function to install paramiko if needed
install_paramiko()
"""
def load_ssh_config():
with open("ssh_config.json", "r") as config_file:
return json.load(config_file)
def print_output(command_output):
if command_output != None:
if command_output.strip():
print("Command Output:")
print(command_output)
def execute_ssh_command(ip_address, username, password, command):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect(ip_address, username=username, password=password)
stdin, stdout, stderr = ssh_client.exec_command(command)
return stdout.read().decode()
except paramiko.AuthenticationException:
return "Authentication failed."
except paramiko.SSHException as e:
return f"SSH error: {str(e)}"
finally:
ssh_client.close()
def stop_miner(ip_address, ssh_username, ssh_password):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect(ip_address, username=ssh_username, password=ssh_password)
ssh_command = "systemctl stop bosminer" # Stop the miner service
stdin, stdout, stderr = ssh_client.exec_command(ssh_command)
print_output(stdout.read().decode("utf-8"))
except paramiko.AuthenticationException as auth_ex:
print("Authentication error:", auth_ex)
except paramiko.SSHException as ssh_ex:
print("SSH connection error:", ssh_ex)
finally:
ssh_client.close()
def running_attempt(ip_address, port):
command = {"command": "summary"} # Use the appropriate JSON command to check miner status
output = get_json_output(ip_address, port, command)
# print("Attempt", current_attempt + 1)
if output:
status_list = output.get("STATUS", [])
for status_entry in status_list:
if "STATUS" in status_entry and status_entry["STATUS"] == "S":
return True # Miner is running
def check_running(ip_address, port):
max_attempts = 10
current_attempt = 0
sleep_duration = 5 # in seconds
running_attempt(ip_address, port)
"""
while current_attempt < max_attempts:
running_attempt()
current_attempt += 1
if current_attempt < max_attempts:
# print("Retrying in", sleep_duration, "seconds...")
time.sleep(sleep_duration)
"""
# If no valid output is obtained after max_attempts, raise an exception
# raise Exception("Miner is not running after multiple attempts")
return False
"""
def check_paramiko_installed():
try:
subprocess.run(["pip3", "show", "paramiko"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False
def install_paramiko():
if not check_paramiko_installed():
try:
subprocess.run(["pip3", "install", "paramiko"], check=True)
print("paramiko installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing paramiko:", e)
else:
print("paramiko is already installed.")
# Call the function to install paramiko if needed
install_paramiko()
"""
def load_ssh_config():
with open("ssh_config.json", "r") as config_file:
return json.load(config_file)
def print_output(command_output):
if command_output != None:
if command_output.strip():
print("Command Output:")
print(command_output)
def execute_ssh_command(ip_address, username, password, command):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect(ip_address, username=username, password=password)
stdin, stdout, stderr = ssh_client.exec_command(command)
return stdout.read().decode()
except paramiko.AuthenticationException:
return "Authentication failed."
except paramiko.SSHException as e:
return f"SSH error: {str(e)}"
finally:
ssh_client.close()
def stop_miner(ip_address, ssh_username, ssh_password):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect(ip_address, username=ssh_username, password=ssh_password)
ssh_command = "systemctl stop bosminer" # Stop the miner service
stdin, stdout, stderr = ssh_client.exec_command(ssh_command)
print_output(stdout.read().decode("utf-8"))
except paramiko.AuthenticationException as auth_ex:
print("Authentication error:", auth_ex)
except paramiko.SSHException as ssh_ex:
print("SSH connection error:", ssh_ex)
finally:
ssh_client.close()
# curl 'http://151.216.205.165/graphql' -X POST -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' -H 'Accept: */*' -H 'Accept-Language: en' -H 'Accept-Encoding: gzip, deflate' -H 'content-type: application/json' -H 'Origin: http://151.216.205.165' -H 'Connection: keep-alive' -H 'Referer: http://151.216.205.165/' -H 'Cookie: session_id=ab29f38a41d1c6649ef004cfb935596d' --data-raw '{"query":"mutation {\n bosminer {\n start {\n ... on BosminerError {\n message\n __typename\n }\n __typename\n }\n __typename\n }\n}\n","variables":{}}'
# {"data":{"bosminer":{"__typename":"BosminerMutation","start":{"__typename":"VoidResult"}}}}
def start_miner(miner_ip_address):
curl_command = (
f"curl 'http://{miner_ip_address}/graphql' -X POST "
"-H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' "
"-H 'Accept: */*' -H 'Accept-Language: en' -H 'Accept-Encoding: gzip, deflate' "
"-H 'content-type: application/json' "
f"-H 'Origin: http://{miner_ip_address}' "
"-H 'Connection: keep-alive' "
f"-H 'Referer: http://{miner_ip_address}/' "
"-H 'Cookie: session_id=ab29f38a41d1c6649ef004cfb935596d' "
"--data-raw '{\"query\":\"mutation {\\n bosminer {\\n start {\\n ... on BosminerError {\\n message\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n}\\n\",\"variables\":{}}'"
)
try:
result = subprocess.run(curl_command, shell=True, capture_output=True, text=True)
print("Curl Output:", result.stdout)
except Exception as e:
print("Error:", e)
def is_miner_fully_booted(ip_address, port):
max_attempts = 10
current_attempt = 0
while current_attempt < max_attempts:
json_output = get_json_output(ip_address, port, command={"command": "summary"})
if json_output and "STATUS" in json_output and "SUMMARY" in json_output:
status = json_output["STATUS"][0]["STATUS"]
summary = json_output["SUMMARY"][0]["SUMMARY"]
# Check if the status and summary indicate that the miner is fully booted
if status == "S" and summary == "Booted":
return True
# Wait for a few seconds before the next attempt
if current_attempt == 0:
print("waiting for miner to boot")
else:
print("still waiting for miner to boot, attempt: ", current_attempt)
time.sleep(5)
current_attempt += 1
# If max_attempts are reached and the miner is not fully booted, return False
return False
def get_miner_power(ip_address, port, depth=0):
json_output = get_json_output(ip_address, port, command={"command": "tunerstatus"})
if json_output:
try:
approximate_power_consumption = json_output['TUNERSTATUS'][0]['ApproximateChainPowerConsumption']
approximate_miner_power_consumption = json_output['TUNERSTATUS'][0]['ApproximateMinerPowerConsumption']
power = {
"chain power": approximate_power_consumption,
"miner power": approximate_miner_power_consumption
}
return power
except KeyError as e:
# print("KeyError:", e)
# print("JSON Output:", json_output) # Print the content of the JSON output for debuggingf
# Read SSH credentials from ssh_config.json
with open('ssh_config.json') as config_file:
ssh_config = json.load(config_file)
ip_address = ssh_config['miner_ip_address']
ssh_username = ssh_config['ssh_username']
ssh_password = ssh_config['ssh_password']
if check_running(ip_address, port):
print("The miner is running, and we still got this error. Weird...")
else:
print("The miner is not running")
"""
restart(ip_address, ssh_username, ssh_password)
if depth > 0:
return None
if is_miner_fully_booted(ip_address, port):
return get_miner_power(ip_address, port, depth = depth + 1)
# Your error handling logic here
# For example, you can return None or raise a custom exception
"""
return None # Returning None to indicate failure
"""
def check_paramiko_installed():
try:
subprocess.run(["pip3", "show", "paramiko"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False
def install_paramiko():
if not check_paramiko_installed():
try:
subprocess.run(["pip3", "install", "paramiko"], check=True)
print("paramiko installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing paramiko:", e)
else:
print("paramiko is already installed.")
# Call the function to install paramiko if needed
install_paramiko()
"""
def load_ssh_config():
with open("ssh_config.json", "r") as config_file:
return json.load(config_file)
def print_output(command_output):
if command_output != None:
if command_output.strip():
print("Command Output:")
print(command_output)
def execute_ssh_command(ip_address, username, password, command):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect(ip_address, username=username, password=password)
stdin, stdout, stderr = ssh_client.exec_command(command)
return stdout.read().decode()
except paramiko.AuthenticationException:
return "Authentication failed."
except paramiko.SSHException as e:
return f"SSH error: {str(e)}"
finally:
ssh_client.close()
def restart(ip_address, ssh_username, ssh_password):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip_address, username=ssh_username, password=ssh_password)
# Execute the reboot command
stdin, stdout, stderr = client.exec_command('reboot')
print("Miner restarted successfully.")
# Close the SSH connection
client.close()
except paramiko.ssh_exception.AuthenticationException:
print("Authentication failed. Check the username and password.")
except paramiko.ssh_exception.SSHException as e:
print("SSH error:", e)
except Exception as e:
print("Error restarting miner:", e)
def stop_miner(miner_ip_address):
curl_command = (
f"curl 'http://{miner_ip_address}/graphql' -X POST "
"-H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' "
"-H 'Accept: */*' -H 'Accept-Language: en' -H 'Accept-Encoding: gzip, deflate' "
"-H 'content-type: application/json' "
f"-H 'Origin: http://{miner_ip_address}' "
"-H 'Connection: keep-alive' "
f"-H 'Referer: http://{miner_ip_address}/' "
"-H 'Cookie: session_id=ab29f38a41d1c6649ef004cfb935596d' "
"--data-raw '{\"query\":\"mutation {\\n bosminer {\\n stop {\\n ... on VoidResult {\\n void\\n __typename\\n }\\n ... on BosminerError {\\n message\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n}\\n\",\"variables\":{}}'"
)
try:
result = subprocess.run(curl_command, shell=True, capture_output=True, text=True)
print("Curl Output:", result.stdout)
except Exception as e:
print("Error:", e)