import sys
import socket
from requests.packages.urllib3.contrib import pyopenssl
def domain_dns(ip, port, timeout=0.09):
try:
# 创建一个 socket
sock = socket.create_connection((ip, port), timeout=timeout)
# 设置超时
sock.settimeout(timeout)
# 加载 SSL 证书
x509 = pyopenssl.OpenSSL.crypto.load_certificate(
pyopenssl.OpenSSL.crypto.FILETYPE_PEM,
pyopenssl.ssl.get_server_certificate((ip, port))
)
# 获取域名
domain = x509.get_subject().CN
print(ip,'-----',domain)
except Exception as e:
print(f"{ip} > Get CN failed: {str(e)}")
if __name__ == '__main__':
ip = "27.155.119.166"
# ip = "113.89.101.85"
port = 443
domain_dns(ip, port,)