def normalize_host(ip):
def resolve_host(ip):
logger = logging.getLogger('resolve_host')
try:
hostname = gethostbyaddr(ip)[0]
except herror as e:
# socket.herror: [Errno 1] Unknown host
logger.error("Unable to resolve %s: %s", ip, e)
return ip
hostname = hostname.split('.')[0]
logger.info("%s is known as %s", ip, hostname)
if not hostname.startswith('ap-'):
return hostname
else:
# ap-s200 -> ap-s*
return re.sub(r'\d+$', '*', hostname)
if ip not in hosts_cache:
hosts_cache[ip] = resolve_host(ip)
return hosts_cache[ip]
评论列表
文章目录