-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer_Func.py
47 lines (31 loc) · 1.45 KB
/
Server_Func.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
import os
import subprocess
import interactions
from interactions.ext.files import command_send
import Bot_Func
async def run(ctx: interactions.CommandContext, config: dict, server: dict, script: dict, run_script: str):
if script['type'] == 'command':
run_command(server, script, run_script)
await ctx.send("Complete")
return
if script['type'] == 'fetch':
end_file: interactions.File = run_fetch(server, script, config)
await command_send(ctx, script['name'], files=end_file)
return
def run_command(server: dict, script: dict, run_script: str):
subprocess.run(["ssh", f"{server['username']}@{server['host']}", run_script],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False)
def run_fetch(server: dict, script: dict, config: dict):
file_location: str = script['location']
file_destination: str = config['general']['fileDropClientFilePath']
subprocess.run(["scp", f"{server['username']}@{server['host']}:{file_location}", file_destination])
file_name: str = os.path.basename(file_location)
file_path: str = f'{file_destination}/{file_name}'
return interactions.File(file_path)
def is_server_up(server_name: str, config: dict):
server: dict = Bot_Func.get_object_by_name(server_name, config['servers'])
response: int = os.system("ping -c 1 " + server['host'])
return response == 0