def onTriSend(self, message, sutAddress): try: # FIXME: # Should go to a configured codec instance instead. # (since we modify the message here...) if not message.has_key('version'): message['version'] = self['version'] try: (encodedMessage, summary) = CodecManager.encode('http.request', message) except Exception as e: raise ProbeImplementationManager.ProbeException( 'Invalid request message format: cannot encode HTTP request:\n%s' % ProbeImplementationManager.getBacktrace()) # Connect if needed if not self.isConnected(): self.connect() # Send our payload self._httpConnection.send(encodedMessage) self.logSentPayload(summary, encodedMessage, "%s:%s" % self._httpConnection.getpeername()) # Now wait for a response asynchronously self.waitResponse() except Exception as e: raise ProbeImplementationManager.ProbeException( 'Unable to send HTTP request: %s' % str(e))
def onTriSend(self, message, sutAddress): """ Internal SSH probe message: { 'cmd': 'execute', 'command': string, 'host': string, 'username': string, 'password': string, [ 'timeout': float in s, 5.0 by default ] } { 'cmd': 'cancel' } The timeout is the maximum amount of time allowed to connect and start executing the command. The command itself may last forever. """ self.getLogger().debug("onTriSend(%s, %s)" % (unicode(message), unicode(sutAddress))) if not (isinstance(message, tuple) or isinstance(message, list)) and not len(message) == 2: raise Exception("Invalid message format") cmd, value = message if cmd == 'execute': m = { 'cmd': 'execute', 'command': value, 'host': self['host'], 'username': self['username'], 'password': self['password'], 'workingdir': self['working_dir'] } elif cmd == 'cancel': m = {'cmd': 'cancel'} else: raise Exception("Invalid message format") try: self._checkArgs(m, [('cmd', None)]) cmd = m['cmd'] if cmd == 'cancel': return self.cancelCommand() elif cmd == 'execute': self._checkArgs(m, [('command', None), ('host', None), ('username', None), ('password', None), ('timeout', self['timeout']), ('workingdir', None)]) command = m['command'] host = m['host'] username = m['username'] password = m['password'] timeout = m['timeout'] workingdir = m['workingdir'] try: self.executeCommand(command, username, host, password, timeout, workingdir) except Exception as e: self.triEnqueueMsg(str(e)) except Exception as e: raise ProbeImplementationManager.ProbeException( self._getBacktrace())
def onTriSend(self, message, sutAddress): try: # FIXME: # Should go to a configured codec instance instead. # (since we modify the message here... should be a copy instead) if not message.has_key('version'): message['version'] = self['version'] if not message.has_key('headers'): message['headers'] = {} cseq = None # Non-strict mode: CSeq management: we add one if none is found if not self['strict_mode']: # Look for a CSeq for k, v in message['headers'].items(): if k.lower() == 'cseq': cseq = str(v) if cseq is None: # Generate and set a cseq message['headers']['CSeq'] = self.generateCSeq() cseq = str(message['headers']['CSeq']) try: encodedMessage, summary = CodecManager.encode( 'rtsp.request', message) except Exception as e: raise ProbeImplementationManager.ProbeException( 'Invalid request message format: cannot encode RTSP request' ) # Connect if needed if not self.isConnected(): self.connect() # Send our payload self._connection.send(encodedMessage) self.logSentPayload(summary, encodedMessage, "%s:%s" % self._connection.getpeername()) # Now wait for a response asynchronously self.waitResponse(cseq=cseq) except Exception as e: raise ProbeImplementationManager.ProbeException( 'Unable to send RTSP request: %s' % str(e))
def onTriSend(self, message, sutAddress): (cmd, args) = message if cmd == 'startWatchingDirs': self._checkArgs(args, [('dirs', None), ('interval', 1.0), ('patterns', [r'.*'])]) compiledPatterns = [re.compile(x) for x in args['patterns']] self.startWatching(dirs=args['dirs'], interval=args['interval'], patterns=compiledPatterns) elif cmd == 'stopWatchingDirs': self.stopWatching() else: raise ProbeImplementationManager.ProbeException( "Invalid message format (%s)" % cmd)
def onTriSend(self, message, sutAddress): (cmd, args) = message if cmd == 'startWatchingFiles': self._checkArgs(args, [ ('files', None), ('interval', 1.0), ('patterns', [ r'.*' ])] ) compiledPatterns = [ re.compile(x) for x in args['patterns']] # Glob files here - glob.glob() blocks when called from the watching thread (?! - blocked in fnmatch.filter: import os,posixpath) files = [] for arg in args['files']: files += glob.glob(arg) self.startWatching(files = files, interval = args['interval'], patterns = compiledPatterns) elif cmd == 'stopWatchingFiles': self.stopWatching() else: raise ProbeImplementationManager.ProbeException("Invalid message format (%s)" % cmd)
def _send(self, conn, data): encoder = self['default_encoder'] if encoder: try: (data, summary) = CodecManager.encode(encoder, data) except Exception: raise ProbeImplementationManager.ProbeException( 'Cannot encode outgoing message using defaut encoder:\n%s' % ProbeImplementationManager.getBacktrace()) self.logSentPayload(summary, data, "%s:%s" % conn.socket.getpeername()) else: self.logSentPayload("TCP data", data, "%s:%s" % conn.socket.getpeername()) conn.socket.send(data)