From 41675a53616e3e90f0936f4232363108af8a3fd5 Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:18:33 -0500 Subject: [PATCH] Initial concept --- mc_control.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 mc_control.py diff --git a/mc_control.py b/mc_control.py new file mode 100644 index 0000000..387a4f9 --- /dev/null +++ b/mc_control.py @@ -0,0 +1,120 @@ +import os.path +import json +from rcon.source import Client + +servers_file = "./.midnight_servers.json" +servers = [] +isfirstrun = False + +def load_servers(): + global servers + if os.path.isfile(servers_file): + with open(servers_file,'r') as openfile: + servers = json.load(openfile) + else: + create_server(True) + +def write_servers(): + json_obj = json.dumps(servers,indent=4) + with open(servers_file, "w") as outfile: + outfile.write(json_obj) + +def list_servers(mode=int(1)): + if(mode==1): + index = int(0) + for x in servers: + strindex = str(index) + print('ID: '+strindex+' ===') + print(' Name:'+x['name']) + print(' Address:'+x['address']) + print(' Port:'+x['port']) + print(' Password: **********\n') + index += 1 + initial_choice() + elif(mode==2): + index = int(0) + for x in servers: + strindex = str(index) + print('ID: '+strindex+' ('+x['name']+')') + index += 1 + +def create_server(isfirstrun=False): + global servers + if(isfirstrun): + print('!! Looks like you do not have any servers yet - Let\'s add one.\n') + + print('=== Add Server ===') + srv_name = input('Name: ') + srv_addr = input('Address: ') + srv_port = input('Port: ') + srv_pass = input('Password: ') + print('OK\n') + new_server = [{"name":srv_name,"address":srv_addr,"port":srv_port,"password":srv_pass}] + + if(isfirstrun): + json_obj = json.dumps(new_server,indent=4) + with open(servers_file, "w") as outfile: + outfile.write(json_obj) + else: + servers = servers + new_server + write_servers() + initial_choice() + +def pop_server(): + global servers + print('=== Remove Server ===\nPick a server to delete') + list_servers(2) + delete = int(input('?: ')) + + servers.pop(delete) + + write_servers() + + initial_choice() + +def send_commands(): + proceed=True + print('=== Send commands ===') + choice = int(input('Single Server (1) or All (2)?: ')) + if(choice==1): + list_servers(2) + server = int(input('Server?: ')) + while(proceed): + command = input('Input command: ') + if(command!=""): + with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client: + response = client.run(command) + + print(servers[server]['name']+': '+response) + else: + proceed = False + print('No command was entered. Exiting.\n') + initial_choice() + elif(choice==2): + print('Would do every server') + +def initial_choice(): + choose_func = input('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit\n?: ') + + match choose_func: + case "1": + create_server() + case "2": + pop_server() + case "3": + list_servers() + case "4": + send_commands() + case "5": + exit() + case _: + print("\n!! Invalid input\n") + initial_choice() + +load_servers() +initial_choice() + +#with Client('10.100.4.60', 30580, passwd='orrQLZvXH6jgD@wt') as client: +# response = client.run('tps') +# +#print(response) \ No newline at end of file