Skip to content

Commit

Permalink
refactor: Use command-line arguments for DNS monitoring
Browse files Browse the repository at this point in the history
This commit refactors the dns_monitor.py script to use command-line arguments instead of hard-coded values or environment variables for the hostname, substring, and delay. It introduces an argparse module to parse the command-line arguments and assigns them to the corresponding variables. This change provides more flexibility and makes it easier to customize the script's behavior without modifying the code directly.
  • Loading branch information
Aricg committed May 15, 2023
1 parent f16b442 commit fddfdf9
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions dns_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import dns.resolver
import time
import os
import argparse

# You can replace these with environment variables or command line arguments
hostname = "futurestay.com"
substring = "google-site-verification"
d = 5
# Command-line arguments
parser = argparse.ArgumentParser(description='Monitor DNS TXT records.')
parser.add_argument('--hostname', type=str, default=os.getenv('HOSTNAME', 'futurestay.com'))
parser.add_argument('--substring', type=str, default=os.getenv('SUBSTRING', 'google-site-verification'))
parser.add_argument('--d', type=int, default=os.getenv('DELAY', 5))
args = parser.parse_args()

# Store the keys
keys = set()
Expand All @@ -23,17 +26,17 @@ def monitor_dns(hostname, substring, d):
for txt_string in rdata.strings:
txt_string = txt_string.decode()
if substring in txt_string:
key = txt_string.split('=')[-1]
new_keys.add(key)
if key not in keys:
found_new_key = True
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - Found new '{substring}' key '{key}' in TXT record of {hostname}")
key = txt_string.split('=')[-1] if '=' in txt_string else None
if key:
new_keys.add(key)
if key not in keys:
found_new_key = True
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - Found new '{substring}' key '{key}' in TXT record of {hostname}")
keys = new_keys
if not found_new_key:
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - No new keys found in this loop for {hostname}")
except Exception as e:
print(f"Error occurred: {e}")
time.sleep(d)

monitor_dns(hostname, substring, d)

monitor_dns(args.hostname, args.substring, args.d)

0 comments on commit fddfdf9

Please sign in to comment.