Esempio n. 1
0
def _connect_ssh(entry, username, idfile, tunnel=None):
    """
    SSH into to a host.

    :param entry: The host entry to pull the hostname from.
    :type entry: :py:class:`HostEntry`
    :param username: To use a specific username.
    :type username: ``str`` or ``NoneType``
    :param idfile: The SSH identity file to use, if supplying a username.
    :type idfile: ``str`` or ``NoneType``
    :param tunnel: Host to tunnel SSH command through.
    :type tunnel: ``str`` or ``NoneType``

    :return: An exit status code.
    :rtype: ``int``
    """
    if entry.hostname != "" and entry.hostname is not None:
        _host = entry.hostname
    elif entry.public_ip != "" and entry.public_ip is not None:
        _host = entry.public_ip
    elif entry.private_ip != "" and entry.private_ip is not None:
        if tunnel is None:
            raise ValueError("Entry does not have a hostname or public IP. "
                             "You can connect via private IP if you use a "
                             "tunnel.")
        _host = entry.private_ip
    else:
        raise ValueError("No hostname, public IP or private IP information "
                         "found on host entry. I don't know how to connect.")
    command = _build_ssh_command(_host, username, idfile, None, tunnel)
    print('Connecting to %s...' % cyan(entry.display()))
    print('SSH command: %s' % green(command))
    proc = subprocess.Popen(command, shell=True)
    return proc.wait()
Esempio n. 2
0
def _run_ssh(entries, username, idfile, no_prompt=False, command=None,
             show=None, only=None, sort_by=None, limit=None, tunnel=None):
    """
    Lets the user choose which instance to SSH into.

    :param entries: The list of host entries.
    :type entries: [:py:class:`HostEntry`]
    :param username: The SSH username to use. Defaults to current user.
    :type username: ``str`` or ``NoneType``
    :param idfile: The identity file to use. Optional.
    :type idfile: ``str`` or ``NoneType``
    :param no_prompt: Whether to disable confirmation for SSH command.
    :type no_prompt: ``bool``
    :param command: SSH command to run on matching instances.
    :type command: ``str`` or ``NoneType``
    :param show: Instance attributes to show in addition to defaults.
    :type show: ``NoneType`` or ``list`` of ``str``
    :param only: If not ``None``, will *only* show these attributes.
    :type only: ``NoneType`` or ``list`` of ``str``
    :param sort_by: What to sort columns by. By default, sort by 'name'.
    :type sort_by: ``str``
    :param limit: At most how many results to show.
    :type limit: ``int`` or ``NoneType``
    """
    _print_entries = True
    _print_help = False
    if len(entries) == 0:
        exit('No entries matched the filters.')
    if no_prompt is True and command is not None:
        return _run_ssh_command(entries, username, idfile, command, tunnel)
    elif len(entries) == 1:
        if command is None:
            return _connect_ssh(entries[0], username, idfile, tunnel)
        else:
            return _run_ssh_command(entries, username, idfile, command, tunnel)
    elif command is not None:
        print(HostEntry.render_entries(entries,
                                       additional_columns=show,
                                       only_show=only, numbers=True))
        if no_prompt is False:
            get_input("Press enter to run command {} on the {} "
                      "above machines (Ctrl-C to cancel)"
                      .format(cyan(command), len(entries)))
        return _run_ssh_command(entries, username, idfile, command, tunnel)
    else:
        while True:
            if sort_by is not None:
                entries = HostEntry.sort_by(entries, sort_by)
            if limit is not None:
                entries = entries[:limit]
            if _print_entries is True:
                print(HostEntry.render_entries(entries,
                                               additional_columns=show,
                                               only_show=only, numbers=True))
                print('%s matching entries.' % len(entries))
                _print_entries = False
            if _print_help is True:
                cmd_str = green(command) if command is not None else 'none set'
                msg = COMMANDS_STRING.format(username=username or 'none set',
                                             idfile=idfile or 'none set',
                                             cur_cmd=cmd_str)
                print(msg)
                _print_help = False
            elif command is not None:
                print('Set to run ssh command: %s' % cyan(command))
            msg = 'Enter command (%s for help, %s to quit): ' % (cyan('h'),
                                                                 cyan('q'))
            choice = get_input(msg)
            if isinstance(choice, int):
                if 0 <= choice <= len(entries):
                    break
                else:
                    msg = 'Invalid number: must be between 0 and %s'
                    print(msg % (len(entries) - 1))
            elif choice == 'x':
                if command is None:
                    print('No command has been set. Set command with `c`')
                else:
                    return _run_ssh_command(entries, username, idfile,
                                            command, tunnel)
            elif choice == 'h':
                _print_help = True
            elif choice in ['q', 'quit', 'exit']:
                print('bye!')
                return
            else:
                # All of these commands take one or more arguments, so the
                # split length must be at least 2.
                commands = choice.split()
                if len(commands) < 2:
                    print(yellow('Unknown command "%s".' % choice))
                else:
                    cmd = commands[0]
                    if cmd in ['u', 'i', 'p']:
                        if cmd == 'u':
                            username = commands[1]
                        elif cmd == 'i':
                            _idfile = commands[1]
                            if not os.path.exists(_idfile):
                                print(yellow('No such file: %s' % _idfile))
                                continue
                            idfile = _idfile
                        elif cmd == 'p':
                            p = commands[1]
                            try:
                                profile = LsiProfile.load(p)
                                _username = profile.username
                                _idfile = expanduser(profile.identity_file)
                            except LsiProfile.LoadError:
                                print(yellow('No such profile: %s' % repr(p)))
                                continue
                            username = _username
                            idfile = _idfile
                        print('username: %s' % green(repr(username)))
                        print('identity file: %s' % green(repr(idfile)))
                    elif cmd == 'f':
                        entries = filter_entries(entries, commands[1:], [])
                        _print_entries = True
                    elif cmd == 'e':
                        entries = filter_entries(entries, [], commands[1:])
                        _print_entries = True
                    elif cmd == 'c':
                        command = ' '.join(commands[1:])
                    elif cmd == 'limit':
                        try:
                            limit = int(commands[1])
                            _print_entries = True
                        except ValueError:
                            print(yellow('Invalid limit (must be an integer)'))
                    elif cmd == 'sort':
                        sort_by = commands[1]
                        if sort_by not in show:
                            show.append(sort_by)
                        _print_entries = True
                    elif cmd == 'show':
                        if show is None:
                            show = commands[1:]
                        else:
                            show.extend(commands[1:])
                        _print_entries = True
                    else:
                        print(yellow('Unknown command "%s".' % cmd))
        return _connect_ssh(entries[choice], username, idfile, tunnel)
