Soo。。。长话短说,我正在尝试获取链接的IP地址,这些链接可能会对Discord上的其他用户造成极大伤害(也称为帐户诈骗链接、帐户令牌抓取链接等(。[如果你担心我的IP地址被抓取,我正在使用VPN,所以这对他们没有多大帮助]我已经搜索了如何在打印IP地址后保存来自请求的响应xyz.com的<IP地址>。
经过一些尝试,我决定问所谓的";无所不知的stackoverflow社区";帮我解决问题。
import socket
with open("suspicious_links.txt", 'r') as f:
while True:
hostname = f.read()
# IP lookup from hostname
with open("log.txt", 'a') as e:
try:
e.write(f"The {hostname}'s IP Address is {socket.gethostbyname(hostname)}n")
print(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')
except socket.gaierror:
print(f"Searching up {hostname} has failed. Perhaps this Website doesn't exist anymore!")
问题:
实际的IP地址没有显示,它只是告诉我0.0.0.0,在第5行初始化的{hostname}变量的问题似乎没有影响,因为主机名为空。
预期输出:
我希望《守则》能打印出主机的IP地址,这样我就可以进一步调查,并可能联系这些网站的主机的合法所有者,告知他们这种恶意行为。
感谢您提前阅读
问候
Ohnezahn ZAE
您的问题是逐行错误地读取文件。我想你需要这样的东西:
import socket
with open("suspicious_links.txt", 'r') as f:
hostnames = [line.rstrip() for line in f]
for hostname in hostnames:
# IP lookup from hostname
with open("log.txt", 'a') as e:
try:
e.write(f"The {hostname}'s IP Address is {socket.gethostbyname(hostname)}n")
print(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')
except socket.gaierror:
print(f"Searching up {hostname} has failed. Perhaps this Website doesn't exist anymore!")
如果您的suspicious_links.txt
将是,例如:
stackoverflow.com
youtube.com
google.com
则输出为:
The stackoverflow.com's IP Address is 151.101.65.69
The youtube.com's IP Address is 216.58.215.110
The google.com's IP Address is 172.217.16.46
这就是应该发生的事情
>>> socket.gethostbyname('')
'0.0.0.0'
然后在查询之前验证您的hostname
不是None
或为空。