2021-04-13 20:06:16 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
|
|
oldexcepthook = sys.excepthook
|
|
|
|
def newexcepthook(type,value,traceback):
|
|
|
|
oldexcepthook(type,value,traceback)
|
2021-04-14 01:12:13 +00:00
|
|
|
#input("Press ENTER to quit.")
|
2021-04-13 20:06:16 +00:00
|
|
|
sys.excepthook = newexcepthook
|
|
|
|
|
|
|
|
import os
|
|
|
|
p = os.path.join
|
|
|
|
pUp = os.path.dirname
|
|
|
|
s = False
|
|
|
|
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
|
|
|
|
s = os.path.realpath(sys.executable)
|
|
|
|
else:
|
|
|
|
s = os.path.realpath(__file__)
|
|
|
|
sp = pUp(s)
|
|
|
|
|
|
|
|
# script start
|
2021-04-13 18:33:16 +00:00
|
|
|
import subprocess
|
|
|
|
import socket
|
2021-04-13 20:06:16 +00:00
|
|
|
|
2021-04-21 11:16:28 +00:00
|
|
|
def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
2021-12-19 10:43:33 +00:00
|
|
|
bufferSize = 8192 # buffer size in bytes
|
2021-04-14 16:52:18 +00:00
|
|
|
timeout = 15 # timeout in seconds
|
2021-04-13 18:33:16 +00:00
|
|
|
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
2021-12-16 07:39:02 +00:00
|
|
|
unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) # Make unbuffered stdout
|
2021-04-14 20:03:53 +00:00
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
def listToCommand(lst):
|
|
|
|
cmd = ""
|
|
|
|
for arg in lst:
|
|
|
|
arg = arg.replace("\\","\\\\")
|
|
|
|
arg = arg.replace(",","\\,")
|
|
|
|
cmd += arg + ","
|
|
|
|
|
|
|
|
return cmd[:-1]
|
|
|
|
|
2021-12-15 20:24:35 +00:00
|
|
|
def commandToList(cmd):
|
|
|
|
args = []
|
|
|
|
cArg = ""
|
|
|
|
escape = False
|
|
|
|
for letter in cmd:
|
|
|
|
if escape == True:
|
|
|
|
cArg += letter
|
|
|
|
escape = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
if letter == "\\":
|
|
|
|
escape = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
if letter == ",":
|
|
|
|
if cArg == "": continue
|
|
|
|
args.append(cArg)
|
|
|
|
cArg = ""
|
|
|
|
continue
|
|
|
|
|
|
|
|
cArg += letter
|
|
|
|
|
|
|
|
args.append(cArg)
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
def makePayload(lst):
|
|
|
|
cmdText = listToCommand(lst)
|
|
|
|
cmdBytes = cmdText.encode("utf-8")
|
2021-12-15 20:24:35 +00:00
|
|
|
cmdBytes += b" " * (1024 - len(cmdBytes))
|
2021-04-13 23:09:34 +00:00
|
|
|
return cmdBytes
|
|
|
|
|
2021-04-21 12:02:39 +00:00
|
|
|
def stringToAddressTuple(addr):
|
|
|
|
rtn = addr.rsplit(":",1)
|
|
|
|
rtn[1] = int(rtn[1])
|
|
|
|
rtn = tuple(rtn)
|
|
|
|
return rtn
|
|
|
|
|
2021-04-13 20:06:16 +00:00
|
|
|
def main():
|
2021-04-14 15:23:36 +00:00
|
|
|
global serverAddr
|
2021-04-21 12:02:39 +00:00
|
|
|
serverAddr = stringToAddressTuple(sys.argv[1])
|
2021-12-15 20:24:35 +00:00
|
|
|
global bufferSize
|
2021-04-21 12:02:39 +00:00
|
|
|
|
2021-04-21 11:16:28 +00:00
|
|
|
eprint("Connecting to server...")
|
2021-04-14 16:52:18 +00:00
|
|
|
connection.settimeout(timeout)
|
2021-04-13 20:06:16 +00:00
|
|
|
connection.connect(serverAddr)
|
2021-04-21 11:16:28 +00:00
|
|
|
eprint("Sending payload...")
|
2021-04-14 15:23:36 +00:00
|
|
|
connection.sendall(makePayload(sys.argv[2:]))
|
2021-12-15 20:24:35 +00:00
|
|
|
eprint("Receiving payload...")
|
|
|
|
args = commandToList(connection.recv(1024).decode("utf-8").rstrip(" "))
|
2021-04-13 23:09:34 +00:00
|
|
|
|
2021-04-14 15:23:36 +00:00
|
|
|
if sys.argv[2] == "watch":
|
2021-12-15 20:24:35 +00:00
|
|
|
bufferSize = int(args[0])
|
2021-04-14 20:03:53 +00:00
|
|
|
try:
|
2021-04-21 11:16:28 +00:00
|
|
|
eprint("Receiving data...")
|
2021-04-14 20:03:53 +00:00
|
|
|
while True:
|
|
|
|
data = connection.recv(bufferSize)
|
|
|
|
if data == b"": return
|
2021-12-16 07:39:02 +00:00
|
|
|
unbufferedStdout.write(data)
|
2021-04-14 20:03:53 +00:00
|
|
|
except:
|
|
|
|
connection.close()
|
|
|
|
raise
|
2021-04-13 20:06:16 +00:00
|
|
|
|
2021-04-14 15:23:36 +00:00
|
|
|
if sys.argv[2] == "broadcast":
|
2021-12-15 20:24:35 +00:00
|
|
|
bufferSize = int(sys.argv[3])
|
2021-04-14 20:03:53 +00:00
|
|
|
try:
|
2021-04-21 11:16:28 +00:00
|
|
|
eprint("Sending data...")
|
2021-04-14 20:03:53 +00:00
|
|
|
while True:
|
|
|
|
data = sys.stdin.buffer.read(bufferSize)
|
2021-12-15 20:24:35 +00:00
|
|
|
connection.sendall(data)
|
2021-04-14 20:03:53 +00:00
|
|
|
except:
|
|
|
|
connection.close()
|
|
|
|
raise
|
2021-04-13 18:33:16 +00:00
|
|
|
|
2021-04-13 20:06:16 +00:00
|
|
|
if __name__ == '__main__':
|
2021-12-16 07:39:02 +00:00
|
|
|
main()
|