Skip to content

Instantly share code, notes, and snippets.

@atr0s
Last active April 30, 2024 22:24
Show Gist options
  • Select an option

  • Save atr0s/70d2c7327a3d1520d58998adc620fa5b to your computer and use it in GitHub Desktop.

Select an option

Save atr0s/70d2c7327a3d1520d58998adc620fa5b to your computer and use it in GitHub Desktop.

Revisions

  1. atr0s revised this gist Aug 13, 2019. No changes.
  2. atr0s revised this gist Aug 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .bash_profile
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function aws-ssm-menu {

    function aws-scp {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-scp <profile name> <ssh parameters>"
    echo "Usage: aws-scp <profile name> <scp parameters>"
    else
    scp -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\"" ${@:2}
    fi
  3. atr0s revised this gist Aug 13, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions .bash_profile
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,14 @@ function aws-ssm-menu {
    fi
    }

    function aws-scp {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-scp <profile name> <ssh parameters>"
    else
    scp -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\"" ${@:2}
    fi
    }

    function aws-ssh {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-ssh <profile name> <ssh parameters>"
  4. atr0s revised this gist Aug 12, 2019. 1 changed file with 53 additions and 2 deletions.
    55 changes: 53 additions & 2 deletions .bash_profile
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,61 @@
    # Snippet of code to be put on ~/.bash_profile to help SSH into instances via Session Manager while specifying AWS profiles
    #
    # It can be used by saving ~/.ssm_bash_profile and adding the following line to ~/.bash_profile
    # source ~/.ssm_bash_profile

    function aws-ssm-instance-list {
    if [ "$#" -lt 1 ]; then
    echo "Usage: aws-ssm-instance-list <profile name>"
    else
    output=$(aws ssm describe-instance-information --profile $1 --query "InstanceInformationList[*].{Name:ComputerName,Id:InstanceId,IPAddress:IPAddress}" --output text)
    echo "$output"
    fi
    }

    function aws-ssm-menu {
    printf "List of instances for $1:\n\n"
    instance_list_output=$(aws-ssm-instance-list $1)
    IFS=$'\n'
    instance_list=($instance_list_output)
    unset IFS
    for i in "${!instance_list[@]}"; do
    printf "%s) %s\n" "$i" "${instance_list[$i]}"
    done
    printf "\nSelect an instance from the list above: "
    IFS= read -r opt
    if [[ $opt =~ ^[0-9]+$ ]] && (( (opt >= 0) && (opt < "${#instance_list[@]}") )); then
    return $opt
    else
    printf 'invalid option\n'
    return -1
    fi
    }

    function aws-ssh {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-ssh <profile name> <ssh parameters>"
    else
    ssh ${@:2} -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\""
    fi
    }
    }

    function aws-ssm-session {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-ssm-session <profile name> <instance_id>"
    else
    aws ssm start-session --target $2 --profile $1
    fi
    }

    function aws-session-interactive {
    if [ "$#" -lt 1 ]; then
    echo "Usage: aws-ssh-interactive <profile name>"
    else
    aws-ssm-menu $1
    choice=$?
    if [ $choice -ne 255 ] ; then
    instance_id=$(echo "${instance_list[$choice]}"| awk {'print $2'})
    aws-ssm-session $1 $instance_id
    fi
    fi
    }

  5. atr0s revised this gist Aug 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .bash_profile
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@ function aws-ssh {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-ssh <profile name> <ssh parameters>"
    else
    ssh ${@:2:99} -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\""
    ssh ${@:2} -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\""
    fi
    }
  6. atr0s revised this gist Aug 7, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion .bash_profile
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,5 @@ function aws-ssh {
    echo "Usage: aws-ssh <profile name> <ssh parameters>"
    else
    ssh ${@:2:99} -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\""

    fi
    }
  7. atr0s created this gist Aug 7, 2019.
    11 changes: 11 additions & 0 deletions .bash_profile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # Snippet of code to be put on ~/.bash_profile to help SSH into instances via Session Manager while specifying AWS profiles
    #

    function aws-ssh {
    if [ "$#" -lt 2 ]; then
    echo "Usage: aws-ssh <profile name> <ssh parameters>"
    else
    ssh ${@:2:99} -o ProxyCommand="bash -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile $1\""

    fi
    }