Esempio n. 3
0
'''{commands}:
  h: Show this message
  <number {n}>: Connect to the {n}th instance in the list
  p {profile}: Use profile {profile}
  u {username}: Change SSH username to {username} (currently {_username})
  i {idfile}: Change identity file to {idfile} (currently {_idfile})
  f <one or more {pattern}s>: Restrict results to those with {pattern}s
  e <one or more {pattern}s>: Restrict results to those without {pattern}s
  limit {n}: Limit output to first {n} lines
  sort <{attribute}>: Sort the list by {attribute}
  show <one or more {attribute}s>: Additionally show those {attribute}s
  c <{command}>: Set ssh command to run on matching hosts (currently {cur_cmd})
  x: Execute the above command on the above host(s)
  {q}: Quit
'''.format(commands=green('Commands'),
                          n=cyan('n'),
                          profile=cyan('profile'),
                          attribute=cyan('attribute'),
                          username=cyan('username'), _username='******',
                          idfile=cyan('idfile'), _idfile='{idfile}',
                          pattern=cyan('pattern'), command=cyan('command'),
                          cur_cmd='{cur_cmd}',
                          q=cyan('q'))



# Path to where the SSH known hosts will be stored.
_KNOWN_HOSTS_FILE = os.path.expanduser(os.environ.get('LSI_KNOWN_HOSTS',
                                                      '~/.lsi-known-hosts'))

# Maps bash commands to their fully qualified paths, e.g. 'ls' -> '/bin/ls'