Ejemplo n.º 1
0
 def init_ssh(self):
     """set up ssh tunnels, if needed."""
     if not self.sshserver and not self.sshkey:
         return
     
     if self.sshkey and not self.sshserver:
         self.sshserver = self.ip
         self.ip=LOCALHOST
     
     lports = select_random_ports(4)
     rports = self.shell_port, self.iopub_port, self.stdin_port, self.hb_port
     self.shell_port, self.iopub_port, self.stdin_port, self.hb_port = lports
     
     remote_ip = self.ip
     self.ip = LOCALHOST
     self.log.info("Forwarding connections to %s via %s"%(remote_ip, self.sshserver))
     
     if tunnel.try_passwordless_ssh(self.sshserver, self.sshkey):
         password=False
     else:
         password = getpass("SSH Password for %s: "%self.sshserver)
     
     for lp,rp in zip(lports, rports):
         tunnel.ssh_tunnel(lp, rp, self.sshserver, remote_ip, self.sshkey, password)
     
     self.log.critical("To connect another client to this tunnel, use:")
     self.log.critical(
         "--existing --shell={0} --iopub={1} --stdin={2} --hb={3}".format(
             self.shell_port, self.iopub_port, self.stdin_port,
             self.hb_port))
Ejemplo n.º 2
0
    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 url

        return connect, maybe_tunnel
Ejemplo n.º 3
0
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.
    """
    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)
Ejemplo n.º 4
0
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.
    """
    if isinstance(connection_info, basestring):
        # 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)
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
 def init_ssh(self):
     """set up ssh tunnels, if needed."""
     if not self.sshserver and not self.sshkey:
         return
     
     if self.sshkey and not self.sshserver:
         self.sshserver = self.ip
         self.ip=LOCALHOST
     
     lports = select_random_ports(4)
     rports = self.shell_port, self.iopub_port, self.stdin_port, self.hb_port
     self.shell_port, self.iopub_port, self.stdin_port, self.hb_port = lports
     
     remote_ip = self.ip
     self.ip = LOCALHOST
     self.log.info("Forwarding connections to %s via %s"%(remote_ip, self.sshserver))
     
     if tunnel.try_passwordless_ssh(self.sshserver, self.sshkey):
         password=False
     else:
         password = getpass("SSH Password for %s: "%self.sshserver)
     
     for lp,rp in zip(lports, rports):
         tunnel.ssh_tunnel(lp, rp, self.sshserver, remote_ip, self.sshkey, password)
Ejemplo n.º 7
0
 def __init__(self, url_or_file=None, profile='default', cluster_dir=None, ipython_dir=None,
         context=None, username=None, debug=False, exec_key=None,
         sshserver=None, sshkey=None, password=None, paramiko=None,
         timeout=10
         ):
     super(Client, self).__init__(debug=debug, profile=profile)
     if context is None:
         context = zmq.Context.instance()
     self._context = context
         
     
     self._setup_cluster_dir(profile, cluster_dir, ipython_dir)
     if self._cd is not None:
         if url_or_file is None:
             url_or_file = pjoin(self._cd.security_dir, 'ipcontroller-client.json')
     assert url_or_file is not None, "I can't find enough information to connect to a hub!"\
         " Please specify at least one of url_or_file or profile."
     
     try:
         util.validate_url(url_or_file)
     except AssertionError:
         if not os.path.exists(url_or_file):
             if self._cd:
                 url_or_file = os.path.join(self._cd.security_dir, url_or_file)
             assert os.path.exists(url_or_file), "Not a valid connection file or url: %r"%url_or_file
         with open(url_or_file) as f:
             cfg = json.loads(f.read())
     else:
         cfg = {'url':url_or_file}
     
     # sync defaults from args, json:
     if sshserver:
         cfg['ssh'] = sshserver
     if exec_key:
         cfg['exec_key'] = exec_key
     exec_key = cfg['exec_key']
     sshserver=cfg['ssh']
     url = cfg['url']
     location = cfg.setdefault('location', None)
     cfg['url'] = util.disambiguate_url(cfg['url'], location)
     url = cfg['url']
     
     self._config = cfg
     
     self._ssh = bool(sshserver or sshkey or password)
     if self._ssh and sshserver is None:
         # default to ssh via localhost
         sshserver = url.split('://')[1].split(':')[0]
     if self._ssh and password is None:
         if tunnel.try_passwordless_ssh(sshserver, sshkey, paramiko):
             password=False
         else:
             password = getpass("SSH Password for %s: "%sshserver)
     ssh_kwargs = dict(keyfile=sshkey, password=password, paramiko=paramiko)
     if exec_key is not None and os.path.isfile(exec_key):
         arg = 'keyfile'
     else:
         arg = 'key'
     key_arg = {arg:exec_key}
     if username is None:
         self.session = ss.StreamSession(**key_arg)
     else:
         self.session = ss.StreamSession(username, **key_arg)
     self._query_socket = self._context.socket(zmq.XREQ)
     self._query_socket.setsockopt(zmq.IDENTITY, self.session.session)
     if self._ssh:
         tunnel.tunnel_connection(self._query_socket, url, sshserver, **ssh_kwargs)
     else:
         self._query_socket.connect(url)
     
     self.session.debug = self.debug
     
     self._notification_handlers = {'registration_notification' : self._register_engine,
                                 'unregistration_notification' : self._unregister_engine,
                                 'shutdown_notification' : lambda msg: self.close(),
                                 }
     self._queue_handlers = {'execute_reply' : self._handle_execute_reply,
                             'apply_reply' : self._handle_apply_reply}
     self._connect(sshserver, ssh_kwargs, timeout)
