Soo... Long story short, I'm trying to get the IP-Addresses of Links which can cause extrem harm to other Users on Discord (also known as Account Scam Links, Account Token Grabber Links, etc. etc.). [If you may have any concern about my IP-Address being grabbed, I'm using a VPN, so it won't help them this much] I've searched up on how to save the response which comes from the Request after it's printing The IP Address of xyz.com is <IP-Address>.
After some tries I've decided to ask the so called "all-knowing stackoverflow Community" to help me out on my Problem.
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!")
The Problem:
The actual IP-Address doesn't show up, it's just telling me 0.0.0.0, the issue of the {hostname} variable initialized at line 5 doesn't seem to have an effect since the hostname is left blank.
The expected Output:
I expected the Code to print out the IP Address of the Hosts so I could further investigate and maybe contact the legal owners of the hosts for those Websites to inform them about this malicious behaviour.
Thanks for reading in advance
Greetings
Ohnezahn ZAE
CodePudding user response:
This is what should be happening
>>> socket.gethostbyname('')
'0.0.0.0'
then verify your hostname is not None or empty before querying.
CodePudding user response:
Your problem is you incorrectly reading file line by line. I suppose you need something like this:
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!")
If you suspicious_links.txt will be, for example:
stackoverflow.com
youtube.com
google.com
then output will be:
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
