Example #1
0
    def _openCliSession(self):

        logging.info("Opening CLI Session to '%s'" % self.hostname)

        # Open Log File
        fileId = open(self.log, 'a')

        # Spawn CLI Session
        cmd = 'ssh %s' % self.hostname if not self.username else 'ssh %s@%s' % (self.username, self.hostname)
        self.session = pexpect.spawn(cmd, env={"TERM":"dumb"}, logfile=fileId)

        # SSH Prompts
        while True:
            match = self.session.expect([self.prompt, 'yes/no', '[Pp]assword:', pexpect.EOF, pexpect.TIMEOUT], timeout=self.timeout)
            if match == 0:
                logging.info("Received Prompt on '%s'" % self.hostname)
                return
            elif match == 1:
                logging.info("Received SSH RSA Authentication Prompt on '%s'" % self.hostname)
                self.session.sendline('yes')
            elif match == 2:
                logging.info("Received Password Prompt on '%s'" % self.hostname)
                self.session.sendline(self.password)
            elif match == 3:
                uhoh("Received EOF Termination from '%s'" % self.hostname)
            elif match == 4:
                uhoh("Received TIMEOUT on '%s'" % self.hostname)
Example #2
0
    def _sendCmd(self, cmd, timeout=None, prompt=None, raw=False):

        # Initialize Variables
        cmd = cmd + '\n'
        retVal = []

        if not timeout: timeout = self.timeout
        if not prompt:  prompt = self.prompt

        # Send Command
        self.session.send(cmd)

        # Expect Output
        match = self.session.expect([prompt, pexpect.EOF, pexpect.TIMEOUT], timeout=timeout)

        # Receive Prompt
        if match == 0:
            logging.info("Received Prompt on '%s'" % self.hostname)
            output = self.session.before.decode()
        elif match == 1:
            uhoh("Received EOF Termination from '%s'" % self.hostname)
        elif match == 2:
            uhoh("Received Timeout on '%s'" % self.hostname)

        # Error Check
        for error in self.errors:
            if error in output:
                uhoh("Error '%s' detected on '%s'" % (error, self.hostname))

        # Output List of lists (skip cmd echo)
        if raw:
            return output
        else:
            output = output.split('\r\n')[1:]
            for count,line in enumerate(output):
                logging.debug('Row %2s = %s' % (count, line))
                retVal.append(line.split())

        # Return
        return retVal