-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathssh.py
More file actions
44 lines (37 loc) · 1.24 KB
/
Copy pathssh.py
File metadata and controls
44 lines (37 loc) · 1.24 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
import os
import pty
import sys
import select
import logging
from .prompts import get_prompt
logger = logging.getLogger(__name__)
def run_interactive_ssh(tokens, shellm):
"""Runs an interactive SSH session."""
try:
pid, fd = pty.fork()
if pid == 0:
os.execvp(tokens[0], tokens)
else:
shellm.ssh_session = fd # Set the ssh_session file descriptor
interactive_ssh(fd, shellm)
except Exception as e:
logger.error(f"SSH session error: {e}")
def interactive_ssh(fd, shellm):
"""Handles interactive SSH session."""
while True:
rlist, _, _ = select.select([fd, sys.stdin], [], [])
if fd in rlist:
data = os.read(fd, 1024)
if data:
sys.stdout.buffer.write(data)
sys.stdout.flush()
else:
break
if sys.stdin in rlist:
command = input(get_prompt())
if command.strip().startswith('##'):
shellm.answer_question(command[2:].strip())
elif command.strip().startswith('#'):
shellm.handle_lm_command(command[1:].strip(), remote=True)
else:
os.write(fd, (command + '\n').encode())