This commit is contained in:
yumoqing 2025-04-15 17:44:49 +08:00
parent 25fc565343
commit 9bf42664a8

View File

@ -2,6 +2,7 @@ import os
import sys
import time
import shlex
from contextlib import asynccontextmanager
from functools import partial
from threading import Thread
from appPublic.myTE import tmpTml
@ -111,6 +112,25 @@ class SSHNode:
j['refconn'] = refconn
self.conn = await self._connect(**j)
@asynccontextmanager
async def getconnector(self):
refconn = None
for jj in self.jumpers:
j = jj.copy()
j['refconn'] = refconn
refconn = await self._connect(**j)
j = self.server2.copy()
j['refconn'] = refconn
conn = await self._connect(**j)
try:
yield SshConnector(conn, refconn=refconn)
except Exception as e:
exception(f'{e=}, {format_exc()}')
conn.close()
if refconn:
refconn.close()
def close(self):
self.conn.close()
cnt = len(self.jumper_conns)
@ -141,8 +161,8 @@ class SSHNode:
async def _xcmd(self, cmd, xmsgs=[], ns={},
show_input=None,
show_stdout=None):
proc = await self._process(cmd, term_type='xterm',
term_size=(80,24),
proc = await self._process(cmd, term_type='xterm-256color',
term_size=(24, 80),
encoding='utf-8'
)
@ -323,8 +343,8 @@ class SSHBash:
async def run(self, read_co, write_co):
await self.node.connect()
self.p_obj = await self.node._process('bash',
term_type='vt100',
term_size=(80,24),
term_type='xterm-256color',
term_size=(24, 80),
encoding=None)
if isinstance(self.p_obj, Exception):
print('Excetion:', self.p_obj)
@ -346,6 +366,31 @@ class SSHBash:
x = await self.p_obj.stdout.read(1024)
await write_co(x)
def SshConnector:
def __init__(self, conn, refconn=None):
self.conn = conn
self.refconn = refconn
async def r2l(self, rf, lf):
x = await asyncssh.scp((self.conn, rf),
lf,
preserve=True,
recurse=True)
return x
async def l2r(self, lf, rf):
x = await asyncssh.scp(lf, (self.comm, rf),
preserve=True,
recurse=Tree)
return x
async def run_process(self, *args, **kw):
a = await self.conn.create_process(*args, **kw)
return a
async def run(self, cmdline, input=None, stdin=None, stdout=None, stderr=None):
return await self.conn.run(cmdline, input=input, stdin=stdin, stdout=stdout)
if __name__ == '__main__':
async def sysstdin_read():
return os.read(sys.stdin.fileno(), 65535)