def _tunnel_to_kernel(self, connection_info, server, port=ssh_port, key=None): """Tunnel connections to a kernel over SSH This will open five SSH tunnels from localhost on this machine to the ports associated with the kernel. See jupyter_client/connect.py for original implementation. """ cf = connection_info lports = self.select_ports(5) rports = cf['shell_port'], cf['iopub_port'], cf['stdin_port'], cf[ 'hb_port'], cf['control_port'] channels = KernelChannel.SHELL, KernelChannel.IOPUB, KernelChannel.STDIN, \ KernelChannel.HEARTBEAT, KernelChannel.CONTROL remote_ip = cf['ip'] if not tunnel.try_passwordless_ssh(server + ":" + str(port), key): self.log_and_raise( http_status_code=403, reason="Must use password-less scheme by setting up the " "SSH public key on the cluster nodes") for lp, rp, kc in zip(lports, rports, channels): self._create_ssh_tunnel(kc, lp, rp, remote_ip, server, port, key) return tuple(lports)
def _tunnel_to_kernel(self, connection_info, server, port=ssh_port, key=None): """Tunnel connections to a kernel over SSH This will open five SSH tunnels from localhost on this machine to the ports associated with the kernel. See jupyter_client/connect.py for original implementation. """ cf = connection_info lports = self.select_ports(5) rports = cf['shell_port'], cf['iopub_port'], cf['stdin_port'], cf['hb_port'], cf['control_port'] channels = KernelChannel.SHELL, KernelChannel.IOPUB, KernelChannel.STDIN, \ KernelChannel.HEARTBEAT, KernelChannel.CONTROL remote_ip = cf['ip'] if not tunnel.try_passwordless_ssh(server + ":" + str(port), key): self.log_and_raise(http_status_code=403, reason="Must use password-less scheme by setting up the " "SSH public key on the cluster nodes") for lp, rp, kc in zip(lports, rports, channels): self._create_ssh_tunnel(kc, lp, rp, remote_ip, server, port, key) return tuple(lports)
def _tunnel_to_kernel(self, connection_info, sshserver, sshkey=None): """Tunnel connections to a kernel over SSH This will open five SSH tunnels from localhost on this machine to the ports associated with the kernel. See jupyter_client/connect.py for original implementation. """ cf = connection_info lports = tunnel.select_random_ports(5) rports = cf['shell_port'], cf['iopub_port'], cf['stdin_port'], cf[ 'hb_port'], cf['control_port'] port_names = "SHELL", "IOPUB", "STDIN", "HB", "CONTROL" remote_ip = cf['ip'] if not tunnel.try_passwordless_ssh(sshserver, sshkey): raise RuntimeError( "Must use passwordless scheme by setting up the SSH public key on the cluster nodes" ) for lp, rp, pn in zip(lports, rports, port_names): self._create_ssh_tunnel(pn, lp, rp, remote_ip, sshserver, sshkey) return tuple(lports)
def tunnel_to_kernel(connection_info, sshserver, sshkey=None): """tunnel connections to a kernel via ssh This will open four SSH tunnels from localhost on this machine to the ports associated with the kernel. They can be either direct localhost-localhost tunnels, or if an intermediate server is necessary, the kernel must be listening on a public IP. Parameters ---------- connection_info : dict or str (path) Either a connection dict, or the path to a JSON connection file sshserver : str The ssh sever to use to tunnel to the kernel. Can be a full `user@server:port` string. ssh config aliases are respected. sshkey : str [optional] Path to file containing ssh key to use for authentication. Only necessary if your ssh config does not already associate a keyfile with the host. Returns ------- (shell, iopub, stdin, hb) : ints The four ports on localhost that have been forwarded to the kernel. """ from zmq.ssh import tunnel if isinstance(connection_info, string_types): # it's a path, unpack it with open(connection_info) as f: connection_info = json.loads(f.read()) cf = connection_info lports = tunnel.select_random_ports(4) rports = cf['shell_port'], cf['iopub_port'], cf['stdin_port'], cf[ 'hb_port'] remote_ip = cf['ip'] if tunnel.try_passwordless_ssh(sshserver, sshkey): password = False else: password = getpass("SSH Password for %s: " % cast_bytes_py2(sshserver)) for lp, rp in zip(lports, rports): tunnel.ssh_tunnel(lp, rp, sshserver, remote_ip, sshkey, password) return tuple(lports)
def tunnel_to_kernel(connection_info, sshserver, sshkey=None): """tunnel connections to a kernel via ssh This will open four SSH tunnels from localhost on this machine to the ports associated with the kernel. They can be either direct localhost-localhost tunnels, or if an intermediate server is necessary, the kernel must be listening on a public IP. Parameters ---------- connection_info : dict or str (path) Either a connection dict, or the path to a JSON connection file sshserver : str The ssh sever to use to tunnel to the kernel. Can be a full `user@server:port` string. ssh config aliases are respected. sshkey : str [optional] Path to file containing ssh key to use for authentication. Only necessary if your ssh config does not already associate a keyfile with the host. Returns ------- (shell, iopub, stdin, hb) : ints The four ports on localhost that have been forwarded to the kernel. """ from zmq.ssh import tunnel if isinstance(connection_info, string_types): # it's a path, unpack it with open(connection_info) as f: connection_info = json.loads(f.read()) cf = connection_info lports = tunnel.select_random_ports(4) rports = cf['shell_port'], cf['iopub_port'], cf[ 'stdin_port'], cf['hb_port'] remote_ip = cf['ip'] if tunnel.try_passwordless_ssh(sshserver, sshkey): password = False else: password = getpass("SSH Password for %s: " % cast_bytes_py2(sshserver)) for lp, rp in zip(lports, rports): tunnel.ssh_tunnel(lp, rp, sshserver, remote_ip, sshkey, password) return tuple(lports)
def init_connector(self): """construct connection function, which handles tunnels.""" self.using_ssh = bool(self.sshkey or self.sshserver) if self.sshkey and not self.sshserver: # We are using ssh directly to the controller, tunneling localhost to localhost self.sshserver = self.url.split('://')[1].split(':')[0] if self.using_ssh: if tunnel.try_passwordless_ssh(self.sshserver, self.sshkey, self.paramiko): password = False else: password = getpass("SSH Password for %s: " % self.sshserver) else: password = False def connect(s, url): url = disambiguate_url(url, self.location) if self.using_ssh: self.log.debug("Tunneling connection to %s via %s", url, self.sshserver) return tunnel.tunnel_connection( s, url, self.sshserver, keyfile=self.sshkey, paramiko=self.paramiko, password=password, ) else: return s.connect(url) def maybe_tunnel(url): """like connect, but don't complete the connection (for use by heartbeat)""" url = disambiguate_url(url, self.location) if self.using_ssh: self.log.debug("Tunneling connection to %s via %s", url, self.sshserver) url, tunnelobj = tunnel.open_tunnel( url, self.sshserver, keyfile=self.sshkey, paramiko=self.paramiko, password=password, ) return str(url) return connect, maybe_tunnel