Skip to content

Commit 14ce0f7

Browse files
committed
add reverse lookup tutorial
1 parent 98e4e34 commit 14ce0f7

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
6969
- [How to Make Malware Persistent in Python](https://thepythoncode.com/article/how-to-create-malware-persistent-in-python). ([code](ethical-hacking/persistent-malware))
7070
- [How to Remove Persistent Malware in Python](https://thepythoncode.com/article/removingg-persistent-malware-in-python). ([code](ethical-hacking/remove-persistent-malware))
7171
- [How to Check Password Strength with Python](https://thepythoncode.com/article/test-password-strength-with-python). ([code](ethical-hacking/checking-password-strength))
72+
- [How to Perform Reverse DNS Lookups Using Python](https://thepythoncode.com/article/reverse-dns-lookup-with-python). ([code](ethical-hacking/reverse-dns-lookup))
7273

7374
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
7475
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Perform Reverse DNS Lookups Using Python](https://thepythoncode.com/article/reverse-dns-lookup-with-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Import the necessary libraries
2+
import argparse
3+
import ipaddress
4+
import socket
5+
import requests
6+
7+
API_KEY = "Your-Api-Key-Here" # Replace with your ViewDNS API key
8+
9+
# Function to Check if IP address is valid.
10+
def is_valid_ip(ip):
11+
12+
try:
13+
ipaddress.ip_address(ip)
14+
return True
15+
except ValueError:
16+
return False
17+
18+
19+
# Perform reverse look up.
20+
def reverse_lookup(ip):
21+
try:
22+
domain = socket.gethostbyaddr(ip)[0]
23+
return domain
24+
except socket.herror:
25+
return None
26+
27+
28+
# Get websites on same server.
29+
def get_websites_on_server(ip):
30+
url = f"https://api.viewdns.info/reverseip/?host={ip}&apikey={API_KEY}&output=json"
31+
response = requests.get(url)
32+
if response.status_code == 200:
33+
data = response.json()
34+
if "response" in data and "domains" in data["response"]:
35+
websites = data["response"]["domains"]
36+
return websites
37+
return []
38+
39+
40+
# Get user arguments and execute.
41+
def main():
42+
parser = argparse.ArgumentParser(description="Perform IP reverse lookup.")
43+
parser.add_argument("ips", nargs="+", help="IP address(es) to perform reverse lookup on.")
44+
parser.add_argument("--all", "-a", action="store_true", help="Print all other websites on the same server.")
45+
args = parser.parse_args()
46+
47+
for ip in args.ips:
48+
if not is_valid_ip(ip):
49+
print(f"[-] Invalid IP address: {ip}")
50+
continue
51+
52+
domain = reverse_lookup(ip)
53+
if domain:
54+
print(f"[+] IP: {ip}, Domain: {domain}")
55+
if args.all:
56+
websites = get_websites_on_server(ip)
57+
if websites:
58+
print("\nOther websites on the same server:")
59+
for website in websites:
60+
print(f"[+] {website}")
61+
print('\n')
62+
else:
63+
print("[-] No other websites found on the same server.")
64+
else:
65+
print(f"[-] No domain found for IP: {ip}")
66+
67+
68+
if __name__ == "__main__":
69+
main()

0 commit comments

Comments
 (0)