/
Publish_My_Ip.py
81 lines (67 loc) · 2.09 KB
/
Publish_My_Ip.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
'''
this program gets your device local and global ip
and sends in via MQTT protocol every hour OR every time the ip changes
'''
# import suppurting libs
import paho.mqtt.publish as publish
import ipgetter
import socket
import datetime
import time
HOSTNAME = "broker.mqttdashboard.com"
PORT = 1883
'''
getting my local and global ip address.
'''
def getAddress():
# global ip address of device
globalIp = ipgetter.myip()
# local ip address of device
localIp = socket.gethostbyname(socket.gethostname())
output = 'Global_IP:' + globalIp + '//' + 'Local_Ip:' + localIp
return output
'''
publishing a single (encrypted) message to the broker with thee device IP.
'''
def pubIp():
encPayload = encryptMessage(getAddress())
pubMsg('babykeeper/Ras_Pi', encPayload)
'''
publishing a single message to the broker.
'''
def pubMsg(topic, msg):
publish.single(topic, msg, 0, False, HOSTNAME, PORT)
'''
encrypting a message using Ceaser Cypher and a unique key.
'''
def encryptMessage(message):
key = 16 # give's '16' as a key.
letters = "8&YC9ZvMaxP@wOz|%*sf3QN0G/cp+U5$>gDorLmhE1l#eJ,*yWbdB_2KV(n=i?HSuX^{TI4;jq}'FA[:).t!<kR]67"
letterslen = len(letters)
translated = ""
for m in message:
if m in letters:
myval = letters.find(m)
myval = myval + key
if myval >= letterslen:
myval -= letterslen
elif myval < 0:
myval += letterslen
translated += letters[myval]
else:
translated += m
return translated
def main():
currentAddress = getAddress()
currentHour = time.localtime()[3]
pubIp()
# main loop to check if address changed, and if so - publish them again.
while True:
if currentAddress != getAddress() or time.localtime()[3] != currentHour:
pubIp()
currentAddress = getAddress()
currentHour = time.localtime()[3]
#else: publish.single("babykeeper/Rasp_Ip","same Ip", hostname="broker.mqttdashboard.com")
if __name__ == "__main__":
main()