-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.py
executable file
·57 lines (47 loc) · 1.35 KB
/
listen.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
#!/usr/bin/env python3
""" usage: ./listen SHELLDIR
SHELLDIR points to a directory with sscripts
"""
from docopt import docopt
from socket import *
from glob import glob
import subprocess
from os import listdir, access, X_OK
from os.path import isfile, join
import logging as log
log.basicConfig(level=log.DEBUG)
def run_shelldir(d):
log.debug("running all in {}".format(d))
for fname in listdir(d):
f = join(d,fname)
if (isfile(f) and access(f, X_OK)):
print("running {}".format(f))
subprocess.call([f])
else:
print("will not run {}".format(f))
def main():
d = docopt(__doc__)["SHELLDIR"]
listen_socket(d)
def listen_socket(d):
s=socket(AF_INET, SOCK_DGRAM)
s.bind(('',2342))
log.debug("begin listen")
while True:
pkg,ipport = s.recvfrom(1024)
try:
typ = pkg[0]
if typ == 0x0a:
log.debug("got hauptschalter")
action = pkg[1]
if action == 2:
log.debug("pre-shutdown")
elif action == 1:
log.debug("shutdown")
run_shelldir(d)
elif action == 0:
print("shutdown successful")
except Exception as e:
log.error(e)
pass
if __name__ == "__main__":
main()