Commit 95e9ac11 authored by ibuler's avatar ibuler

[Update] 修改proxy

parent bb1031b6
...@@ -86,36 +86,44 @@ class ProxyServer: ...@@ -86,36 +86,44 @@ class ProxyServer:
def get_telnet_server_conn(self, asset, system_user): def get_telnet_server_conn(self, asset, system_user):
pass pass
def make_proxy_command(self, asset): def get_proxy_sock(self, asset):
gateway = asset.domain.random_gateway() domain = self.app.service.get_domain_detail_with_gateway(
proxy_command = [ asset.domain
"ssh", "-p", str(gateway.port), )
"{}@{}".format(gateway.username, gateway.ip),
"-W", "{}:{}".format(asset.ip, asset.port), "-q",
]
if gateway.password:
proxy_command.insert(0, "sshpass -p {}".format(gateway.password))
if gateway.private_key:
gateway.set_key_dir(os.path.join(self.app.root_path, 'keys'))
proxy_command.append("-i {}".format(gateway.private_key_file))
proxy_command = ' '.join(proxy_command)
return proxy_command
def get_ssh_server_conn(self, asset, system_user):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sock = None sock = None
if asset.domain: for i in domain.gateways:
asset.domain = self.app.service.get_domain_detail_with_gateway(asset.domain) gateway = domain.random_gateway()
proxy_command = [
"ssh", "-p", str(gateway.port),
"{}@{}".format(gateway.username, gateway.ip),
"-W", "{}:{}".format(asset.ip, asset.port), "-q",
]
if gateway.password:
proxy_command.insert(0, "sshpass -p {}".format(gateway.password))
if gateway.private_key:
gateway.set_key_dir(os.path.join(self.app.root_path, 'keys'))
proxy_command.append("-i {}".format(gateway.private_key_file))
proxy_command = ' '.join(proxy_command)
try: try:
proxy_command = self.make_proxy_command(asset)
sock = paramiko.ProxyCommand(proxy_command) sock = paramiko.ProxyCommand(proxy_command)
break
except (paramiko.AuthenticationException, except (paramiko.AuthenticationException,
paramiko.BadAuthenticationType, SSHException, paramiko.BadAuthenticationType, SSHException,
TimeoutError) as e: TimeoutError) as e:
logger.error(e) logger.error(e)
continue
return sock
def get_ssh_server_conn(self, asset, system_user):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sock = None
if asset.domain:
sock = self.get_proxy_sock(asset)
try: try:
ssh.connect( ssh.connect(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment