def _run_ssh(entries, username, idfile, no_prompt=False, command=None,<EOL>show=None, only=None, sort_by=None, limit=None, tunnel=None,<EOL>num=None, random=False):
_print_entries = num is None<EOL>_print_help = False<EOL>if len(entries) == <NUM_LIT:0>:<EOL><INDENT>exit('<STR_LIT>')<EOL><DEDENT>if no_prompt is True and command is not None:<EOL><INDENT>return _run_ssh_command(entries, username, idfile, command, tunnel)<EOL><DEDENT>elif len(entries) == <NUM_LIT:1> or random is True:<EOL><INDENT>entry = py_random.choice(entries)<EOL>if command is None:<EOL><INDENT>return _connect_ssh(entry, username, idfile, tunnel)<EOL><DEDENT>else:<EOL><INDENT>return _run_ssh_command([entry], username, idfile, command, tunnel)<EOL><DEDENT><DEDENT>elif command is not None:<EOL><INDENT>print(HostEntry.render_entries(entries,<EOL>additional_columns=show,<EOL>only_show=only, numbers=True))<EOL>if no_prompt is False:<EOL><INDENT>get_input("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>.format(cyan(command), len(entries)))<EOL><DEDENT>return _run_ssh_command(entries, username, idfile, command, tunnel)<EOL><DEDENT>else:<EOL><INDENT>while True:<EOL><INDENT>if sort_by is not None:<EOL><INDENT>entries = HostEntry.sort_by(entries, sort_by)<EOL><DEDENT>if limit is not None:<EOL><INDENT>entries = entries[:limit]<EOL><DEDENT>if _print_entries is True:<EOL><INDENT>print(HostEntry.render_entries(entries,<EOL>additional_columns=show,<EOL>only_show=only, numbers=True))<EOL>print('<STR_LIT>' % len(entries))<EOL>_print_entries = False<EOL><DEDENT>if _print_help is True:<EOL><INDENT>cmd_str = green(command) if command is not None else '<STR_LIT>'<EOL>msg = COMMANDS_STRING.format(username=username or '<STR_LIT>',<EOL>idfile=idfile or '<STR_LIT>',<EOL>cur_cmd=cmd_str)<EOL>print(msg)<EOL>_print_help = False<EOL><DEDENT>elif command is not None:<EOL><INDENT>print('<STR_LIT>' % cyan(command))<EOL><DEDENT>msg = '<STR_LIT>' % (cyan('<STR_LIT:h>'),<EOL>cyan('<STR_LIT:q>'))<EOL>if num is not None:<EOL><INDENT>choice = num<EOL><DEDENT>else:<EOL><INDENT>choice = get_input(msg)<EOL><DEDENT>if isinstance(choice, int):<EOL><INDENT>if <NUM_LIT:0> <= choice <= len(entries):<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>if num is not None:<EOL><INDENT>exit("<STR_LIT>"<EOL>.format(num, len(entries) - <NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>"<EOL>print(msg % (len(entries) - <NUM_LIT:1>))<EOL><DEDENT><DEDENT><DEDENT>elif choice == '<STR_LIT:x>':<EOL><INDENT>if command is None:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return _run_ssh_command(entries, username, idfile,<EOL>command, tunnel)<EOL><DEDENT><DEDENT>elif choice == '<STR_LIT:h>':<EOL><INDENT>_print_help = True<EOL><DEDENT>elif choice in ['<STR_LIT:q>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>print('<STR_LIT>')<EOL>return<EOL><DEDENT>else:<EOL><INDENT>commands = choice.split()<EOL>if len(commands) < <NUM_LIT:2>:<EOL><INDENT>print(yellow('<STR_LIT>' % choice))<EOL><DEDENT>else:<EOL><INDENT>cmd = commands[<NUM_LIT:0>]<EOL>if cmd in ['<STR_LIT:u>', '<STR_LIT:i>', '<STR_LIT:p>']:<EOL><INDENT>if cmd == '<STR_LIT:u>':<EOL><INDENT>username = commands[<NUM_LIT:1>]<EOL><DEDENT>elif cmd == '<STR_LIT:i>':<EOL><INDENT>_idfile = commands[<NUM_LIT:1>]<EOL>if not os.path.exists(_idfile):<EOL><INDENT>print(yellow('<STR_LIT>' % _idfile))<EOL>continue<EOL><DEDENT>idfile = _idfile<EOL><DEDENT>elif cmd == '<STR_LIT:p>':<EOL><INDENT>p = commands[<NUM_LIT:1>]<EOL>try:<EOL><INDENT>profile = LsiProfile.load(p)<EOL>_username = profile.username<EOL>_idfile = expanduser(profile.identity_file)<EOL><DEDENT>except LsiProfile.LoadError:<EOL><INDENT>print(yellow('<STR_LIT>' % repr(p)))<EOL>continue<EOL><DEDENT>username = _username<EOL>idfile = _idfile<EOL><DEDENT>print('<STR_LIT>' % green(repr(username)))<EOL>print('<STR_LIT>' % green(repr(idfile)))<EOL><DEDENT>elif cmd == '<STR_LIT:f>':<EOL><INDENT>entries = filter_entries(entries, commands[<NUM_LIT:1>:], [])<EOL>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT:e>':<EOL><INDENT>entries = filter_entries(entries, [], commands[<NUM_LIT:1>:])<EOL>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT:c>':<EOL><INDENT>command = '<STR_LIT:U+0020>'.join(commands[<NUM_LIT:1>:])<EOL><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>limit = int(commands[<NUM_LIT:1>])<EOL>_print_entries = True<EOL><DEDENT>except ValueError:<EOL><INDENT>print(yellow('<STR_LIT>'))<EOL><DEDENT><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>sort_by = commands[<NUM_LIT:1>]<EOL>if sort_by not in show:<EOL><INDENT>show.append(sort_by)<EOL><DEDENT>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>if show is None:<EOL><INDENT>show = commands[<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>show.extend(commands[<NUM_LIT:1>:])<EOL><DEDENT>_print_entries = True<EOL><DEDENT>else:<EOL><INDENT>print(yellow('<STR_LIT>' % cmd))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return _connect_ssh(entries[choice], username, idfile, tunnel)<EOL><DEDENT>
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``
:param num: If not None, choose the given entry from within filters.
:type num: ``int`` or ``NoneType``
:param random: If true, choose a random entry from within filters.
:type random: ``bool``