Compare commits

..

2 commits

Author SHA1 Message Date
5a9f23f6b6
Options to cancel 2024-02-04 18:44:43 -05:00
13b7594b8a
Colors! 2024-02-04 17:41:23 -05:00
2 changed files with 45 additions and 33 deletions

View file

@ -1,5 +1,6 @@
import os.path import os.path
import json import json
from termcolor import colored, cprint
from rcon.source import Client from rcon.source import Client
# Declare variables that are needed everywhere. # Declare variables that are needed everywhere.
@ -14,7 +15,7 @@ def load_servers():
with open(servers_file,'r') as openfile: with open(servers_file,'r') as openfile:
servers = json.load(openfile) servers = json.load(openfile)
else: else:
print('\n!! Looks like you do not have any servers yet - Let\'s add one.\n') print(colored('\n!! Looks like you do not have any servers yet - Let\'s add one.','white','on_green',attrs=['bold']))
create_server() create_server()
# Write the contents to the servers variable to the servers_file. # Write the contents to the servers variable to the servers_file.
@ -25,32 +26,30 @@ def write_servers():
# Lists servers, default action is to list all information minus password. Second option lists only ID and name. # 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. def list_servers(mode=int(1)): # make sure a default is set.
output = ""
if(mode==1): if(mode==1):
index = int(0) index = int(0)
for x in servers: # for each dict in the servers array as x. for x in servers: # for each dict in the servers array as x.
strindex = str(index) strindex = str(index)
print('ID: '+strindex+' ===') output+='ID: '+strindex+' ===\n Name:'+x['name']+'\n Address:'+x['address']+'\n Port:'+x['port']+'\n'
print(' Name:'+x['name'])
print(' Address:'+x['address'])
print(' Port:'+x['port'])
print(' Password: **********\n')
index += 1 index += 1
elif(mode==2): elif(mode==2):
index = int(0) index = int(0)
for x in servers: for x in servers:
strindex = str(index) strindex = str(index)
print('ID: '+strindex+' ('+x['name']+')') output+='ID: '+strindex+' ('+x['name']+')\n'
index += 1 index += 1
return output
# Creates new server dicts in the server array. # Creates new server dicts in the server array.
def create_server(): def create_server():
global servers # Global so that we can write to it. global servers # Global so that we can write to it.
print('\n=== Add Server ===') print(colored('\n=== Add Server ===','green'))
srv_name = input('Name: ') srv_name = input(colored('Name: ','yellow'))
srv_addr = input('Address: ') srv_addr = input(colored('Address: ','yellow'))
srv_port = input('Port: ') srv_port = input(colored('Port: ','yellow'))
srv_pass = input('Password: ') srv_pass = input(colored('Password: ','yellow'))
print('OK\n') print(colored('OK\n','green'))
new_server = [{"name":srv_name,"address":srv_addr,"port":srv_port,"password":srv_pass}] # Build new dict in array. 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. servers = servers + new_server # Append new array to existing array.
write_servers() # Write to the file. write_servers() # Write to the file.
@ -58,46 +57,58 @@ def create_server():
# Used to delete servers from the array and write. # Used to delete servers from the array and write.
def pop_server(): def pop_server():
global servers # Global so we can write global servers # Global so we can write
print('\n=== Remove Server ===\nPick a server to delete') print(colored('\n=== Remove Server ===\nPick a server to delete','red'))
list_servers(2) # Lists server ID and Name only. print(colored(list_servers(2),'light_blue')) # Lists server ID and Name only.
delete = int(input('?: ')) delete = input(colored('(enter to cancel) ?: ','yellow'))
servers.pop(delete) if(delete==""):
print(colored('Cancelled\n','red'))
return
servers.pop(int(delete))
print(colored('Deleted\n','light_blue'))
write_servers() write_servers()
# Let's send some commands to the servers! # Let's send some commands to the servers!
def send_commands(): def send_commands():
proceed=True # Used in while loops to determine if we still want to send more commands. proceed=True # Used in while loops to determine if we still want to send more commands.
print('\n=== Send commands ===') print(colored('\n=== Send commands ===','green'))
choice = int(input('Single Server (1) or All (2)?: ')) choice = input(colored('(Enter to cancel) Single (1) All (2)?: ','yellow'))
if(choice==1): # We only want to interact with one server. if(choice==""):
list_servers(2) # Show us which servers we can pick from. print(colored('Cancelled\n','red'))
server = int(input('Server?: ')) return
if(choice=="1"): # We only want to interact with one server.
print(colored(list_servers(2),'light_blue')) # Show us which servers we can pick from.
server = input(colored('(Enter to cancel) Server?: ','yellow'))
if(server==""):
print(colored('Cancelled\n','red'))
return
server=int(server)
while(proceed): while(proceed):
command = input('Input command (None to exit): ') command = input(colored('Input command (None to exit): ','yellow'))
if(command!=""): if(command!=""):
with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client: with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client:
response = client.run(command) response = client.run(command)
print(servers[server]['name']+': '+response+'\n') print(colored(servers[server]['name']+': '+response,'cyan'))
else: else:
proceed = False proceed = False
print('No command was entered. Exiting.\n') print(colored('No command was entered. Exiting.\n','red'))
elif(choice==2): elif(choice=="2"):
while(proceed): while(proceed):
command = input('Input command (None to exit): ') command = input(colored('Input command (None to exit): ','yellow'))
for x in servers: # Run for each server. for x in servers: # Run for each server.
if(command!=""): if(command!=""):
with Client(x['address'], int(x['port']), passwd=x['password']) as client: with Client(x['address'], int(x['port']), passwd=x['password']) as client:
response = client.run(command) response = client.run(command)
print(x['name']+': '+response+'\n') print(colored(x['name']+': '+response,'cyan'))
else: else:
proceed = False proceed = False
print('No command was entered. Exiting.\n') print(colored('No command was entered. Exiting.\n','red'))
# The main menu. # The main menu.
def initial_choice(): def initial_choice():
choose_func = input('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit\n?: ') print(colored('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit','green'))
choose_func = input(colored("?: ",'yellow'))
match choose_func: match choose_func:
case "1": case "1":
@ -107,7 +118,7 @@ def initial_choice():
pop_server() pop_server()
initial_choice() initial_choice()
case "3": case "3":
list_servers() print(colored(list_servers(),'light_blue'))
initial_choice() initial_choice()
case "4": case "4":
send_commands() send_commands()
@ -115,7 +126,7 @@ def initial_choice():
case "5": case "5":
exit() exit()
case _: case _:
print("\n!! Invalid input\n") print(colored("\n!! Invalid input\n",'red'))
initial_choice() initial_choice()
load_servers() load_servers()

View file

@ -1 +1,2 @@
rcon==2.4.4 rcon==2.4.4
termcolor==2.4.0