aboutsummaryrefslogtreecommitdiffstats
path: root/cmdToTarget.py
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@serenitascapital.com>2015-11-04 12:30:44 -0500
committerGuillaume Horel <guillaume.horel@serenitascapital.com>2015-11-04 12:30:44 -0500
commita5309fed914fdaa7697f2d369e7dcd02309063ab (patch)
tree975bb588c4d9072ae1158ab670bf9fa851abd6f4 /cmdToTarget.py
downloadmrsync-a5309fed914fdaa7697f2d369e7dcd02309063ab.tar.gz
initial import
Diffstat (limited to 'cmdToTarget.py')
-rwxr-xr-xcmdToTarget.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/cmdToTarget.py b/cmdToTarget.py
new file mode 100755
index 0000000..ccf042a
--- /dev/null
+++ b/cmdToTarget.py
@@ -0,0 +1,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;
+