def connect(self, timeout=60, skip_login=False): """ Connect to and authenticate with host. *timeout* how long to wait for connect """ cmd = "telnet" args = list() args.append(self.hostname) if self.port: args.append(self.port) logger.debug(args) spawn.__init__(self, cmd, args, timeout) if not skip_login: self.expect([r"(?i)User.*", "(?i)User"]) self.send(self.username) self.send("\r") self.expect(self.username) self.expect([r"(?i)Password.*", "Password:"******"\r") self.expect(self.prompt) return True
def connect(self, timeout=10, expectation=True): """ Connect to and authenticate with host. """ cmd = "cec" args = list() args.append("-s%s" % self.shelf) args.append(self.iface) logger.debug("%s %s" % (cmd, str(args))) connectstr = re.compile('Escape is.*') try: if hasattr(self, 'closed'): self.close() # this may break if this is interrupted spawn.__init__(self, cmd, args=args, timeout=timeout) else: logger.debug("object had no attribute closed ... hmm") spawn.__init__(self, cmd, args=args, timeout=timeout) # tricky because cec doesn't return a prompt # it just connects try: self.expect(connectstr) except EOF: emesg = self.before.strip().splitlines()[0] if "can't netopen" in self.before: if expectation: raise ConnectionError("%s:\n try something like 'sudo chmod u+s /usr/sbin/cec'" % emesg) return False elif "none found" in self.before: if expectation: raise ConnectionError("%s\n could not reach shelf address %s" % (emesg, self.shelf)) return False self.sendline() self.sendline() # must now account for possible login prompt i = self.expect(["Password", self.prompt], timeout=3) if i == 0: if self.password is None: self.password = "******" self.sendline(self.password) self.expect(self.prompt) elif i == 1: pass except KeyError as e: logger.critical("CEC couldn't complete connection") if e.message: logger.error(e.message) if e.args: logger.debug(e.args) if expectation: raise ConnectionError(str(e)) return False except ExceptionPexpect as e: if "The command was not found or was not executable" in e: raise ConnectionError("%s:\n is cec installed at /usr/sbin/cec?" % e) else: raise e return True
def connect(self, timeout=10, args=None, nolog=False): """Connect to and authenticate with host.""" cmd = "ssh" if args is None: args = [ "-q", "-o PubkeyAuthentication no", "-o UserKnownHostsFile=/dev/null", "-o UserKnownHostsFile2=/dev/null", "-o StrictHostKeyChecking=no" ] args.append("-l" + self.user) args.append(self.host) if not nolog: logger.debug("%s %s" % (cmd, str(args))) try: spawn.__init__(self, cmd, args, timeout) prompt = self.expect(["(?i)password: "******"(?i)password"]) if prompt in [0, 1]: self.sendline(self.password) else: self.close() return False try: if self.prompt == ':\>': self.expect_exact(self.prompt) else: self.expect(self.prompt) except TIMEOUT: raise ConnectionError( "Connected but didn't find prompt '%s'\n instead self.before was:\n%s" % (self.prompt, self.before)) self.connected = True except KeyError as e: if not nolog: logger.critical("Couldn't complete connection") if e.message: logger.error(e.message) return False except (EOF, TIMEOUT) as e: if not nolog: logger.critical("Couldn't complete connection to %s@%s" % (self.user, self.host)) if e.message: logger.error(e.message) return ReturnCode(False, message=e.message) return ReturnCode(True, message=self.before)
def connect(self, timeout=10, args=None, nolog=False): """Connect to and authenticate with host.""" cmd = "ssh" if args is None: args = ["-q", "-o PubkeyAuthentication no", "-o UserKnownHostsFile=/dev/null", "-o UserKnownHostsFile2=/dev/null", "-o StrictHostKeyChecking=no"] args.append("-l" + self.user) args.append(self.host) if not nolog: logger.debug("%s %s" % (cmd, str(args))) try: spawn.__init__(self, cmd, args, timeout) prompt = self.expect(["(?i)password: "******"(?i)password"]) if prompt in [0, 1]: self.sendline(self.password) else: self.close() return False try: if self.prompt == ':\>': self.expect_exact(self.prompt) else: self.expect(self.prompt) except TIMEOUT: raise ConnectionError( "Connected but didn't find prompt '%s'\n instead self.before was:\n%s" % (self.prompt, self.before)) self.connected = True except KeyError as e: if not nolog: logger.critical("Couldn't complete connection") if e.message: logger.error(e.message) return False except (EOF, TIMEOUT) as e: if not nolog: logger.critical("Couldn't complete connection to %s@%s" % (self.user, self.host)) if e.message: logger.error(e.message) return ReturnCode(False, message=e.message) return ReturnCode(True, message=self.before)
def connect(self, timeout=60): """ Connect to and authenticate with the pdu. This telnet interface for the Eaton pdu is pretty specialized. Also, it depends sometimes on where a previous user left off. When trying to connect, you can be at the login screen or already logged in and on either the main menu or a sub-menu. This method doesn't return until on the main menu ( which is what the methods in the Eaton class expect ). """ cmd = "telnet" args = list() args.append(self.hostname) if self.port: args.append(self.port) logger.debug(args) main_menu = False spawn.__init__(self, cmd, args, timeout) while True: # continues until on the main menu m = self.expect([ "Main Menu", "UserName", "Password", "\[\+none", "<ESC> = Back" ]) if m == 0: break elif m == 1: self.sendline(self.username) continue elif m == 2: self.sendline(self.password) continue elif m == 3: # for [+none, bootes], need to hit enter self.send("\r") continue elif m == 4: # hit escape until out of each sub-menu self.send('\x1b') continue return True
def connect(self, timeout=60): """ Connect to and authenticate with the pdu. This telnet interface for the Eaton pdu is pretty specialized. Also, it depends sometimes on where a previous user left off. When trying to connect, you can be at the login screen or already logged in and on either the main menu or a sub-menu. This method doesn't return until on the main menu ( which is what the methods in the Eaton class expect ). """ cmd = "telnet" args = list() args.append(self.hostname) if self.port: args.append(self.port) logger.debug(args) main_menu = False spawn.__init__(self, cmd, args, timeout) while True: # continues until on the main menu m = self.expect(["Main Menu", "UserName", "Password", "\[\+none", "<ESC> = Back"]) if m == 0: break elif m == 1: self.sendline(self.username) continue elif m == 2: self.sendline(self.password) continue elif m == 3: # for [+none, bootes], need to hit enter self.send("\r") continue elif m == 4: # hit escape until out of each sub-menu self.send('\x1b') continue return True
def connect(self, timeout=10, expectation=True): """ Connect to and authenticate with host. """ cmd = "cec" args = list() args.append("-s%s" % self.shelf) args.append(self.iface) logger.debug("%s %s" % (cmd, str(args))) connectstr = re.compile('Escape is.*') try: if hasattr(self, 'closed'): self.close() # this may break if this is interrupted spawn.__init__(self, cmd, args=args, timeout=timeout) else: logger.debug("object had no attribute closed ... hmm") spawn.__init__(self, cmd, args=args, timeout=timeout) # tricky because cec doesn't return a prompt # it just connects try: self.expect(connectstr) except EOF: emesg = self.before.strip().splitlines()[0] if "can't netopen" in self.before: if expectation: raise ConnectionError( "%s:\n try something like 'sudo chmod u+s /usr/sbin/cec'" % emesg) return False elif "none found" in self.before: if expectation: raise ConnectionError( "%s\n could not reach shelf address %s" % (emesg, self.shelf)) return False self.sendline() self.sendline() # must now account for possible login prompt i = self.expect(["Password", self.prompt], timeout=3) if i == 0: if self.password is None: self.password = "******" self.sendline(self.password) self.expect(self.prompt) elif i == 1: pass except KeyError as e: logger.critical("CEC couldn't complete connection") if e.message: logger.error(e.message) if e.args: logger.debug(e.args) if expectation: raise ConnectionError(str(e)) return False except ExceptionPexpect as e: if "The command was not found or was not executable" in e: raise ConnectionError( "%s:\n is cec installed at /usr/sbin/cec?" % e) else: raise e return True