U update ssh lib

pull/480/head
vapao 2022-04-14 22:15:15 +08:00
parent ca2775c9bf
commit f93dc97f51
1 changed files with 9 additions and 11 deletions

View File

@ -5,7 +5,7 @@ from paramiko.client import SSHClient, AutoAddPolicy
from paramiko.rsakey import RSAKey from paramiko.rsakey import RSAKey
from paramiko.ssh_exception import AuthenticationException from paramiko.ssh_exception import AuthenticationException
from io import StringIO from io import StringIO
import base64 from uuid import uuid4
import time import time
import re import re
@ -17,8 +17,8 @@ class SSH:
self.client = None self.client = None
self.channel = None self.channel = None
self.sftp = None self.sftp = None
self.exec_file = None
self.eof = 'Spug EOF 2108111926' self.eof = 'Spug EOF 2108111926'
self.already_init = False
self.default_env = self._make_env_command(default_env) self.default_env = self._make_env_command(default_env)
self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?') self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?')
self.arguments = { self.arguments = {
@ -116,11 +116,11 @@ class SSH:
def put_file(self, local_path, remote_path): def put_file(self, local_path, remote_path):
sftp = self._get_sftp() sftp = self._get_sftp()
sftp.put(local_path, remote_path) sftp.put(local_path, remote_path, confirm=False)
def put_file_by_fl(self, fl, remote_path, callback=None): def put_file_by_fl(self, fl, remote_path, callback=None):
sftp = self._get_sftp() sftp = self._get_sftp()
sftp.putfo(fl, remote_path, callback=callback) sftp.putfo(fl, remote_path, callback=callback, confirm=False)
def list_dir_attr(self, path): def list_dir_attr(self, path):
sftp = self._get_sftp() sftp = self._get_sftp()
@ -180,19 +180,17 @@ class SSH:
def _handle_command(self, command, environment): def _handle_command(self, command, environment):
new_command = commands = '' new_command = commands = ''
if not self.already_init: if not self.exec_file:
commands = 'export SPUG_EXEC_FILE=$(mktemp)\n' self.exec_file = f'/tmp/{uuid4().hex}'
commands += 'trap \'rm -f $SPUG_EXEC_FILE\' EXIT\n' commands += f'trap \'rm -f {self.exec_file}\' EXIT\n'
self.already_init = True
env_command = self._make_env_command(environment) env_command = self._make_env_command(environment)
if env_command: if env_command:
new_command += f'{env_command}\n' new_command += f'{env_command}\n'
new_command += command new_command += command
new_command += f'\necho {self.eof} $?\n' new_command += f'\necho {self.eof} $?\n'
b64_command = base64.standard_b64encode(new_command.encode()) self.put_file_by_fl(StringIO(new_command), self.exec_file)
commands += f'echo {b64_command.decode()} | base64 -di > $SPUG_EXEC_FILE\n' commands += f'source {self.exec_file}\n'
commands += 'source $SPUG_EXEC_FILE\n'
return commands return commands
def _decode(self, content): def _decode(self, content):