Skip to content

Instantly share code, notes, and snippets.

@feliperyan
Created October 22, 2020 04:52
Show Gist options
  • Save feliperyan/a7b69c2d1818b8cec34dc5d4a2bf2fe4 to your computer and use it in GitHub Desktop.
Save feliperyan/a7b69c2d1818b8cec34dc5d4a2bf2fe4 to your computer and use it in GitHub Desktop.
Notify me on Slack when my external IP changes
from urllib.request import urlopen
from urllib import request
resp = urlopen("https://api.ipify.org")
ip = resp.read().decode('utf-8').strip()
f = open('last_ip.txt', 'r+')
last_ip = f.read().strip()
if (ip != last_ip):
f.seek(0)
f.write(ip)
f.truncate()
print(f'Old ip: {last_ip} new ip: {ip}')
# call webhook
data_utf8 = f'{{"text": "New IP: *{ip}*"}}'
data_utf8 = data_utf8.encode('utf-8')
req = request.Request('https://hooks.slack.com/services/your_long_string_for_webhooks_here', data=data_utf8)
resp = request.urlopen(req)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment