-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcsctl.py
99 lines (86 loc) · 3.05 KB
/
csctl.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sys
import telnetlib
import psutil
import signal
import emoji
import binascii
from termcolor import colored
from time import sleep
from os import path
# Config
tn_host = "127.0.0.1"
tn_port = "2121"
cfg_path = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Counter-Strike Global Offensive\\csgo\\cfg\\"
def signal_handler(signal, frame):
print("\nquitting...")
sys.exit(0)
# Print with emojis
def print_e(message):
print(colored(emoji.emojize(message, use_aliases=True), attrs=['bold']))
# List PIDs of processes matching processName
def processExists(processName):
procList = []
for proc in psutil.process_iter(['name']):
if proc.info['name'].lower() == processName.lower():
return True
return False
# Runs commands on the csgo console
def run(txn, command):
cmd_s = command + "\n"
txn.write(cmd_s.encode('utf-8'))
sleep(0.005)
signal.signal(signal.SIGINT, signal_handler)
def main():
if (len(sys.argv) > 1):
if (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
print(colored("Run with no arguments to initiate and connect to csgo", attrs=['bold']))
print(colored("Make sure you set up csgo to receive connections with this launch option: -netconport "+str(tn_port), attrs=['bold']))
# Make sure cs:go is running before trying to connect
if not processExists("csgo.exe"):
print_e(":information: Waiting for csgo to start... ")
while not processExists("csgo.exe"):
sleep(0.25)
sleep(10)
# Initialize csgo telnet connection
print_e(":information: Trying " + tn_host + ":" + tn_port + "...")
try:
tn = telnetlib.Telnet(tn_host, tn_port)
except ConnectionRefusedError:
# Retry in 30 seconds
sleep(30)
pass
try:
tn = telnetlib.Telnet(tn_host, tn_port)
except ConnectionRefusedError:
print_e(":x: Connection refused. Make sure you have the following launch option set:")
print(colored(" -netconport "+str(tn_port), attrs=['bold']))
sys.exit(1)
tn.write(b"echo CSCTL Active, use exectn instruction_file to execute commands\n")
tn.read_until(b"commands")
print_e(":heavy_check_mark: Successfully Connected")
while True:
print_e(":information: Listening for command from console")
# Capture console output until we encounter our exec string
tn.read_until(b"exectn ")
# Parse output and get filename
instr_fname = tn.read_eager().decode("utf-8")
instr_fname = instr_fname.replace('\n', ' ').replace('\r', '')
if (path.exists(instr_fname) or path.exists(cfg_path+instr_fname)):
# Execute instructions from file
if (path.exists(cfg_path+instr_fname)):
instr_fname = cfg_path+instr_fname
for line in open(instr_fname, "r"):
sspl = line.split(" ")
if (sspl[0] == 'delay'):
delay_time = sspl[1].replace('\n', ' ').replace('\r', '')
print_e(":sparkle: sleeping for "+str(delay_time)+" seconds")
sleep(float(sspl[1]))
else:
line = line.replace('\n', ' ').replace('\r', '')
print_e(":sparkle: exec "+str(line))
run(tn, str(line))
else:
run(tn, "echo File not found: "+str(instr_fname))
print_e(":heavy_check_mark: Instructions complete")
if __name__== "__main__":
main()