-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopd.py
70 lines (66 loc) · 2.06 KB
/
loopd.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
import sys, os
import client as cli
import datetime
import time
import config
f = open(os.path.split(os.path.realpath(__file__))[0] + "/log", "a")
def writeLog(msg):
content = ""
if msg == "\n":
content = "\n"
else:
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
content = "[" + t + "] " + msg + "\n"
f.write(content)
f.flush()
def main():
while True:
cookies = None
loginSuccess = False
writeLog("Log in...")
while not loginSuccess:
try:
cookies = cli.login(config)
loginSuccess = True
except:
writeLog("Login failed. Retry in " + str(config.retryDelay) + " seconds...")
time.sleep(config.retryDelay)
writeLog("Log in ok!")
while True:
writeLog("Start a scan...")
try:
cli.ipLogout(cookies, config)
except:
break
time.sleep(config.interval)
# writeLog("==============================================")
if __name__ == "__main__":
# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
pid = None
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent, print eventual PID before
print "Daemon PID %d" % pid
writeLog("\n")
writeLog("Daemon start at PID: %d" % pid)
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
# start the daemon main loop
main()