6 ssh_options = { 'StrictHostKeyChecking':'no',
8 'PasswordAuthentication':'no',
9 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
10 from select import select
14 class Sopen(subprocess.Popen):
15 def kill(self, signal = signal.SIGTERM):
16 os.kill(self.pid, signal)
18 def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
19 lin, lout, lerr = select([stream], [], [], timeout)
21 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
23 return stream.read(count)
29 def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
31 #print "CMD.run_noexcept(%s)" % cmd
33 return CMD.run(self,cmd,timeout)
34 except ExceptionTimeout:
35 import traceback; print traceback.print_exc()
36 return ("", "SCRIPTTIMEOUT")
38 def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
39 (o,e) = self.run(cmd, timeout)
42 if self.s.returncode is None:
44 return self.s.returncode
46 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
48 #print "CMD.run(%s)" % cmd
49 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
51 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
52 #print "calling select(%s)" % timeout
53 lout, lin, lerr = select([f_out], [], [f_err], timeout)
54 #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
55 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
56 # Reached a timeout! Nuke process so it does not hang.
58 s.kill(signal.SIGKILL)
59 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
62 #print len(lin), len(lout), len(lerr)
68 #print "reading from f_out"
69 if len(lout) > 0: o_value = f_out.read()
70 #print "reading from f_err"
71 if len(lerr) > 0: e_value = f_err.read()
73 #print "striping output"
74 o_value = o_value.strip()
75 e_value = e_value.strip()
77 #print "OUTPUT", o_value, e_value
79 #print "closing files"
86 #print "after s.kill()"
88 # no such process, due to it already exiting...
91 #print o_value, e_value
92 return (o_value, e_value)
94 def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
96 #print "CMD.run(%s)" % " ".join(args)
97 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
99 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
100 lout, lin, lerr = select([f_out], [], [f_err], timeout)
101 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
102 # Reached a timeout! Nuke process so it does not hang.
103 s.kill(signal.SIGKILL)
104 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
105 o_value = f_out.read()
107 if o_value == "": # An error has occured
108 e_value = f_err.read()
110 o_value = o_value.strip()
111 e_value = e_value.strip()
119 # no such process, due to it already exiting...
122 return (o_value, e_value)
126 def __init__(self, user, host, port=22, options = ssh_options):
127 self.options = options
133 def __options_to_str(self):
135 for o,v in self.options.iteritems():
136 options = options + "-o %s=%s " % (o,v)
139 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
140 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
141 self.user, self.host, cmd)
142 #print "SSH.run(%s)" % cmd
143 return CMD.run(self, cmd, timeout)
145 def get_file(self, rmt_filename, local_filename=None):
146 if local_filename == None:
147 local_filename = "./"
148 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(),
149 self.user, self.host,
150 rmt_filename, local_filename)
152 # errors will be on stderr,
153 # success will have a blank stderr...
154 return CMD.run_noexcept(self, cmd)
156 def run_noexcept(self, cmd):
157 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
158 self.user, self.host, cmd)
159 #print "SSH.run_noexcept(%s)" % cmd
160 return CMD.run_noexcept(self, cmd)
162 def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
163 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
164 self.user, self.host, cmd)
165 #print "SSH.run_noexcept2(%s)" % cmd
166 r = CMD.run_noexcept(self, cmd, timeout)
168 # XXX: this may be resulting in deadlocks... not sure.
169 #if self.s.returncode is None:
171 # self.s.kill(signal.SIGKILL)
173 # self.ret = self.s.returncode
178 def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
179 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
180 self.user, self.host, cmd)
181 #print "SSH.system2(%s)" % cmd
182 return CMD.system(self, cmd, timeout)
185 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
186 self.user, self.host, cmd)
189 (f_in, f_out, f_err) = os.popen3(cmd)
192 if value == "": # An error has occured
194 value = value.strip()
197 print " == %s" % value
206 self.start = time.time()
209 self.end = time.time()
210 t = self.end-self.start
214 self.end = time.time()
215 t = self.end-self.start
216 self.start = self.end