diff --git a/docker-compose.yml b/docker-compose.yml index 55189d536165ab8b14e4ae9da3ab9314009e7c6d..6e44265732f1bb7b7415ae6659a657abbe21025e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,7 +20,8 @@ services: APP_MODE: 'dev' TELEGRAM_BOT_TOKEN: 'your dev token here' volumes: - - ~/.ssh:/root/.ssh + - ~/.ssh/id_rsa:/root/.ssh/id_rsa + - ~/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub - ./:/app working_dir: '/app' networks: diff --git a/src/utils/ssh.py b/src/utils/ssh.py index ff8a2c9107fc0c813b5bfeb43bc0ac63bb4ac42c..7452cbc280354754f53c4104a3fe827a05e989fd 100644 --- a/src/utils/ssh.py +++ b/src/utils/ssh.py @@ -10,26 +10,35 @@ logger = logging.getLogger() async def open_ssh_session(server: Server) -> asyncssh.SSHClientProcess: - connection = await asyncssh.connect(server.server_address, server.server_port) + connection = await asyncssh.connect(server.server_address, server.server_port, known_hosts=None) process = await connection.create_process('/bin/bash') return process -async def run_interactive_command(command: str, - process: asyncssh.SSHClientProcess, - timeout=0.5) -> str: - process.stdin.write(command + '\n') +async def collect_output(out_pipe, timeout): res = [] try: - line = await asyncio.wait_for(process.stdout.readline(), timeout) + line = await asyncio.wait_for(out_pipe.readline(), timeout) res.append(line) while line: - logger.debug(line) - res.append(await asyncio.wait_for(process.stdout.readline(), timeout)) - except asyncio.exceptions.TimeoutError as e: - logger.exception(e) - return '\n'.join(res).strip() - return '\n'.join(res).strip() + logger.info(line) + res.append(await asyncio.wait_for(out_pipe.readline(), timeout)) + except asyncio.exceptions.TimeoutError: + return ''.join(res).strip() + return ''.join(res).strip() + + +async def run_interactive_command(command: str, + process: asyncssh.SSHClientProcess, + timeout=0.5) -> str: + process.stdin.write(command + '\n') + stdout = await collect_output(process.stdout, timeout) + stderr = await collect_output(process.stderr, timeout) + logger.debug(f"STDOUT: {stdout}") + logger.debug(f"STDERR: {stderr}") + if stderr: + raise Exception(stderr) + return stdout async def run_ssh_command(server: Server, command: str) -> str: