Skip to content

Instantly share code, notes, and snippets.

@drmcarvalho
Last active December 17, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmcarvalho/a577a6d5009e65545ec07a989dac8425 to your computer and use it in GitHub Desktop.
Save drmcarvalho/a577a6d5009e65545ec07a989dac8425 to your computer and use it in GitHub Desktop.
Detecta mudança no IP externo
#!/usr/bin/env python3
from requests import get
from time import sleep
from json import dump, load
from sys import exit
"""
Faca o arquivo executavel
$ sudo chmod u+x your_script.py
"""
def getIP():
return get("https://ipapi.co/ip/").text
def saveIP(ip):
with open("data.json", "w") as f:
data = {"ip": ip}
dump(data, f)
def getIPFromFile():
with open("data.json") as f:
data = load(f)
return data
def init():
data = getIPFromFile()
if "ip" not in data:
ipAtual = getIP()
if not ipAtual:
raise Exception("Nao foi possivel obter o IP!")
saveIP(ipAtual)
else:
ipAtual = data["ip"]
while True:
ipAux = getIP()
if ipAtual != ipAux:
print("IP foi alterado! Novo IP: %s" % ipAux)
else:
print("IP nao alterado")
sleep(30)
def main():
try:
init()
return 0
except Exception as e:
return "error: %s" % e
if __name__ == "__main__":
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment