<commit_before>import os, sys, argparse, subprocess

from . import register_parser
from .util.aws import resolve_instance_id
from .util.crypto import add_ssh_host_key_to_known_hosts

def ssh(args):
    from .util.aws.resources import ec2
    prefix, at, name = args.name.rpartition("@")
    instance = ec2.Instance(resolve_instance_id(name))
    tags = {tag["Key"]: tag["Value"] for tag in instance.tags or []}
    ssh_host_key = tags.get("SSHHostPublicKeyPart1", "") + tags.get("SSHHostPublicKeyPart2", "")
    if ssh_host_key:
        # FIXME: this results in duplicates.
        # Use paramiko to detect if the key is already listed and not insert it then (or only insert if different)
        add_ssh_host_key_to_known_hosts(instance.public_dns_name + " " + ssh_host_key + "\n")
    ssh_args = ["ssh", prefix + at + instance.public_dns_name] + args.ssh_args
    os.execvp("ssh", ssh_args)

parser = register_parser(ssh, help="Connect to an instance")
parser.add_argument("name")
parser.add_argument("ssh_args", nargs=argparse.REMAINDER)<commit_msg>Expand aegea ssh help<commit_after>"""
Connect to an EC2 instance via SSH, by name or instance ID.

Security groups, network ACLs, interfaces, VPC routing tables, VPC
Internet Gateways, and internal firewalls for the instance must be
configured to allow SSH connections.

To facilitate SSH connections, ``aegea ssh`` resolves instance names
to public DNS names assigned by AWS, and securely retrieves SSH host
public keys from instance metadata before connecting. This avoids both
the prompt to save the instance public key and the resulting transient
MITM vulnerability.
"""

import os, sys, argparse, subprocess

from . import register_parser
from .util.aws import resolve_instance_id
from .util.crypto import add_ssh_host_key_to_known_hosts
from .util.printing import BOLD

def ssh(args):
    from .util.aws.resources import ec2
    prefix, at, name = args.name.rpartition("@")
    instance = ec2.Instance(resolve_instance_id(name))
    tags = {tag["Key"]: tag["Value"] for tag in instance.tags or []}
    ssh_host_key = tags.get("SSHHostPublicKeyPart1", "") + tags.get("SSHHostPublicKeyPart2", "")
    if ssh_host_key:
        # FIXME: this results in duplicates.
        # Use paramiko to detect if the key is already listed and not insert it then (or only insert if different)
        add_ssh_host_key_to_known_hosts(instance.public_dns_name + " " + ssh_host_key + "\n")
    ssh_args = ["ssh", prefix + at + instance.public_dns_name] + args.ssh_args
    os.execvp("ssh", ssh_args)

parser = register_parser(ssh, help="Connect to an instance", description=__doc__)
parser.add_argument("name")
parser.add_argument("ssh_args", nargs=argparse.REMAINDER,
                    help="Arguments to pass to ssh; please see " + BOLD("man ssh") + " for details")
{'fraction_non_alphanumeric': 0.10007122507122507, 'fraction_numerical': 0.003205128205128205, 'mean_word_length': 4.486328125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3652506', 'n_tokens_mistral': 904, 'n_tokens_neox': 850, 'n_words': 272}