aboutsummaryrefslogtreecommitdiffstats
path: root/cmdToTarget.py
blob: ccf042af50223e89145789848806c8540c566747 (plain)
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
# send cmds to a target machine:
#   docmd(machine, cmd)
#   return (1, output) when succeeds, (0, []) when fails

import os, time, popen2, tempfile;

secsForWait = 30;
def waitForJob(p, machine):
    count = 0;
    while (p.poll()):
        time.sleep(1);
        count += 1;
        if count == secsForWait:
            killJobs = "kill -9 `ps -ef | grep %s | egrep -v 'grep|python' "\
                       "| awk '{ print $2}'` 2>/dev/null" % machine;
            os.system(killJobs);
            return 0;
    return 1;

def docmd(rsh, machine, cmd):
    # if scheme tmpfile is not used, p will fail to return if the
    # output of cmd is large
    tmpFile = tempfile.mktemp();
    p = popen2.Popen3("%s %s '%s' > %s 2>/dev/null" % (rsh, machine, cmd, tmpFile));

    if not waitForJob(p, machine): return (0, []);

    lines = open(tmpFile).readlines();
    os.remove(tmpFile);
    return (1, lines);

# check if the machine can be talked to by rsh
def isMachineOK(rsh, machine):
    p = popen2.Popen3("%s %s 'date 2>/dev/null'" % (rsh, machine));

    if not waitForJob(p, machine): return False;
    
    lists = p.fromchild.readlines();
    
    if len(lists)==0: return False;
    return True;