-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-ip-daemon.py
110 lines (84 loc) · 2.77 KB
/
no-ip-daemon.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, time, json, urllib2, base64, re
_SETTINGS = dict()
_OLD_IP = '1.1.1.1'
def createDaemon():
global _SETTINGS, _OLD_IP
"""
This function create a service/Daemon that will execute a det. task
"""
jsonfile = open('settings.json')
_SETTINGS = json.load(jsonfile)
jsonfile.close()
#print json.dumps(_SETTINGS)
try:
pidfile = open(_SETTINGS['pidfile'])
print 'Error: pid file already exists'
pidfile.close()
os._exit(1)
except IOError, error:
print 'Forking new process'
try:
# Store the Fork PID
pid = os.fork()
if pid > 0:
print 'PID: %d' % pid
with open(_SETTINGS['pidfile'], 'w') as pidfile:
pidfile.write(str(pid))
pidfile.close()
os._exit(0)
except OSError, error:
print 'Unable to fork. Error: %d (%s)' % (error.errno, error.strerror)
os._exit(1)
doTask()
def doTask():
global _OLD_IP
"""
This function create a task that will be a daemon
"""
# Open the file in write mode
file = open(_SETTINGS['log'], 'w')
# Start the write
while True:
_new_ = urllib2.urlopen(_SETTINGS['ipresolve']).read().strip()
print >> file, time.ctime() + ' new ip: ' + _new_ + ' old ip: ' + _OLD_IP
print >> file, 'next update check in %d seconds.' % _SETTINGS['interval']
if _new_ == _OLD_IP:
print >> file, 'ip has not changed. no need to update.'
else:
print >> file, 'ip has changed. trying to update.'
_updateurl_ = _SETTINGS['updateurl']
""" Update current public ip """
_url_ = _updateurl_.format(hostname=_SETTINGS['hostname'], ip=_new_)
_user_data_ = "Basic " + (_SETTINGS['username'] + ":" + _SETTINGS['password']).encode("base64").rstrip()
print >> file, _url_ + " " + _user_data_
req = urllib2.Request(_url_)
req.add_header("Authorization", _user_data_)
req.add_header('User-agent', 'Python no-ip-daemon v1.0 neskola@gmail.com')
print >> file, req
try:
res = urllib2.urlopen(req)
res_txt = res.read()
res_code = res.getcode()
print >> file, 'response is ' + res_txt + ' ' + str(res_code)
if re.match('^(good)|(nochg).*', res_txt):
print >> file, '... Succeed'
_OLD_IP = _new_
else:
print >> file, '... Failed. Exiting.'
file.flush()
file.close()
os._exit(1)
except urllib2.HTTPError as e:
print >> file, "HTTP exception {0},{1}".format(e.code, e.reason)
file.flush()
file.close()
os._exit(1)
file.flush()
time.sleep(_SETTINGS['interval'])
# Close the file
file.close()
if __name__ == '__main__':
# Create the Daemon
createDaemon()