From a5309fed914fdaa7697f2d369e7dcd02309063ab Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 4 Nov 2015 12:30:44 -0500 Subject: initial import --- cmdToTarget.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 cmdToTarget.py (limited to 'cmdToTarget.py') 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; + -- cgit v1.2.3-70-g09d2