Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created August 3, 2021 01:00
Show Gist options
  • Save turnipsoup/aaa5c9f020367b6cd45289da821c30a1 to your computer and use it in GitHub Desktop.
Save turnipsoup/aaa5c9f020367b6cd45289da821c30a1 to your computer and use it in GitHub Desktop.
Checks your current IP against the last one, and alerts you in Discord if it changes.
import requests
import os.path
ip_address_file = "./last_ip_address.txt"
discord_webhook_url = "<webhook-url-goes-here>"
# Get current IP address from icanhazip
def get_current_ip() -> str:
r = requests.get("https://icanhazip.com")
return r.content.decode().strip()
# Check the locally saved IP address.
def get_last_ip() -> str:
if not os.path.isfile(ip_address_file):
write_new_ip()
return open(ip_address_file, "r").read().strip()
# If the IP returned from the request does not match the IP in the file, alert us in Discord.
def check_ip_and_alert() -> None:
current_ip = get_current_ip()
last_ip = get_last_ip()
if current_ip != last_ip:
send_data = {
"username": "Captain Hook",
"content": f"Lynnwood IP address has changed from {last_ip} to {current_ip}! Update what you need to update!"
}
requests.post(discord_webhook_url, json=send_data)
write_new_ip()
# Write the new IP to the file
def write_new_ip() -> None:
file = open(ip_address_file, "w")
file.write(get_current_ip())
file.close()
# Main
def main() -> None:
check_ip_and_alert()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment