-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
40 lines (34 loc) · 1.11 KB
/
client.py
File metadata and controls
40 lines (34 loc) · 1.11 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
import pty, os, sys, select, fcntl, termios, struct, re
def set_winsize(fd, rows, cols):
winsize = struct.pack("HHHH", rows, cols, 0, 0)
fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)
master, slave = pty.openpty()
set_winsize(master, 24, 80)
pid = os.fork()
if pid == 0:
os.setsid()
os.dup2(slave, 0)
os.dup2(slave, 1)
os.dup2(slave, 2)
os.close(master)
os.close(slave)
os.execve('/bin/bash', ['/bin/bash'], os.environ)
sys.exit(0)
os.close(slave)
esc_pattern = re.compile(b'\x1b\[8;(\d+);(\d+)t')
while True:
r, _, _ = select.select([sys.stdin.fileno(), master], [], [])
if sys.stdin.fileno() in r:
data = os.read(sys.stdin.fileno(), 1024)
if not data: break
while True:
match = esc_pattern.search(data)
if not match: break
set_winsize(master, int(match.group(1)), int(match.group(2)))
data = data[:match.start()] + data[match.end():]
os.write(master, data)
if master in r:
data = os.read(master, 1024)
if not data: break
os.write(sys.stdout.fileno(), data)
os.waitpid(pid, 0)