def _suboption(self): subopt = self.sbdataq self.sbdataq = '' if len(subopt) != 3: logging.error("Bad suboption recieved: {!r}".format(subopt)) return if subopt[0] == COM_PORT_OPTION: comopt = subopt[1] if comopt == RESP_NOTIFY_LINESTATE: value = subopt[2] self._linestate = LineState(subopt[2]) elif comopt == RESP_NOTIFY_MODEMSTATE: self._modemstate = ModemState(subopt[2]) elif comopt == RESP_FLOWCONTROL_SUSPEND: self._suspended = True logging.warning("Telnet: requested to suspend tx.") elif comopt == RESP_FLOWCONTROL_RESUME: self._suspended = False logging.warning("Telnet: requested to resume tx.") else: logging.warning("Telnet: unhandled COM opton: {}".format( repr(subopt))) else: logging.warning("Telnet: unhandled subnegotion: {}".format( repr(subopt)))
def updaterow(modelname, entry_id, data): klass = get_model(modelname) dbrow = webhelpers.get_row(klass, entry_id) try: webhelpers.update_row_from_data(data, klass, dbrow) except types.ValidationError, err: webhelpers.dbsession.rollback() logging.error(err) return False
def respawn_callback(self, deadproc): """Callback that performs a respawn, for persistent services.""" if deadproc.exitstatus.status == 127: logging.error("process {!r} didn't start (NOT restarting).\n".format( # noqa deadproc.cmdline)) raise ProcessError("Process never started. Check command line.") elif not deadproc.exitstatus: logging.error("process {!r} died: %s (restarting in 1 sec.).\n".format( # noqa deadproc.cmdline, deadproc.exitstatus)) scheduler.add(self._respawn, 1.0, args=(deadproc,)) else: logging.info("process {!r} normal exit (NOT restarting).\n".format( deadproc.cmdline)) return None
def respawn_callback(self, deadproc): """Callback that performs a respawn, for persistent services.""" if deadproc.exitstatus.status == 127: logging.error( "process {!r} didn't start (NOT restarting).\n".format( # noqa deadproc.cmdline)) raise ProcessError("Process never started. Check command line.") elif not deadproc.exitstatus: logging.error("process {!r} died: %s (restarting in 1 sec.).\n". format( # noqa deadproc.cmdline, deadproc.exitstatus)) scheduler.add(self._respawn, 1.0, args=(deadproc, )) else: logging.info("process {!r} normal exit (NOT restarting).\n".format( deadproc.cmdline)) return None
def _process_rawq(self): buf = ['', ''] # data buffer and SB buffer try: while self._rawq: c = self._rawq_getchar() if not self.iacseq: if c == NULL: continue if c == "\021": continue if c != IAC: buf[self.sb] += c continue else: self.iacseq += c elif len(self.iacseq) == 1: if c in (DO, DONT, WILL, WONT): self.iacseq += c continue self.iacseq = '' if c == IAC: buf[self.sb] += c else: if c == SB: # SB ... SE start. self.sb = 1 self.sbdataq = '' elif c == SE: self.sb = 0 self.sbdataq += buf[1] buf[1] = '' self._suboption() else: logging.warning( 'Telnet: IAC {!r} not recognized'.format(c)) elif len(self.iacseq) == 2: cmd = self.iacseq[1] self.iacseq = '' if cmd in (DO, DONT, WILL, WONT): self._neg_option(cmd, c) else: logging.error("telnet bad command: {!r}".format(cmd)) except EOFError: self.iacseq = '' # Reset on EOF self.sb = 0 self._q += buf[0] self.sbdataq += buf[1]
def _process_rawq(self): buf = ['', ''] # data buffer and SB buffer try: while self._rawq: c = self._rawq_getchar() if not self.iacseq: if c == NULL: continue if c == "\021": continue if c != IAC: buf[self.sb] += c continue else: self.iacseq += c elif len(self.iacseq) == 1: if c in (DO, DONT, WILL, WONT): self.iacseq += c continue self.iacseq = '' if c == IAC: buf[self.sb] += c else: if c == SB: # SB ... SE start. self.sb = 1 self.sbdataq = '' elif c == SE: self.sb = 0 self.sbdataq += buf[1] buf[1] = '' self._suboption() else: logging.warning('Telnet: IAC {!r} not recognized'.format(c)) elif len(self.iacseq) == 2: cmd = self.iacseq[1] self.iacseq = '' if cmd in (DO, DONT, WILL, WONT): self._neg_option(cmd, c) else: logging.error("telnet bad command: {!r}".format(cmd)) except EOFError: self.iacseq = '' # Reset on EOF self.sb = 0 self._q += buf[0] self.sbdataq += buf[1]
def _suboption(self): subopt = self.sbdataq self.sbdataq = '' if len(subopt) != 3: logging.error("Bad suboption recieved: {!r}".format(subopt)) return if subopt[0] == COM_PORT_OPTION: comopt = subopt[1] if comopt == RESP_NOTIFY_LINESTATE: value = subopt[2] self._linestate = LineState(subopt[2]) elif comopt == RESP_NOTIFY_MODEMSTATE: self._modemstate = ModemState(subopt[2]) elif comopt == RESP_FLOWCONTROL_SUSPEND: self._suspended = True logging.warning("Telnet: requested to suspend tx.") elif comopt == RESP_FLOWCONTROL_RESUME: self._suspended = False logging.warning("Telnet: requested to resume tx.") else: logging.warning("Telnet: unhandled COM opton: {}".format(repr(subopt))) else: logging.warning("Telnet: unhandled subnegotion: {}".format(repr(subopt)))
Forks and execs a process as given by the command line argument. The process's stdio is connected to this instance via a pty, and can be read and written to by the instances read() and write() methods. That pty becomes the processes controlling terminal. """ def __init__(self, cmdline, logfile=None, env=None, callback=None, merge=1, pwent=None, async=False, devnull=False, _pgid=0): Process.__init__(self, cmdline, logfile, callback, async) if env: self.environment = env cmd = split_command_line(self.cmdline) try: pid, self._fd = os.forkpty() except OSError as err: logging.error("ProcessPty error: {}".format(err)) raise else: if pid == 0: # child sys.excepthook = sys.__excepthook__ if devnull: # Redirect standard file descriptors. sys.stdout.flush() sys.stderr.flush() os.close(sys.__stdin__.fileno()) os.close(sys.__stdout__.fileno()) os.close(sys.__stderr__.fileno()) # stdin always from /dev/null sys.stdin = open("/dev/null", 'r') os.dup2(sys.stdin.fileno(), 0) # log file is stdout and stderr, otherwise /dev/null
def updaterow(modelname, entry_id, data): klass = get_model(modelname) dbrow = webhelpers.get_row(klass, entry_id) try: webhelpers.update_row_from_data(data, klass, dbrow) except types.ValidationError, err: webhelpers.dbsession.rollback() logging.error(err) return False try: webhelpers.dbsession.commit() except (DataError, IntegrityError), err: webhelpers.dbsession.rollback() logging.error(err) return False return True def get_row(modelname, entry_id): klass = get_model(modelname) return webhelpers.get_row(klass, entry_id) def get_ids(modelname, idlist): klass = get_model(modelname) return webhelpers.get_ids(klass, idlist) def query(modelname, filt=None, columns=None, order_by=None, start=None, end=None):
def exception_handler(self, ex, val, tb): logging.error("Telnet poller error: {} ({})".format(ex, val))
def exception_handler(self, ex, val, tb): logging.error("Error event: {} ({})\n".format(ex, val))
def error_handler(self): logging.error("Async handler error occured: {}.\n".format( self.basename))
logfile=None, env=None, callback=None, merge=1, pwent=None, async=False, devnull=False, _pgid=0): Process.__init__(self, cmdline, logfile, callback, async) if env: self.environment = env cmd = split_command_line(self.cmdline) try: pid, self._fd = os.forkpty() except OSError as err: logging.error("ProcessPty error: {}".format(err)) raise else: if pid == 0: # child sys.excepthook = sys.__excepthook__ if devnull: # Redirect standard file descriptors. sys.stdout.flush() sys.stderr.flush() os.close(sys.__stdin__.fileno()) os.close(sys.__stdout__.fileno()) os.close(sys.__stderr__.fileno()) # stdin always from /dev/null sys.stdin = open("/dev/null", 'r') os.dup2(sys.stdin.fileno(), 0) # log file is stdout and stderr, otherwise /dev/null
def send(self, stream, retry=16, timeout=60, quiet=0): ''' Send a stream via the XMODEM protocol. >>> stream = file('/etc/issue', 'rb') >>> print modem.send(stream) True Returns ``True`` upon succesful transmission or ``False`` in case of failure. ''' # initialize protocol error_count = 0 crc_mode = 0 cancel = 0 while True: char = self.getc(1) if char: if char == NAK: crc_mode = 0 break elif char == CRC: crc_mode = 1 break elif char == CAN: if not quiet: print >> sys.stderr, 'received CAN' if cancel: return False else: cancel = 1 else: logging.error('send ERROR expected NAK/CRC, got %s' % (ord(char), )) error_count += 1 if error_count >= retry: self.abort(timeout=timeout) return False # send data error_count = 0 packet_size = 128 sequence = 1 while True: data = stream.read(packet_size) if not data: # end of stream break data = data.ljust(packet_size, '\xff') if crc_mode: crc = self.calc_crc(data) else: crc = self.calc_checksum(data) # emit packet while True: self.putc(SOH) self.putc(chr(sequence)) self.putc(chr(0xff - sequence)) self.putc(data) if crc_mode: self.putc(chr(crc >> 8)) self.putc(chr(crc & 0xff)) else: self.putc(chr(crc)) char = self.getc(1, timeout) if char == ACK: break if char == NAK: error_count += 1 if error_count >= retry: # excessive amounts of retransmissions requested, # abort transfer self.abort(timeout=timeout) logging.warning('excessive NAKs, transfer aborted') return False # return to loop and resend continue # protocol error self.abort(timeout=timeout) logging.error('xmodem protocol error') return False # keep track of sequence sequence = (sequence + 1) % 0x100 # end of transmission self.putc(EOT) return True
def send(self, stream, retry=16, timeout=60, quiet=0): ''' Send a stream via the XMODEM protocol. >>> stream = file('/etc/issue', 'rb') >>> print modem.send(stream) True Returns ``True`` upon succesful transmission or ``False`` in case of failure. ''' # initialize protocol error_count = 0 crc_mode = 0 cancel = 0 while True: char = self.getc(1) if char: if char == NAK: crc_mode = 0 break elif char == CRC: crc_mode = 1 break elif char == CAN: if not quiet: print >> sys.stderr, 'received CAN' if cancel: return False else: cancel = 1 else: logging.error('send ERROR expected NAK/CRC, got %s' % (ord(char),)) error_count += 1 if error_count >= retry: self.abort(timeout=timeout) return False # send data error_count = 0 packet_size = 128 sequence = 1 while True: data = stream.read(packet_size) if not data: # end of stream break data = data.ljust(packet_size, '\xff') if crc_mode: crc = self.calc_crc(data) else: crc = self.calc_checksum(data) # emit packet while True: self.putc(SOH) self.putc(chr(sequence)) self.putc(chr(0xff - sequence)) self.putc(data) if crc_mode: self.putc(chr(crc >> 8)) self.putc(chr(crc & 0xff)) else: self.putc(chr(crc)) char = self.getc(1, timeout) if char == ACK: break if char == NAK: error_count += 1 if error_count >= retry: # excessive amounts of retransmissions requested, # abort transfer self.abort(timeout=timeout) logging.warning('excessive NAKs, transfer aborted') return False # return to loop and resend continue # protocol error self.abort(timeout=timeout) logging.error('xmodem protocol error') return False # keep track of sequence sequence = (sequence + 1) % 0x100 # end of transmission self.putc(EOT) return True
def error_handler(self): logging.error( "Async handler error occured: {}.\n".format(self.basename))