Ejemplo n.º 8
0
    def __init__(self,
                 url_or_file=None,
                 profile='default',
                 cluster_dir=None,
                 ipython_dir=None,
                 context=None,
                 username=None,
                 debug=False,
                 exec_key=None,
                 sshserver=None,
                 sshkey=None,
                 password=None,
                 paramiko=None,
                 timeout=10):
        super(Client, self).__init__(debug=debug, profile=profile)
        if context is None:
            context = zmq.Context.instance()
        self._context = context

        self._setup_cluster_dir(profile, cluster_dir, ipython_dir)
        if self._cd is not None:
            if url_or_file is None:
                url_or_file = pjoin(self._cd.security_dir,
                                    'ipcontroller-client.json')
        assert url_or_file is not None, "I can't find enough information to connect to a hub!"\
            " Please specify at least one of url_or_file or profile."

        try:
            util.validate_url(url_or_file)
        except AssertionError:
            if not os.path.exists(url_or_file):
                if self._cd:
                    url_or_file = os.path.join(self._cd.security_dir,
                                               url_or_file)
                assert os.path.exists(
                    url_or_file
                ), "Not a valid connection file or url: %r" % url_or_file
            with open(url_or_file) as f:
                cfg = json.loads(f.read())
        else:
            cfg = {'url': url_or_file}

        # sync defaults from args, json:
        if sshserver:
            cfg['ssh'] = sshserver
        if exec_key:
            cfg['exec_key'] = exec_key
        exec_key = cfg['exec_key']
        sshserver = cfg['ssh']
        url = cfg['url']
        location = cfg.setdefault('location', None)
        cfg['url'] = util.disambiguate_url(cfg['url'], location)
        url = cfg['url']

        self._config = cfg

        self._ssh = bool(sshserver or sshkey or password)
        if self._ssh and sshserver is None:
            # default to ssh via localhost
            sshserver = url.split('://')[1].split(':')[0]
        if self._ssh and password is None:
            if tunnel.try_passwordless_ssh(sshserver, sshkey, paramiko):
                password = False
            else:
                password = getpass("SSH Password for %s: " % sshserver)
        ssh_kwargs = dict(keyfile=sshkey, password=password, paramiko=paramiko)
        if exec_key is not None and os.path.isfile(exec_key):
            arg = 'keyfile'
        else:
            arg = 'key'
        key_arg = {arg: exec_key}
        if username is None:
            self.session = ss.StreamSession(**key_arg)
        else:
            self.session = ss.StreamSession(username, **key_arg)
        self._query_socket = self._context.socket(zmq.XREQ)
        self._query_socket.setsockopt(zmq.IDENTITY, self.session.session)
        if self._ssh:
            tunnel.tunnel_connection(self._query_socket, url, sshserver,
                                     **ssh_kwargs)
        else:
            self._query_socket.connect(url)

        self.session.debug = self.debug

        self._notification_handlers = {
            'registration_notification': self._register_engine,
            'unregistration_notification': self._unregister_engine,
            'shutdown_notification': lambda msg: self.close(),
        }
        self._queue_handlers = {
            'execute_reply': self._handle_execute_reply,
            'apply_reply': self._handle_apply_reply
        }
        self._connect(sshserver, ssh_kwargs, timeout)