07
December
This script creates a file called ipinfo.db that contains your current WAN IP address. When this script is scheduled to run, it will check to see if if your WAN IP has changed. If it has, it will report back to your Discord webhook.
- You must register an account with https://ipinfo.io.
- You will need to download Python from https://www.python.org/downloads/
- You will need to download pip for Python https://pip.pypa.io/en/stable/installing/
- Install ipinfo with this following command
pip install ipinfo
Script (Copy and paste into a new file ex: checkIP.py)
#You will require Python, pip and ipinfo (installed from pip)
import ipinfo
import requests #dependency
import json
#Your Discord Webhook
url = 'https://discord.com/api/webhooks/123456789123456789/yourwebhook'
#Write Variables
access_token = '123456789yourtoken' #Access Token from your account at https://ipinfo.io/account
handler = ipinfo.getHandler(access_token)
details = handler.getDetails()
ip = details.ip
#Check if file exists, if not create a new file
try:
f = open("ipinfo.db")
except IOError:
f = open( 'ipinfo.db', 'w' )
print("Creating new file ipinfo.db file.")
finally:
f.close()
previous_ip_file = open('ipinfo.db')
previous_ip = previous_ip_file.read()
print("Previous IP:" + previous_ip)
print("Current IP: " + ip)
f = open( 'ipinfo.db', 'w' )
f.write( ip )
f.close()
if(ip != previous_ip):
print("IP address has changed")
data = {}
data["content"] = "The server IP is: " + ip
data["username"] = "Server"
result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print("Success, code {}.".format(result.status_code))
else:
print("IP address has not changed")