
Python: Teams Alerts for Expiring Domain Names
Managing a large number of domain names can be a tedious task, especially when it comes to tracking expiration dates. This Python script can help automate 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 Microsoft Teams webhook: Configure an incoming webhook URL in your Teams channel to receive alerts.
- Run the script: Execute the script, providing the Microsoft Teams 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 Teams channel for each impending expiration.
Need help with the Teams webhook setup? I’ve included a brief guide below to get you started.
Create Microsoft Teams Webhook
If you haven’t already, create a Teams channel to host your domain alerts. Click the there *** (dots) on the name of the channel and choose Manage channel.
If not already expanded, expand the area under Connectors. Choose Edit.
Search for Incoming Webhook if the connector isnt already showing in your options. Click Configure.
Create your webhook name, and upload an image (optional) for your webhook and click Create.
Last thing you need to do is copy the Webhook URL. You will need this for the Python script. Click the X button to close the pop-up.
Looking to do this with PowerShell instead? Check this page out.
Python Script
Now that you have your Microsoft Teams 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
from datetime import datetime, timedelta
import requests
import json
# Make sure to create your Microsoft Teams webhook URL
TEAMS_WEBHOOK_URL = "YOUR SUPER COOL MS TEAMS WEBHOOK URL"
# Function to send message to Microsoft Teams
def send_to_teams(message):
headers = {"Content-Type": "application/json"}
payload = {"text": message}
response = requests.post(TEAMS_WEBHOOK_URL, headers=headers, data=json.dumps(payload))
if response.status_code != 200:
print(f"Failed to send message to Teams: {response.status_code}, {response.text}")
else:
print("Message sent to Teams successfully")
# Function to check domain expiration and return details
def check_domain_expiration(domain):
try:
domain_info = whois.whois(domain)
expiration_date = domain_info.expiration_date
registrar = domain_info.registrar
# Some WHOIS data return a list for expiration_date
if isinstance(expiration_date, list):
expiration_date = expiration_date[0]
if expiration_date:
days_until_expiration = (expiration_date - datetime.now()).days
if days_until_expiration <= 30:
return {
"domain": domain,
"expiration_date": expiration_date.strftime('%Y-%m-%d'),
"days_until_expiration": days_until_expiration,
"registrar": registrar
}
except Exception as e:
print(f"Error fetching WHOIS for {domain}: {e}")
return None
# Main function
def main():
with open("/Users/daveherrell/Desktop/domains.txt", "r") as file: #Make sure you update this with your txt file path!
domains = file.readlines()
message = "**Domains expiring within 30 days:**\n"
has_expiring_domains = False
for domain in domains:
domain = domain.strip()
domain_details = check_domain_expiration(domain)
if domain_details:
has_expiring_domains = True
message += (
f"- **Domain**: {domain_details['domain']}\n"
f" - **Expiration Date**: {domain_details['expiration_date']}\n"
f" - **Days Until Expiration**: {domain_details['days_until_expiration']} days\n"
f" - **Registrar**: {domain_details['registrar']}\n\n"
)
if has_expiring_domains:
send_to_teams(message)
else:
print("No domains are expiring within the next 30 days.")
if __name__ == "__main__":
main()
A couple items to note:
- You can change the date range on line 32 with whatever you wish. Just make sure it’s in days. For instance you can set it for 90 days instead of 30 days.
- Make sure you update the txt file path on line 45!
- 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!
- You can easily set this script to run via Scheduled task on Windows server or any Linux server using a cron job.
- Error Handling: Skips domains without an expiration date or with WHOIS lookup errors.
The Results
Finally you should be able to run your Python Script and receive your alerts. You should get a similar Microsoft Teams alert:
Domains I knew were expiring were used in this example.
And there you have it. Simple Alerts to your Teams Channel.
Hope you find this helpful!
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