-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (34 loc) · 1.23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import public_ip as ip
from tinydb import TinyDB, Query
import telegram
import time
db = TinyDB('/tmp/ip_database.json') # TODO change DB folder to persistant one
def get_public_ip():
return ip.get()
def get_last_ip_address():
Ip = Query()
result = db.search(Ip.ip_address.exists())
if result:
result.sort(key=lambda x: x['timestamp'], reverse=True)
return result[0]['ip_address']
else:
return None
def add_ip_to_database(ip_address):
timestamp = int(time.time())
Ip = Query()
existing_ip = db.get(Ip.ip_address == ip_address)
last_ip_address = get_last_ip_address()
if existing_ip:
db.update({'timestamp': timestamp}, Ip.ip_address == ip_address)
return False
else:
db.insert({'ip_address': ip_address, 'timestamp': timestamp})
if ip_address != last_ip_address:
telegram.send_message(f"New host IP {ip_address}","") # TODO print hostname whose IP has changed
return ip_address != last_ip_address
def main():
ip_address = get_public_ip()
add_ip_to_database(ip_address)
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')}: Current IP -> {ip_address}") # TODO print hostname whose IP has changed
if __name__ == "__main__":
main()