|
|
@@ -0,0 +1,88 @@
|
|
|
+#!/usr/bin/python3
|
|
|
+
|
|
|
+import os
|
|
|
+import errno
|
|
|
+import re
|
|
|
+from commands.CmdExit import CmdExit
|
|
|
+from commands.CmdUpdate import CmdUpdate
|
|
|
+
|
|
|
+DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
+
|
|
|
+CONFIG = {
|
|
|
+ "PipePath": os.path.join(DIR, "../data/cmd-pipe"),
|
|
|
+ "CronFragment": os.path.join(DIR, "../data/cron-fragment.txt"),
|
|
|
+ "Running": True,
|
|
|
+}
|
|
|
+
|
|
|
+# try:
|
|
|
+# os.mkfifo(PIPE)
|
|
|
+# except OSError as oe:
|
|
|
+# if oe.errno != errno.EEXIST:
|
|
|
+# raise
|
|
|
+
|
|
|
+def execute(command):
|
|
|
+ if command == "EXIT":
|
|
|
+ global running
|
|
|
+ running = False
|
|
|
+ elif command == "UPDATE":
|
|
|
+ print("Updating...")
|
|
|
+ stream = os.popen('crontab -l')
|
|
|
+ crontab = stream.read()
|
|
|
+ result = crontab
|
|
|
+ begin = re.search("\s*#\s*BEGIN\(ALERTS\).*", crontab)
|
|
|
+ if begin:
|
|
|
+ result = crontab[:begin.span()[1]] + "\n"
|
|
|
+
|
|
|
+ end = re.search("\s*#\s*END\(ALERTS\).*", crontab)
|
|
|
+ if end:
|
|
|
+ result += "muahahah\ntest"
|
|
|
+ result += crontab[end.span()[0]:]
|
|
|
+
|
|
|
+ print(result)
|
|
|
+ else:
|
|
|
+ print("UNKNOWN COMMAND: {0}".format(command))
|
|
|
+
|
|
|
+
|
|
|
+def main(config):
|
|
|
+ # commandPipe = ''
|
|
|
+ # cronFragment = ''
|
|
|
+ # try:
|
|
|
+ # opts, args = getopt.getopt(argv,"h",["help","pipe=","cron="])
|
|
|
+ # except getopt.GetoptError:
|
|
|
+ # print 'cron-section-replace --pipe <command-pipe> --cron <cron-fragment>'
|
|
|
+ # sys.exit(2)
|
|
|
+ # for opt, arg in opts:
|
|
|
+ # if opt in ("-h", "--help"):
|
|
|
+ # print 'cron-section-replace --pipe <command-pipe> --cron <cron-fragment>'
|
|
|
+ # sys.exit()
|
|
|
+ # elif opt in ("--pipe"):
|
|
|
+ # commandPipe = arg
|
|
|
+ # elif opt in ("--cron):
|
|
|
+ # cronFragment = arg
|
|
|
+
|
|
|
+ # print(commandPipe)
|
|
|
+ # print(cronFragment)
|
|
|
+ # sys.exit()
|
|
|
+
|
|
|
+ commands = {
|
|
|
+ "EXIT": CmdExit(config),
|
|
|
+ "UPDATE": CmdUpdate(config)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ while config["Running"]:
|
|
|
+ print("Waiting for connection...")
|
|
|
+ with open(config["PipePath"]) as pipe:
|
|
|
+ print("Connection established")
|
|
|
+ while True:
|
|
|
+ cmd = pipe.read().strip()
|
|
|
+ if len(cmd) == 0:
|
|
|
+ break
|
|
|
+ if cmd in commands:
|
|
|
+ commands[cmd].execute()
|
|
|
+ else:
|
|
|
+ print("UNKNOWN COMMAND: {0}".format(cmd))
|
|
|
+ #execute(data.strip())
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main(CONFIG)
|