Compare commits

..

No commits in common. "5a9f23f6b6910bc2de23b369c18da81354229e81" and "2ea37fc19ee41c72261e5e8869f173e630d09e5a" have entirely different histories.

2 changed files with 33 additions and 45 deletions

View file

@ -1,6 +1,5 @@
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.
@ -15,7 +14,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(colored('\n!! Looks like you do not have any servers yet - Let\'s add one.','white','on_green',attrs=['bold'])) print('\n!! Looks like you do not have any servers yet - Let\'s add one.\n')
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.
@ -26,30 +25,32 @@ 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)
output+='ID: '+strindex+' ===\n Name:'+x['name']+'\n Address:'+x['address']+'\n Port:'+x['port']+'\n' print('ID: '+strindex+' ===')
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)
output+='ID: '+strindex+' ('+x['name']+')\n' print('ID: '+strindex+' ('+x['name']+')')
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(colored('\n=== Add Server ===','green')) print('\n=== Add Server ===')
srv_name = input(colored('Name: ','yellow')) srv_name = input('Name: ')
srv_addr = input(colored('Address: ','yellow')) srv_addr = input('Address: ')
srv_port = input(colored('Port: ','yellow')) srv_port = input('Port: ')
srv_pass = input(colored('Password: ','yellow')) srv_pass = input('Password: ')
print(colored('OK\n','green')) print('OK\n')
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.
@ -57,58 +58,46 @@ 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(colored('\n=== Remove Server ===\nPick a server to delete','red')) print('\n=== Remove Server ===\nPick a server to delete')
print(colored(list_servers(2),'light_blue')) # Lists server ID and Name only. list_servers(2) # Lists server ID and Name only.
delete = input(colored('(enter to cancel) ?: ','yellow')) delete = int(input('?: '))
if(delete==""): servers.pop(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(colored('\n=== Send commands ===','green')) print('\n=== Send commands ===')
choice = input(colored('(Enter to cancel) Single (1) All (2)?: ','yellow')) choice = int(input('Single Server (1) or All (2)?: '))
if(choice==""): if(choice==1): # We only want to interact with one server.
print(colored('Cancelled\n','red')) list_servers(2) # Show us which servers we can pick from.
return server = int(input('Server?: '))
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(colored('Input command (None to exit): ','yellow')) command = input('Input command (None to exit): ')
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(colored(servers[server]['name']+': '+response,'cyan')) print(servers[server]['name']+': '+response+'\n')
else: else:
proceed = False proceed = False
print(colored('No command was entered. Exiting.\n','red')) print('No command was entered. Exiting.\n')
elif(choice=="2"): elif(choice==2):
while(proceed): while(proceed):
command = input(colored('Input command (None to exit): ','yellow')) command = input('Input command (None to exit): ')
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(colored(x['name']+': '+response,'cyan')) print(x['name']+': '+response+'\n')
else: else:
proceed = False proceed = False
print(colored('No command was entered. Exiting.\n','red')) print('No command was entered. Exiting.\n')
# The main menu. # The main menu.
def initial_choice(): def initial_choice():
print(colored('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit','green')) choose_func = input('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit\n?: ')
choose_func = input(colored("?: ",'yellow'))
match choose_func: match choose_func:
case "1": case "1":
@ -118,7 +107,7 @@ def initial_choice():
pop_server() pop_server()
initial_choice() initial_choice()
case "3": case "3":
print(colored(list_servers(),'light_blue')) list_servers()
initial_choice() initial_choice()
case "4": case "4":
send_commands() send_commands()
@ -126,7 +115,7 @@ def initial_choice():
case "5": case "5":
exit() exit()
case _: case _:
print(colored("\n!! Invalid input\n",'red')) print("\n!! Invalid input\n")
initial_choice() initial_choice()
load_servers() load_servers()

View file

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