Open In App

How to Make a Subdomain Scanner in Python?

In this article, we are going to scan the subdomains using requests module in Python, which allows us to easily make HTTPS requests to get information from the websites. To install the requests module, write the following command in your command prompt.

pip install requests

The URL (Uniform Resource Locator) consists mainly of four parts:



The below figure demonstrating all four parts of the URL.



Subdomains are defined as the part of the domain that comes before the Domain name and Domain extension i.e, Top-level domain (TLD). Subdomains are used for organizing or dividing web content into distinct sections. Subdomains help us to separate our website into sections, subdomains are viewed as different websites.

Subdomain File Used:

mail
mail2
www
ns2
ns1
blog
localhost
m
ftp
mobile
ns3
smtp
search
api
dev
secure
webmail
admin
img
news
sms
marketing
test
video
www2
media
static
ads
mail2
beta
wap
blogs
download
dns1
www3
origin
shop
forum
chat
www1
image
new
tv
dns
services
music
images
pay
ddrint
conc

Approach:

Steps Needed

Showing subdomain names present in the text file and creating a list of that subdomains.




# opening the subdomain text file in the read mode
with open('subdomain_names.txt','r') as file:
   
    # reading the file
    name = file.read()
     
    # using splitlines() function storing the list
    # of spitted strings
    sub_dom = name.splitlines()
     
    # printing number of subdomain names present in
    # the list
    print(f"Number of subdomain names present in the file are: {len(sub_dom)}\n")
     
    # printing list of subdomain names present in the
    # text file
    print("List of subdomain names present in the file\n")
    print(sub_dom)

Output:

In the above code, we are opening the text file from the storage in which our subdomain names are present which we have to scan and also after opening the file from the storage in read mode we are making the list of content present in that file and printing number of subdomain names present in the file and printing the list of subdomain names.

The text file contains only 50 subdomains for demonstration you can take as many subdomain as you want to scan according to your need. So in the above output image list of subdomains is printed which we will scan in the upcoming example.

We will use this piece of code in scanning the subdomains.

Example 1: Subdomain scanner program using Python.




# importing module
import requests
 
# function for scanning subdomains
def domain_scanner(domain_name,sub_domnames):
    print('----URL after scanning subdomains----')
     
    # loop for getting URL's
    for subdomain in sub_domnames:
       
        # making url by putting subdomain one by one
        url = f"https://{subdomain}.{domain_name}"
         
        # using try catch block to avoid crash of the
        # program
        try:
            # sending get request to the url
            requests.get(url)
             
            # if after putting subdomain one by one url
            # is valid then printing the url
            print(f'[+] {url}')
             
            # if url is invalid then pass it
        except requests.ConnectionError:
            pass
 
# main function
if __name__ == '__main__':
   
    # inputting the domain name
    dom_name = input("Enter the Domain Name:")
 
    # opening the subdomain text file
    with open('subdomain_names1.txt','r') as file:
       
        # reading the file
        name = file.read()
         
        # using splitlines() function storing the list
        # of splitted strings
        sub_dom = name.splitlines()
         
    # calling the function for scanning the subdomains
    # and getting the url
    domain_scanner(dom_name,sub_dom)
    

Output:

The scanning time will depend on the number of subdomains you are scanning, for the demonstration I have some names of subdomains in the text file, you can add as many as subdomains you want to scan.

Example 2: Subdomain scanner for Wikipedia using Python.




# importing library
import requests
 
# function for scanning subdomains
def domain_scanner(domain_name,sub_domnames):
    print('-----------Scanner Started-----------')
    print('----URL after scanning subdomains----')
     
    # loop for getting URL's
    for subdomain in sub_domnames:
       
        # making url by putting subdomain one by one
        url = f"https://{subdomain}.{domain_name}"
         
        # using try catch block to avoid crash of
        # the program
        try:
           
            # sending get request to the url
            requests.get(url)
             
            # if after putting subdomain one by one url
            # is valid then printing the url
            print(f'[+] {url}')
             
        # if url is invalid then pass it
        except requests.ConnectionError:
            pass
    print('\n')
    print('----Scanning Finished----')
    print('-----Scanner Stopped-----')
 
# main function
if __name__ == '__main__':
   
    # inputting the domain name
    dom_name = input("Enter the Domain Name:")
    print('\n')
 
    # opening the subdomain text file
    with open('subdomain_names1.txt','r') as file:
       
        # reading the file
        name = file.read()
         
        # using splitlines() function storing the
        # list of splitted strings
        sub_dom = name.splitlines()
         
    # calling the function for scanning the subdomains
    # and getting the url
    domain_scanner(dom_name,sub_dom)

Output:


Article Tags :