Esempio n. 1
0
 def __init__(self, equipmentrow, rolename, debug=False):
     self.name = equipmentrow.name
     self._equipment = equipmentrow
     self._debug = debug
     self._device = None
     self._parent = None
     self._initializer = None
     self._console = None
     d = {}
     d["hostname"] = equipmentrow.name
     d["serno"] = equipmentrow.serno
     d["modelname"] = equipmentrow.model.name
     d["manufacturer"] = equipmentrow.model.manufacturer
     d["role"] = rolename
     if equipmentrow.attributes and isinstance(equipmentrow.attributes,
                                               dict):
         d.update(equipmentrow.attributes)
     if equipmentrow.account:  # Account info takes precedence
         if not equipmentrow.account.admin:
             logging.warning("Equipment account not marked as admin.")
         d["login"] = equipmentrow.account.login
         d["password"] = equipmentrow.account.password
     if equipmentrow.user:  # Alternate user account
         if equipmentrow.user.admin:
             logging.warning("Equipment user marked as admin.")
         d["user"] = equipmentrow.user.login
         d["userpassword"] = equipmentrow.user.password
     self._attributes = d
     self._equipmentmodel = EquipmentModelRuntime(equipmentrow.model)
     signals.device_change.connect(self._on_device_change)
Esempio n. 2
0
 def run_command(self, cmd):
     self.send_command(cmd)
     out = self._term.read_until_prompt()
     self._term.write(b"echo $?\r")
     es = self._term.read_until_prompt()
     try:
         es = int(es.strip())
     except ValueError:
         logging.warning("SerialConsole: couldn't get exit status.")
         es = -1
     return es, out
Esempio n. 3
0
 def release_for(self, device, **kwargs):
     hostname = device.get("hostname")
     console_config = device.get("console")
     if console_config:
         devicenode = console_config.get("device")
         if devicenode:
             self.close_channel(hostname, devicenode)
         else:
             logging.warning(
                 "SerialCaptureService no serial device for {}.".format(
                     hostname))
     else:
         logging.warning(
             "SerialCaptureService no console config for {}.".format(
                 hostname))
Esempio n. 4
0
    def _process_rawq(self):
        buf = [b'', b'']  # data buffer and SB buffer
        try:
            while self._rawq:
                c = self._rawq_getchar()
                if not self.iacseq:
                    if c == NULL:
                        continue
                    if c == b"\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 = b''
                    if c == IAC:
                        buf[self.sb] += c
                    else:
                        if c == SB:  # SB ... SE start.
                            self.sb = 1
                            self.sbdataq = b''
                        elif c == SE:
                            self.sb = 0
                            self.sbdataq += buf[1]
                            buf[1] = b''
                            self._suboption()
                        else:
                            logging.warning(
                                'Telnet: IAC {!r} not recognized'.format(c))
                elif len(self.iacseq) == 2:
                    cmd = byte(self.iacseq[1])
                    self.iacseq = b''
                    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 = b''  # Reset on EOF
            self.sb = 0
        self._q += buf[0]
        self.sbdataq += buf[1]
Esempio n. 5
0
 def provide_for(self, device, **kwargs):
     self._start_server()
     hostname = device.get("hostname")
     console_config = device.get("console")
     if console_config:
         devicenode = console_config.get("device")
         if devicenode:
             setup = console_config.get("setup", "115200 8N1")
             self.add_channel(hostname, devicenode, setup)
         else:
             logging.warning(
                 "SerialCaptureService no serial device for {}.".format(
                     hostname))
     else:
         logging.warning(
             "SerialCaptureService no console config for {}.".format(
                 hostname))
Esempio n. 6
0
def _console_login(term, account, password):
    prompt = "root# " if account == "root" else "{}$ ".format(account)
    term.prompt = prompt
    exp = expect.Expect(term, prompt=prompt)
    exp.send("\r")
    while True:
        mo, index = exp.expect([
            "\rlogin:"******"assword:", exp.prompt, AUTOMATION_PROMPT, "\r] ",
            exp.timeoutmatch(10.0)
        ],
                               timeout=30.0)
        if mo:
            if index == 0:
                exp.send(account + "\r")
            elif index == 1:
                exp.send(password + "\r")
            elif index == 2:
                exp.send_slow("stty sane -echo\r")
                exp.send_slow('export PS1="{}"\r'.format(AUTOMATION_PROMPT))
                term.read_until(AUTOMATION_PROMPT.encode("ascii"))  # eat echo
                exp.send("\r")
            elif index == 3:
                exp.prompt = AUTOMATION_PROMPT
                term.prompt = AUTOMATION_PROMPT
                term.write(b"\r")
                term.flush()
                term.read_until_prompt()
                break
            elif index == 4:
                term.prompt = "] "
                logging.warning("console_login: Found recovery mode prompt.")
                break
            elif index == 5:
                raise exceptions.ControllerError(
                    "Soft timeout hit while looking for prompts.")
        else:
            raise exceptions.ControllerError(
                "didn't match anything while logging in.")
Esempio n. 7
0
 def _suboption(self):
     subopt = self.sbdataq
     self.sbdataq = b''
     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:
             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)))