2788647047_py

翎上發表於2024-08-16

import socket
import csv

# 讀取檔案中的域名列表
with open("111.txt", "r") as f:
domains = [line.strip() for line in f.readlines() if line.strip()]

# 查詢域名對應的 IP 地址
ip_addresses = {}
for domain in domains:
try:
ip = socket.gethostbyname(domain)
ip_addresses[domain] = ip
except:
ip_addresses[domain] = "無法解析"

# 將結果輸出為 CSV 檔案
with open("ip_addresses.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["域名", "IP 地址"])
for domain, ip in ip_addresses.items():
writer.writerow([domain, ip])

print("IP 地址查詢結果已儲存到 ip_addresses.csv 檔案中。")