From 93728d803855002da20558b075bfb7e305618b7f Mon Sep 17 00:00:00 2001 From: selang Date: Wed, 1 Feb 2023 13:07:37 +0000 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=AF=B9proxycommand?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/libs/ssh.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/spug_api/libs/ssh.py b/spug_api/libs/ssh.py index 7f4d600..b7c7310 100644 --- a/spug_api/libs/ssh.py +++ b/spug_api/libs/ssh.py @@ -1,6 +1,9 @@ +#coding:utf-8 # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug # Copyright: (c) # Released under the AGPL-3.0 License. +from paramiko.config import SSHConfig +from paramiko.proxy import ProxyCommand from paramiko.client import SSHClient, AutoAddPolicy from paramiko.rsakey import RSAKey from paramiko.auth_handler import AuthHandler @@ -10,7 +13,8 @@ from io import StringIO from uuid import uuid4 import time import re - +import os +import io def _finalize_pubkey_algorithm(self, key_type): if "rsa" not in key_type: @@ -61,6 +65,15 @@ class SSH: self.eof = 'Spug EOF 2108111926' self.default_env = default_env self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?') + sock = None + ssh_config_file = os.path.expanduser("~/.ssh/config") + if os.path.exists(ssh_config_file): + ssh_config = SSHConfig() + with io.open(ssh_config_file, 'rt', encoding='utf-8') as f: + ssh_config.parse(f) + host_config = ssh_config.lookup(hostname) + if 'proxycommand' in host_config: + sock = ProxyCommand(host_config['proxycommand']) self.arguments = { 'hostname': hostname, 'port': port, @@ -70,7 +83,9 @@ class SSH: 'timeout': connect_timeout, 'allow_agent': False, 'look_for_keys': False, - 'banner_timeout': 30 + 'banner_timeout': 30, + 'sock':sock + } @staticmethod @@ -80,6 +95,7 @@ class SSH: key.write_private_key(key_obj) return key_obj.getvalue(), 'ssh-rsa ' + key.get_base64() + def get_client(self): if self.client is not None: return self.client