
Python: Expiring Domain Slack Alerts
Managing a large number of domain names can be a tedious task, especially when it comes to tracking expiration dates. This Python script automates this process, making it easy to stay on top of your domain renewals.
How it works:
- Create a domain list: Prepare a simple text file containing all the domain names you want to monitor.
- Set up Slack webhook: Configure an incoming webhook URL in your Slack workspace to receive alerts.
- Run the script: Execute the script, providing the Slack webhook URL and the path to your domain list.
The script will:
- Scan the domain list for expirations within the next 30 days.
- Send a timely alert to your designated Slack channel for each impending expiration.
Need help with the Slack webhook setup? I’ve included a brief guide below to get you started.
Create Slack Webhook
Log into your Slack workspace as an administrator. Go to the admin section of Slack. On the left menu list, scroll down to Configure Apps.
Choose the Custom Integrations. You can also utilize Slack apps for this. But for this round, we’ll use Slacks Incoming Webhooks. Choose Incoming Webhooks, then click the Add to Slack button.
Choose the Channel you want the alerts to go into. In this example I will use a private channel I created named “domain-expiration-alerts”. Once you chose the channel, click “Add Incoming Webhooks integration”. This will send an alert to the channel you just chose.
Next we’ll configure the basics of the alert such as its Custom name, Label Icon, Etc. Once complete, click Save Settings. You will also notice the app name in your channel alert has updated.
Dont bee a noob. The webook in this screenshot has already been removed.
Last thing you need to do is copy the Webhook URL. You will need this for the Python script.
Looking to do this with PowerShell instead? Check this page out.
Python Script
Now that you have your Slack Webhook URL, it’s time to setup your alert script. Below is the script you can copy. Make sure you update the .txt file path and Webhook URL with your own information.
import whois
import requests
from datetime import datetime, timedelta
# Define your input file path containing domain names (one per line)
input_file_path = "/Users/daveherrell/Desktop/domains.txt"
# Define your Slack webhook URL (replace with your actual Slack webhook URL)
slack_webhook_url = "https://hooks.slack.com/services/T0ACBE211/YOURSUPERCOOLWEBHOOK"
# Define the number of days threshold for notification
expiration_threshold = 30
# Initialize a list to collect domains nearing expiration
expiring_domains = []
# Read domains from file and process each
with open(input_file_path, 'r') as file:
for line in file:
domain = line.strip()
if domain:
try:
# Perform WHOIS lookup
domain_info = whois.whois(domain)
# Get expiration date and registrar
expiration_date = domain_info.expiration_date
registrar = domain_info.registrar
# Handle cases where expiration_date might be a list
if isinstance(expiration_date, list):
expiration_date = expiration_date[0]
# Calculate days until expiration
if expiration_date:
days_until_expiration = (expiration_date - datetime.now()).days
# Check if the domain is expiring within the threshold
if days_until_expiration <= expiration_threshold:
expiring_domains.append({
"domain": domain,
"expiration_date": expiration_date.strftime("%Y-%m-%d"),
"days_until_expiration": days_until_expiration,
"registrar": registrar
})
print(f"Processed {domain}: {days_until_expiration} days until expiration")
else:
print(f"No expiration date found for {domain}")
except Exception as e:
print(f"Failed to process {domain}: {e}")
# Format and send the message to Slack if there are expiring domains
if expiring_domains:
slack_message = "Domains Expiring Soon (Within 80 Days):\n"
for domain_info in expiring_domains:
slack_message += (
f"\n*Domain:* {domain_info['domain']}\n"
f"*Expiration Date:* {domain_info['expiration_date']}\n"
f"*Days Until Expiration:* {domain_info['days_until_expiration']}\n"
f"*Registrar:* {domain_info['registrar']}\n"
)
# Send the message to Slack
payload = {"text": slack_message}
response = requests.post(slack_webhook_url, json=payload)
if response.status_code == 200:
print("Message sent to Slack successfully!")
else:
print(f"Failed to send message to Slack: {response.text}")
else:
print("No domains are expiring within the threshold.")
A couple items to note:
- You can change the date range on line 8 with whatever you wish. Just make sure it’s in days. For instance you can set it for 90 days instead of 30 days.
- Within your TXT file, you can list up to 10 thousand domains before this breaks. However, make sure the file only contains one domain per line!
- Multiple Expiration Dates: Some WHOIS entries return a list of dates; this script picks the first one.
- You can easily set this script to run via Scheduled task on Windows servers, Unix, etc.
- Error Handling: Skips domains without an expiration date or with WHOIS lookup errors.
Categories
Recent Posts
- PowerShell: How to Add an Alias to Every Users Mailbox and Groups in Microsoft 365
- Slack: Disable Entra ID User using a slash command.
- Slack: Retrieve Entra ID (MS365) User Information with a slash command.
- Jira Cloud: Disabling Entra ID User Accounts via Automation and Microsoft Runbook
- Jira Cloud: Restart an Azure VM using JSM Assets and Automation