-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.py
executable file
·61 lines (51 loc) · 1.79 KB
/
ssh.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python2
import sys
import os
import pty
import tty
import re
import select
from StringIO import StringIO
input_line = re.compile(r"\$ ?$")
pid, child_fd = pty.fork()
if pid == 0:
# self.child_fd = sys.stdout.fileno() # used by setwinsize()
# self.setwinsize(24, 80)
# # Do not allow child to inherit open file descriptors from parent.
# max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
# for i in range (3, max_fd):
# try:
# os.close (i)
# except OSError:
# pass
program_name = '/usr/bin/ssh'
os.execv(program_name, [program_name] + sys.argv[1:])
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
first_time = True
child_buffer = StringIO()
while True:
try:
r, w, e = select.select([child_fd, pty.STDIN_FILENO], [child_fd], [])
if child_fd in r:
child_buffer.write(os.read(child_fd, 1000))
#os.write(pty.STDOUT_FILENO, os.read(child_fd, 1000))
if pty.STDIN_FILENO in r:
os.write(child_fd, os.read(pty.STDIN_FILENO, 1000))
if child_fd in w and child_buffer.getvalue().endswith("$ ") and first_time:
os.write(child_fd, "function foo () { echo foo; };\r")
first_time = False
if (child_fd in r and not first_time) or (child_fd in w and not child_buffer.getvalue().endswith("$ ")):
os.write(pty.STDOUT_FILENO, child_buffer.getvalue())
child_buffer.seek(0,0)
child_buffer.truncate()
except OSError:
break
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
# line = read_stdout()
# while not input_line.match(line):
# print line
# line = read_stdout()
# except_stdin()
# append_functions
# give_user_back_stdout_stdin_and_stde