Last active
December 17, 2020 18:48
-
-
Save drmcarvalho/a577a6d5009e65545ec07a989dac8425 to your computer and use it in GitHub Desktop.
Detecta mudança no IP externo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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