rcon-tool/mc_control.py

133 lines
5.3 KiB
Python
Raw Normal View History

2024-02-04 16:18:33 -05:00
import os.path
import json
2024-02-04 17:41:23 -05:00
from termcolor import colored, cprint
2024-02-04 16:18:33 -05:00
from rcon.source import Client
2024-02-04 16:43:12 -05:00
# Declare variables that are needed everywhere.
2024-02-04 16:18:33 -05:00
servers_file = "./.midnight_servers.json"
servers = []
# Loads servers in from the servers_file, if it does not exist, trigger adding first server via create_servers.
2024-02-04 16:18:33 -05:00
def load_servers():
global servers
if os.path.isfile(servers_file):
with open(servers_file,'r') as openfile:
servers = json.load(openfile)
else:
2024-02-04 17:41:23 -05:00
print(colored('\n!! Looks like you do not have any servers yet - Let\'s add one.','white','on_green',attrs=['bold']))
create_server()
2024-02-04 16:18:33 -05:00
# Write the contents to the servers variable to the servers_file.
2024-02-04 16:18:33 -05:00
def write_servers():
json_obj = json.dumps(servers,indent=4) # Add indentation when dumping to string.
2024-02-04 16:18:33 -05:00
with open(servers_file, "w") as outfile:
outfile.write(json_obj)
# Lists servers, default action is to list all information minus password. Second option lists only ID and name.
def list_servers(mode=int(1)): # make sure a default is set.
2024-02-04 17:41:23 -05:00
output = ""
2024-02-04 16:18:33 -05:00
if(mode==1):
index = int(0)
for x in servers: # for each dict in the servers array as x.
2024-02-04 16:18:33 -05:00
strindex = str(index)
2024-02-04 17:41:23 -05:00
output+='ID: '+strindex+' ===\n Name:'+x['name']+'\n Address:'+x['address']+'\n Port:'+x['port']+'\n'
2024-02-04 16:18:33 -05:00
index += 1
elif(mode==2):
index = int(0)
for x in servers:
strindex = str(index)
2024-02-04 17:41:23 -05:00
output+='ID: '+strindex+' ('+x['name']+')\n'
2024-02-04 16:18:33 -05:00
index += 1
2024-02-04 17:41:23 -05:00
return output
2024-02-04 16:18:33 -05:00
# Creates new server dicts in the server array.
def create_server():
global servers # Global so that we can write to it.
2024-02-04 17:41:23 -05:00
print(colored('\n=== Add Server ===','green'))
srv_name = input(colored('Name: ','yellow'))
srv_addr = input(colored('Address: ','yellow'))
srv_port = input(colored('Port: ','yellow'))
srv_pass = input(colored('Password: ','yellow'))
print(colored('OK\n','green'))
new_server = [{"name":srv_name,"address":srv_addr,"port":srv_port,"password":srv_pass}] # Build new dict in array.
servers = servers + new_server # Append new array to existing array.
write_servers() # Write to the file.
2024-02-04 16:18:33 -05:00
2024-02-04 17:01:46 -05:00
# Used to delete servers from the array and write.
2024-02-04 16:18:33 -05:00
def pop_server():
2024-02-04 17:01:46 -05:00
global servers # Global so we can write
2024-02-04 17:41:23 -05:00
print(colored('\n=== Remove Server ===\nPick a server to delete','red'))
print(colored(list_servers(2),'light_blue')) # Lists server ID and Name only.
2024-02-04 18:44:43 -05:00
delete = input(colored('(enter to cancel) ?: ','yellow'))
if(delete==""):
print(colored('Cancelled\n','red'))
return
servers.pop(int(delete))
print(colored('Deleted\n','light_blue'))
2024-02-04 16:18:33 -05:00
write_servers()
2024-02-04 17:01:46 -05:00
# Let's send some commands to the servers!
2024-02-04 16:18:33 -05:00
def send_commands():
2024-02-04 17:01:46 -05:00
proceed=True # Used in while loops to determine if we still want to send more commands.
2024-02-04 17:41:23 -05:00
print(colored('\n=== Send commands ===','green'))
2024-02-04 18:44:43 -05:00
choice = input(colored('(Enter to cancel) Single (1) All (2)?: ','yellow'))
if(choice==""):
print(colored('Cancelled\n','red'))
return
if(choice=="1"): # We only want to interact with one server.
2024-02-04 17:41:23 -05:00
print(colored(list_servers(2),'light_blue')) # Show us which servers we can pick from.
2024-02-04 18:44:43 -05:00
server = input(colored('(Enter to cancel) Server?: ','yellow'))
if(server==""):
print(colored('Cancelled\n','red'))
return
server=int(server)
2024-02-04 16:18:33 -05:00
while(proceed):
2024-02-04 17:41:23 -05:00
command = input(colored('Input command (None to exit): ','yellow'))
2024-02-04 16:18:33 -05:00
if(command!=""):
with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client:
response = client.run(command)
2024-02-04 17:41:23 -05:00
print(colored(servers[server]['name']+': '+response,'cyan'))
2024-02-04 16:18:33 -05:00
else:
proceed = False
2024-02-04 17:41:23 -05:00
print(colored('No command was entered. Exiting.\n','red'))
2024-02-04 18:44:43 -05:00
elif(choice=="2"):
while(proceed):
2024-02-04 17:41:23 -05:00
command = input(colored('Input command (None to exit): ','yellow'))
2024-02-04 17:01:46 -05:00
for x in servers: # Run for each server.
if(command!=""):
with Client(x['address'], int(x['port']), passwd=x['password']) as client:
response = client.run(command)
2024-02-04 17:41:23 -05:00
print(colored(x['name']+': '+response,'cyan'))
else:
proceed = False
2024-02-04 17:41:23 -05:00
print(colored('No command was entered. Exiting.\n','red'))
2024-02-04 16:18:33 -05:00
2024-02-04 17:01:46 -05:00
# The main menu.
2024-02-04 16:18:33 -05:00
def initial_choice():
2024-02-04 17:41:23 -05:00
print(colored('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit','green'))
choose_func = input(colored("?: ",'yellow'))
2024-02-04 16:18:33 -05:00
match choose_func:
case "1":
create_server()
initial_choice()
2024-02-04 16:18:33 -05:00
case "2":
pop_server()
2024-02-04 17:01:46 -05:00
initial_choice()
2024-02-04 16:18:33 -05:00
case "3":
2024-02-04 17:41:23 -05:00
print(colored(list_servers(),'light_blue'))
initial_choice()
2024-02-04 16:18:33 -05:00
case "4":
send_commands()
2024-02-04 17:01:46 -05:00
initial_choice()
2024-02-04 16:18:33 -05:00
case "5":
exit()
case _:
2024-02-04 17:41:23 -05:00
print(colored("\n!! Invalid input\n",'red'))
2024-02-04 16:18:33 -05:00
initial_choice()
load_servers()
2024-02-04 16:41:47 -05:00
initial_choice()