def tunnel_connection(self): """ Set up tunnels to the node using the connection information. """ # Auto accept ssh keys so tunnels work on previously unknown hosts. # This might need to change, but the other option is to get user or # admin to turn StrictHostKeyChecking off in .ssh/ssh_config for this # to work seamlessly. (tunnels will have already done this) pre = self.tunnel_hosts_cmd or '' if ':' in self.host: host = self.host.replace(':', ' -p ') else: host = self.host pexpect_spawn('{pre} ssh -o StrictHostKeyChecking=no ' '{host}'.format(pre=pre, host=host).strip(), logfile=self.log).sendline('exit') # connection info should have the ports being used tunnel_command = self.tunnel_cmd.format(**self.connection_info) tunnel = pexpect_spawn(tunnel_command, logfile=self.log) check_password(tunnel) self.log.info("Setting up tunnels on ports: {0}.".format(", ".join([ "{0}".format(self.connection_info[port_name]) for port_name in PORT_NAMES ]))) self.log.debug("Tunnel command: {0}.".format(tunnel_command)) # Store the tunnel self.tunnels['tunnel'] = tunnel
def _spawn(self, command, timeout=600): """ Helper to start a pexpect.spawn as self.connection. If the session has already been started, just pass the command to sendline. Return the current spawn instance. The logfile is implicitly set to self.log. Parameters ---------- command : str Command to spawn or run in the current session. timeout : int Timeout for command to complete, passed to pexpect. Returns ------- connection : pexpect_spawn The connection object. This is also attached to the class. """ if self.connection is None: self.connection = pexpect_spawn(command, timeout=timeout, logfile=self.log) else: self.connection.sendline(command) return self.connection