From 5fbd8f4cb929beb825f1f15632d74f07808122f0 Mon Sep 17 00:00:00 2001 From: liuheng Date: Mon, 28 Oct 2024 16:30:49 +0800 Subject: [PATCH] =?UTF-8?q?fix=20/etc/hosts=E5=AD=98=E5=9C=A8=E5=86=97?= =?UTF-8?q?=E4=BD=99=EF=BC=8Cip=E5=92=8Chostname=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E6=9C=89=E9=97=AE=E9=A2=98=E5=B9=B6=E6=94=AF=E6=8C=81=E4=B8=80?= =?UTF-8?q?=E7=AB=99=E5=BC=8F=E5=AE=89=E8=A3=85=E4=BD=BF=E7=94=A8=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/base_utils/os/hosts_util.py | 24 ------------------- script/gs_preinstall | 1 + script/gspylib/threads/SshTool.py | 38 ++++++++++++++++++++---------- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/script/base_utils/os/hosts_util.py b/script/base_utils/os/hosts_util.py index 216b9a5..32e8abd 100644 --- a/script/base_utils/os/hosts_util.py +++ b/script/base_utils/os/hosts_util.py @@ -96,30 +96,6 @@ class HostsUtil: if hostname == name: ip_list.append(ip) return ip_list - - @staticmethod - def ip_to_hostname(host_ip): - if not host_ip: - return "" - if host_ip[:1] == '[' and host_ip[-1:] == ']': - host_ip = host_ip.strip('[]') - - # get hostname from /etc/hosts - hostname = HostsUtil.get_hostname_by_ip_from_etc_hosts(host_ip) - if hostname: - return hostname - - hosts_file = FileUtil.get_hosts_file() - if not os.path.isfile(hosts_file): - raise Exception("hosts file is not exist") - contents = HostsUtil.read_hosts_file(hosts_file) - name = "" - for ip, hostname in contents.items(): - if host_ip == ip: - name = hostname - return name - if name == "": - return host_ip @staticmethod def read_hosts_file(path, mode='r'): diff --git a/script/gs_preinstall b/script/gs_preinstall index a4eca23..530dcc1 100644 --- a/script/gs_preinstall +++ b/script/gs_preinstall @@ -25,6 +25,7 @@ import pwd import sys import grp import subprocess +import readline from gspylib.common.CheckPythonVersion import check_python_version, \ check_python_compiler_option, check_os_and_package_arch diff --git a/script/gspylib/threads/SshTool.py b/script/gspylib/threads/SshTool.py index 87e6364..c23bde9 100644 --- a/script/gspylib/threads/SshTool.py +++ b/script/gspylib/threads/SshTool.py @@ -118,16 +118,19 @@ class SshTool(): self._finalizer = weakref.finalize(self, self.clen_ssh_result_files) self.__sessions = {} self.is_ip = False + self.host_ips_names_map = {} if hostNames: # if not ip, convert hostname to ip if not SecurityChecker.check_is_ip(hostNames[0]): self.is_ip = False # key:value hostname:ip - host_ip_list = HostsUtil.hostname_list_to_ip_list(hostNames) - if not host_ip_list: + host_ips_list = HostsUtil.hostname_list_to_ip_list(hostNames) + if not host_ips_list: raise Exception("Failed to hostname to ip.") - self.hostNames = host_ip_list + for ip, name in zip(host_ips_list, hostNames): + self.host_ips_names_map[ip] = name + self.hostNames = host_ips_list else: self.is_ip = True @@ -335,7 +338,8 @@ class SshTool(): if not self.is_ip: res_map = {} for key, value in resultMap.items(): - name = HostsUtil.ip_to_hostname(key) + if key in self.host_ips_names_map: + name = self.host_ips_names_map[key] res_map[name] = value resultMap = res_map @@ -344,7 +348,8 @@ class SshTool(): sshErrorPutFile = "%s/%s" % (self.__errorPath, host) # ip to hostname if not self.is_ip: - host = HostsUtil.ip_to_hostname(host) + if host in self.host_ips_names_map: + host = self.host_ips_names_map[host] if resultMap[host] == DefaultValue.SUCCESS: prefix = "SUCCESS" else: @@ -451,6 +456,8 @@ class SshTool(): else: self.is_ip = False host = HostsUtil.hostname_list_to_ip_list(host_list) + for ip, name in zip(host, host_list): + self.host_ips_names_map[ip] = name return host def executeCommand(self, cmd, cmdReturn=DefaultValue.SUCCESS, @@ -570,7 +577,8 @@ class SshTool(): if localMode: if not self.is_ip: - hostList = [HostsUtil.ip_to_hostname(hostList[0])] + if hostList[0] in self.host_ips_names_map: + hostList = [self.host_ips_names_map[hostList[0]]] resultMap[hostList[0]] = DefaultValue.SUCCESS if status == 0 \ else DefaultValue.FAILURE outputCollect = "[%s] %s:\n%s" \ @@ -586,7 +594,8 @@ class SshTool(): for host in hostList: if not localMode and not self.is_ip: - host = HostsUtil.ip_to_hostname(host) + if host in self.host_ips_names_map: + host = self.host_ips_names_map[host] if resultMap.get(host) != cmdReturn: if outputCollect.find("GAUSS-5") == -1: raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] @@ -713,7 +722,8 @@ class SshTool(): if localMode: dir_permission = 0o700 if not self.is_ip: - hostList = [HostsUtil.ip_to_hostname(hostList[0])] + if hostList[0] in self.host_ips_names_map: + hostList = [self.host_ips_names_map[hostList[0]]] if status == 0: resultMap[hostList[0]] = DefaultValue.SUCCESS outputCollect = "[%s] %s:\n%s" % ("SUCCESS", hostList[0], @@ -751,7 +761,8 @@ class SshTool(): for host in hostList: if not localMode and not self.is_ip: - host = HostsUtil.ip_to_hostname(host) + if host in self.host_ips_names_map: + host = self.host_ips_names_map[host] if resultMap.get(host) != DefaultValue.SUCCESS: if outputCollect.find("GAUSS-5") == -1: outputCollect = ErrorCode.GAUSS_514["GAUSS_51400"] \ @@ -792,7 +803,8 @@ class SshTool(): sshErrorPutFile = "%s/%s" % (self.__errorPath, host) if not self.is_ip: - host = HostsUtil.ip_to_hostname(host) + if host in self.host_ips_names_map: + host = self.host_ips_names_map[host] if os.path.isfile(sshOutPutFile): with open(sshOutPutFile, "r") as fp: context = fp.read() @@ -896,7 +908,8 @@ class SshTool(): if local_mode: dir_permission = 0o700 if not self.is_ip: - ssh_hosts = [HostsUtil.ip_to_hostname(ssh_hosts[0])] + if ssh_hosts[0] in self.host_ips_names_map: + ssh_hosts = [self.host_ips_names_map[ssh_hosts[0]]] if status == 0: resultMap[ssh_hosts[0]] = DefaultValue.SUCCESS outputCollect = "[%s] %s:\n%s" % ("SUCCESS", ssh_hosts[0], @@ -931,7 +944,8 @@ class SshTool(): for host in ssh_hosts: if not local_mode and not self.is_ip: - host = HostsUtil.ip_to_hostname(host) + if host in self.host_ips_names_map: + host = self.host_ips_names_map[host] if resultMap.get(host) != DefaultValue.SUCCESS: raise Exception(ErrorCode.GAUSS_502["GAUSS_50216"] % ("file [%s]" % srcFile) +