IP Checker

> Adding IP checker files and updating README
This commit is contained in:
2023-12-05 18:03:54 -08:00
parent 4bd644637a
commit e7e687df77
3 changed files with 137 additions and 31 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/python3
#
#
#
# Make sure to set you credentials in the ip_checker_config.ini file
#
#
@@ -42,9 +42,11 @@ logs_folder = 'logs'
if not os.path.exists(logs_folder):
os.makedirs(logs_folder)
# Getting the log file path
def get_log_file_path():
return os.path.join(logs_folder, 'ip_checker.log')
# Rotate and archive log files every 24hrs
def rotate_log_files(log_file):
now = datetime.datetime.now()
timestamp = now.strftime('%Y-%m-%d')
@@ -55,10 +57,12 @@ def rotate_log_files(log_file):
# Rename the current log file to the rotated log file
os.rename(log_file, rotated_log_path)
# Formating time
def format_remaining_time(remaining_time):
minutes, seconds = divmod(remaining_time.seconds, 60)
return f'{minutes} minutes {seconds} seconds'
# Getitng external IP address from internet provider
def get_external_ip():
try:
response = requests.get(f'{get_ip_address}')
@@ -69,6 +73,7 @@ def get_external_ip():
logger.info(' ')
return None
# Getting containers current IP address
def get_vpn_container_ip(container_name):
try:
cmd = ["docker", "exec", container_name, "curl", "-s", get_ip_address]
@@ -83,6 +88,7 @@ def get_vpn_container_ip(container_name):
logger.info(' ')
return None
# Function to stop the container that depends on VPN connnection.
def stop_dependent_container(container_name):
"""
Stop the specified Docker container.
@@ -95,6 +101,7 @@ def stop_dependent_container(container_name):
logger.warning(f'[WARN] - Failed stopping container {container_name}: {e}')
logger.info(' ')
# Notifacation alert for ntfy
def send_ntfy_alert(message, urgent=False):
"""
Send a notification using ntfy.
@@ -109,6 +116,7 @@ def send_ntfy_alert(message, urgent=False):
logger.warning(f'[WARN] - Failed to send ntfy notification: {e}')
logger.info(' ')
# Notifacation alert for pushover
def send_pushover_alert(app_token, user_key):
# Define notification title and message
@@ -135,7 +143,7 @@ def send_pushover_alert(app_token, user_key):
logger.info(' ')
# Function to send an alert to the specified notification service
def send_alert(alert_method):
if alert_method == 'ntfy':
# Send urgent ntfy notification
@@ -242,32 +250,3 @@ if __name__ == "__main__":
main()