summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gearman/osWorker.py74
-rw-r--r--gearman/restartWorker.py69
-rw-r--r--gearman/shutdownWorker.py69
-rw-r--r--gearman/whoWorker.py80
4 files changed, 292 insertions, 0 deletions
diff --git a/gearman/osWorker.py b/gearman/osWorker.py
new file mode 100644
index 0000000..94077d9
--- /dev/null
+++ b/gearman/osWorker.py
@@ -0,0 +1,74 @@
+#!/usr/bin/python
+
+from gearman import GearmanWorker
+import subprocess
+import json
+import time
+import re
+
+def osWorker(worker, job):
+ ret = {"rawoutput": "", "rc": -1}
+ target = job.data.strip()
+
+ validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
+ validHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"
+ if not (re.match(validIpAddressRegex, target) or re.match(validHostnameRegex, target)):
+ print 'malformed request'
+ return json.dumps({"error":"invalid parameter - must be ip or host address", "alive":False}) + "\n"
+
+
+ host = target
+ port = 22
+ user = "root"
+ sshopts = "-o PasswordAuthentication=no -a"
+ keyfile = "/etc/systemd/workers.ssh/id_rsa"
+ remotecmd = "lsb_release -a"
+
+ cmd = "ssh -p {p} -l {l} -i {i} {o} {h} {r}".format(p=port, l=user, i=keyfile, o=sshopts, h=host, r=remotecmd)
+ print cmd
+
+
+ sshCommand = subprocess.Popen(["sh", "-c", "%s" % cmd],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ while sshCommand.returncode == None:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ time.sleep(0.05)
+ sshCommand.poll()
+
+ while True:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ else:
+ break
+
+ ret['rawoutput'] = ret['rawoutput'].strip() # get rid of newlines
+ rc = ret['rc'] = sshCommand.returncode
+
+ if rc == 0:
+ lines = ret['rawoutput'].splitlines()
+ for l in xrange(len(lines)):
+ (key, value) = lines[l].split(":")
+ ret[key.strip()] = value.strip()
+ print 'remote command successful.'
+ return json.dumps(ret) + "\n"
+ else:
+ ret['err'] = ""
+ while True:
+ out = sshCommand.stderr.readline()
+ if out != "":
+ ret['err'] += out
+ else:
+ break
+ print 'remote command failed'
+ return json.dumps(ret) + "\n"
+
+
+worker = GearmanWorker(["127.0.0.1"])
+worker.register_task('os', osWorker)
+worker.work() \ No newline at end of file
diff --git a/gearman/restartWorker.py b/gearman/restartWorker.py
new file mode 100644
index 0000000..f449c19
--- /dev/null
+++ b/gearman/restartWorker.py
@@ -0,0 +1,69 @@
+#!/usr/bin/python
+
+from gearman import GearmanWorker
+import subprocess
+import json
+import time
+import re
+
+def restartWorker(worker, job):
+ ret = {"rawoutput": "", "rc": -1}
+ target = job.data.strip()
+
+ validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
+ validHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"
+ if not (re.match(validIpAddressRegex, target) or re.match(validHostnameRegex, target)):
+ print 'malformed request'
+ return json.dumps({"error":"invalid parameter - must be ip or host address", "alive":False}) + "\n"
+
+
+ host = target
+ port = 22
+ user = "root"
+ sshopts = "-o PasswordAuthentication=no -a"
+ keyfile = "/etc/systemd/workers.ssh/id_rsa"
+ remotecmd = "shutdown -r now"
+
+ cmd = "ssh -p {p} -l {l} -i {i} {o} {h} {r}".format(p=port, l=user, i=keyfile, o=sshopts, h=host, r=remotecmd)
+ print cmd
+
+ sshCommand = subprocess.Popen(["sh", "-c", "%s" % cmd],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ while sshCommand.returncode == None:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ time.sleep(0.05)
+ sshCommand.poll()
+
+ while True:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ else:
+ break
+
+ ret['rawoutput'] = ret['rawoutput'].strip() # get rid of newlines
+ rc = ret['rc'] = sshCommand.returncode
+
+ if rc == 0:
+ print 'remote command successful.'
+ return json.dumps(ret) + "\n"
+ else:
+ ret['err'] = ""
+ while True:
+ out = sshCommand.stderr.readline()
+ if out != "":
+ ret['err'] += out
+ else:
+ break
+ print 'remote command failed'
+ return json.dumps(ret) + "\n"
+
+
+worker = GearmanWorker(["127.0.0.1"])
+worker.register_task('restart', restartWorker)
+worker.work() \ No newline at end of file
diff --git a/gearman/shutdownWorker.py b/gearman/shutdownWorker.py
new file mode 100644
index 0000000..d73a0bd
--- /dev/null
+++ b/gearman/shutdownWorker.py
@@ -0,0 +1,69 @@
+#!/usr/bin/python
+
+from gearman import GearmanWorker
+import subprocess
+import json
+import time
+import re
+
+def shutdownWorker(worker, job):
+ ret = {"rawoutput": "", "rc": -1}
+ target = job.data.strip()
+
+ validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
+ validHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"
+ if not (re.match(validIpAddressRegex, target) or re.match(validHostnameRegex, target)):
+ print 'malformed request'
+ return json.dumps({"error":"invalid parameter - must be ip or host address", "alive":False}) + "\n"
+
+
+ host = target
+ port = 22
+ user = "root"
+ sshopts = "-o PasswordAuthentication=no -a"
+ keyfile = "/etc/systemd/workers.ssh/id_rsa"
+ remotecmd = "shutdown -h now"
+
+ cmd = "ssh -p {p} -l {l} -i {i} {o} {h} {r}".format(p=port, l=user, i=keyfile, o=sshopts, h=host, r=remotecmd)
+ print cmd
+
+ sshCommand = subprocess.Popen(["sh", "-c", "%s" % cmd],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ while sshCommand.returncode == None:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ time.sleep(0.05)
+ sshCommand.poll()
+
+ while True:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ else:
+ break
+
+ ret['rawoutput'] = ret['rawoutput'].strip() # get rid of newlines
+ rc = ret['rc'] = sshCommand.returncode
+
+ if rc == 0:
+ print 'remote command successful.'
+ return json.dumps(ret) + "\n"
+ else:
+ ret['err'] = ""
+ while True:
+ out = sshCommand.stderr.readline()
+ if out != "":
+ ret['err'] += out
+ else:
+ break
+ print 'remote command failed'
+ return json.dumps(ret) + "\n"
+
+
+worker = GearmanWorker(["127.0.0.1"])
+worker.register_task('shutdown', shutdownWorker)
+worker.work() \ No newline at end of file
diff --git a/gearman/whoWorker.py b/gearman/whoWorker.py
new file mode 100644
index 0000000..f9694f3
--- /dev/null
+++ b/gearman/whoWorker.py
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+
+from gearman import GearmanWorker
+import subprocess
+import json
+import time
+import re
+
+def whoWorker(worker, job):
+ ret = {"rawoutput": "", "rc": -1}
+ target = job.data.strip()
+
+ validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
+ validHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"
+ if not (re.match(validIpAddressRegex, target) or re.match(validHostnameRegex, target)):
+ print 'malformed request'
+ return json.dumps({"error":"invalid parameter - must be ip or host address", "alive":False}) + "\n"
+
+
+ host = target
+ port = 22
+ user = "root"
+ sshopts = "-o PasswordAuthentication=no -a"
+ keyfile = "/etc/systemd/workers.ssh/id_rsa"
+ remotecmd = "who -u"
+
+ cmd = "ssh -p {p} -l {l} -i {i} {o} {h} {r}".format(p=port, l=user, i=keyfile, o=sshopts, h=host, r=remotecmd)
+ print cmd
+
+
+ sshCommand = subprocess.Popen(["sh", "-c", "%s" % cmd],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ while sshCommand.returncode == None:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ time.sleep(0.05)
+ sshCommand.poll()
+
+ while True:
+ out = sshCommand.stdout.readline()
+ if out != "":
+ ret['rawoutput'] += out
+ else:
+ break
+
+ ret['rawoutput'] = ret['rawoutput'].strip() # get rid of newlines
+ rc = ret['rc'] = sshCommand.returncode
+
+ if rc == 0:
+ lines = ret['rawoutput'].splitlines()
+ ret['who'] = []
+ for l in xrange(len(lines)):
+ rowentries = lines[l].split()
+ if len(rowentries) == 7:
+ (user, tty, login_date, login_time, foo ,pid, display ) = rowentries
+ if len(rowentries) == 6:
+ (user, tty, login_date, login_time, foo ,pid ) = rowentries
+ data = {'user': user, 'tty': tty, 'login_date':login_date, 'login_time':login_time, 'pid':pid, 'display':display}
+ ret['who'].append(data)
+ print 'remote command successful.'
+ return json.dumps(ret) + "\n"
+ else:
+ ret['err'] = ""
+ while True:
+ out = sshCommand.stderr.readline()
+ if out != "":
+ ret['err'] += out
+ else:
+ break
+ print 'remote command failed'
+ return json.dumps(ret) + "\n"
+
+
+worker = GearmanWorker(["127.0.0.1"])
+worker.register_task('who', whoWorker)
+worker.work() \ No newline at end of file