summaryrefslogtreecommitdiffstats
path: root/gearman/lsWorker.py
blob: 52fc85767b7836567692ae3304db3084083036f0 (plain) (blame)
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
#!/usr/bin/python

from gearman import GearmanWorker
import subprocess
import json
import time
import re

def lsWorker(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({"err":"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 = "ls -al /proc/"

    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
        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['ls'] = []
        for l in xrange(len(lines)):
	    if l == 0:
                continue
            rowentries = lines[l].split()
            if len(rowentries) == 8:
                (permissions, directories, owner, group, size, date, time, name) = rowentries
            data = {'permissions': permissions, 'directories': directories, 'owner': owner, 'group': group, 'size': size, 'date': date, 'time': time, 'name': name}
            ret['ls'].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('ls', lsWorker)
worker.work()