Skip to content

Instantly share code, notes, and snippets.

@naviocean
Forked from yuki777/sssh
Last active November 26, 2022 15:22
Show Gist options
  • Save naviocean/7b34b5ec3c6f65a10bb308570e109e4a to your computer and use it in GitHub Desktop.
Save naviocean/7b34b5ec3c6f65a10bb308570e109e4a to your computer and use it in GitHub Desktop.

Revisions

  1. naviocean revised this gist Nov 26, 2022. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -120,13 +120,15 @@ die() {

    validatePrereq() {
    command -v jq &>/dev/null || die "jq not installed on host. Please install jq. See https://stedolan.github.io/jq/download/"
    command -v session-manager-plugin &>/dev/null || die "session-manager-plugin not installed. See https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
    command -v session-manager-plugin &>/dev/null || die "session-manager-plugin not installed. See
    https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
    command -v aws &>/dev/null || die "AWS CLI not found, AWS CLI version 1.16.12 or later must be installed. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    # Checks if AWS CLI is outdated or not., v1 of AWS CLI pipes to std error, redirect
    AWS_CLI_VERSION=$(aws --version 2>&1 | awk '{ print $1 }' | cut -d/ -f2)
    echo_stderr "You have AWS CLI v$AWS_CLI_VERSION installed."
    # Do a best effort check for v1 (so that it's at least 1.10 and up.
    [[ $AWS_CLI_VERSION =~ ^1.1[0-9] || $AWS_CLI_VERSION =~ ^2 ]] &>/dev/null || die "AWS CLI version 1.16.12 or later must be installed to support ecs-exec, Run 'aws --version' to see what you have. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    [[ $AWS_CLI_VERSION =~ ^1.1[0-9] || $AWS_CLI_VERSION =~ ^2 ]] &>/dev/null || die "AWS CLI version 1.16.12 or later must be installed to support ecs-exec, Run 'aws --version' to see what you
    have. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    }

    function print_help() {
    @@ -188,6 +190,10 @@ main(){
    port="${1:?port must be specified in --port}"
    shift
    ;;
    --host()
    shift
    host="${1:?host must be specified in --host}"
    shift
    --local-port)
    shift
    localPort="${1:?local-port must be specified in --local-port}"
    @@ -237,7 +243,8 @@ main(){
    else
    taskId=$(echo $task | awk -F '/' '{print $3}')
    containerId=$(aws ecs describe-tasks $(params) --cluster $cluster --task $task | jq -r --arg container $container '.tasks[0].containers[] | select(.name == $container).runtimeId')
    cmd="aws ssm start-session $(params) --target ecs:${cluster}_${taskId}_${containerId} --document-name AWS-StartPortForwardingSession --parameters {\"portNumber\":[\"$port\"],\"localPortNumber\":[\"$localPort\"]}"
    cmd="aws ssm start-session --target ecs:${cluster}_${taskId}_${containerId} --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters
    {\"portNumber\":[\"$port\"],\"localPortNumber\":[\"$localPort\"],\"host\":[\"$host\"]} --debug"
    fi

    colorEcho $cmd
  2. @yuki777 yuki777 revised this gist Oct 26, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sssh
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@
    # Usage: See --help.
    #
    # Installation: Download the script and `chmod u+x the script`.
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/b5a649a40747cc80a078fd3a69cfbe90e8fe1756/sssh -o sssh && chmod u+x sssh
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/77ec8f3ca3a5bc8808cc8f28bc3fa345fe924599/sssh -o sssh && chmod u+x sssh
    # ./sssh
    #
    # Prerequisites (validated)
  3. @yuki777 yuki777 revised this gist Oct 26, 2022. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,21 @@
    #!/bin/bash

    ############################################################################
    # Copyright 2022 Yuki Adachi https://github.com/yuki777
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    # http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    ############################################################################

    # Bash script to run ecs-exec on Amazon ECS Fargate containers.
    #
    # Usage: See --help.
  4. @yuki777 yuki777 revised this gist Apr 21, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sssh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # Usage: See --help.
    #
    # Installation: Download the script and `chmod u+x the script`.
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/c2be2fac364c3f3d4be59774c29358ed1b370c02/sssh -o sssh && chmod u+x sssh
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/b5a649a40747cc80a078fd3a69cfbe90e8fe1756/sssh -o sssh && chmod u+x sssh
    # ./sssh
    #
    # Prerequisites (validated)
  5. @yuki777 yuki777 revised this gist Apr 21, 2022. 1 changed file with 147 additions and 129 deletions.
    276 changes: 147 additions & 129 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # Usage: See --help.
    #
    # Installation: Download the script and `chmod u+x the script`.
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/5bfa8242ba00ed336da76711d602e6df4f76a452/sssh -o sssh && chmod u+x sssh
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/c2be2fac364c3f3d4be59774c29358ed1b370c02/sssh -o sssh && chmod u+x sssh
    # ./sssh
    #
    # Prerequisites (validated)
    @@ -19,102 +19,102 @@
    set -eu

    selectProfile(){
    # profile parameter not supplied.
    if [ -z ${profile+x} ]; then
    # only works with AWS CLIv2.
    select selected in `aws configure list-profiles`
    do
    break
    done
    echo $selected
    else
    echo $profile
    fi
    # profile parameter not supplied.
    if [ -z ${profile+x} ]; then
    # only works with AWS CLIv2.
    select selected in `aws configure list-profiles`
    do
    break
    done
    echo $selected
    else
    echo $profile
    fi
    }

    params(){
    echo "$(profileParam) $(regionParam)"
    echo "$(profileParam) $(regionParam)"
    }

    profileParam() {
    [[ $profile ]] &>/dev/null && echo "--profile $profile"
    [[ $profile ]] &>/dev/null && echo "--profile $profile"
    }

    regionParam() {
    [[ $region ]] &>/dev/null && echo "--region $region"
    [[ $region ]] &>/dev/null && echo "--region $region"
    }

    selectCluster(){
    select selected in $(aws ecs list-clusters $(params)|jq -r ".clusterArns[]"|sort|cut -d "/" -f 2)
    do
    break
    done
    echo $selected
    select selected in $(aws ecs list-clusters $(params)|jq -r ".clusterArns[]"|sort|cut -d "/" -f 2)
    do
    break
    done
    echo $selected
    }

    selectService(){
    select selected in $(aws ecs list-services $(params) --cluster $cluster|jq -r ".serviceArns[]"|sort)
    do
    break
    done
    echo $selected
    select selected in $(aws ecs list-services $(params) --cluster $cluster|jq -r ".serviceArns[]"|sort)
    do
    break
    done
    echo $selected
    }

    selectTask(){
    select selected in $(aws ecs list-tasks $(params) --cluster $cluster --service-name $service --desired-status RUNNING |jq -r '.taskArns[]'|sort)
    do
    break
    done
    echo $selected
    select selected in $(aws ecs list-tasks $(params) --cluster $cluster --service-name $service --desired-status RUNNING |jq -r '.taskArns[]'|sort)
    do
    break
    done
    echo $selected
    }

    selectContainer(){
    select selected in $(aws ecs describe-tasks $(params) --cluster $cluster --tasks $task | jq -r ".tasks[].containers[].name"|sort)
    do
    break
    done
    echo $selected
    select selected in $(aws ecs describe-tasks $(params) --cluster $cluster --tasks $task | jq -r ".tasks[].containers[].name"|sort)
    do
    break
    done
    echo $selected
    }

    colorEcho(){
    red='\033[0;31m'
    green='\033[0;32m'
    yellow='\033[0;33m'
    reset='\033[0m'

    if echo $@ | egrep -q "prd|prod|production"; then
    color=$red
    elif echo $@ | egrep -q "stg|stage|staging|beta|devo"; then
    color=$yellow
    else
    color=$green
    fi

    echo -e "${color}$@${reset}"
    red='\033[0;31m'
    green='\033[0;32m'
    yellow='\033[0;33m'
    reset='\033[0m'

    if echo $@ | egrep -q "prd|prod|production"; then
    color=$red
    elif echo $@ | egrep -q "stg|stage|staging|beta|devo"; then
    color=$yellow
    else
    color=$green
    fi

    echo -e "${color}$@${reset}"
    }

    echo_stderr() {
    echo -e "$@" >&2
    echo -e "$@" >&2
    }

    die() {
    echo_stderr "$@"
    exit 1
    echo_stderr "$@"
    exit 1
    }

    validatePrereq() {
    command -v jq &>/dev/null || die "jq not installed on host. Please install jq. See https://stedolan.github.io/jq/download/"
    command -v session-manager-plugin &>/dev/null || die "session-manager-plugin not installed. See https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
    command -v aws &>/dev/null || die "AWS CLI not found, AWS CLI version 1.16.12 or later must be installed. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    # Checks if AWS CLI is outdated or not., v1 of AWS CLI pipes to std error, redirect
    AWS_CLI_VERSION=$(aws --version 2>&1 | awk '{ print $1 }' | cut -d/ -f2)
    echo_stderr "You have AWS CLI v$AWS_CLI_VERSION installed."
    # Do a best effort check for v1 (so that it's at least 1.10 and up.
    [[ $AWS_CLI_VERSION =~ ^1.1[0-9] || $AWS_CLI_VERSION =~ ^2 ]] &>/dev/null || die "AWS CLI version 1.16.12 or later must be installed to support ecs-exec, Run 'aws --version' to see what you have. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    command -v jq &>/dev/null || die "jq not installed on host. Please install jq. See https://stedolan.github.io/jq/download/"
    command -v session-manager-plugin &>/dev/null || die "session-manager-plugin not installed. See https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
    command -v aws &>/dev/null || die "AWS CLI not found, AWS CLI version 1.16.12 or later must be installed. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    # Checks if AWS CLI is outdated or not., v1 of AWS CLI pipes to std error, redirect
    AWS_CLI_VERSION=$(aws --version 2>&1 | awk '{ print $1 }' | cut -d/ -f2)
    echo_stderr "You have AWS CLI v$AWS_CLI_VERSION installed."
    # Do a best effort check for v1 (so that it's at least 1.10 and up.
    [[ $AWS_CLI_VERSION =~ ^1.1[0-9] || $AWS_CLI_VERSION =~ ^2 ]] &>/dev/null || die "AWS CLI version 1.16.12 or later must be installed to support ecs-exec, Run 'aws --version' to see what you have. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    }

    function print_help() {
    cat >&2 <<-END
    cat >&2 <<-END
    This script simplifies the process of getting the required information to drop into an
    interactive shell script on your container hosted on Fargate/ECS.
    @@ -125,9 +125,11 @@ Example:
    ./sssh --profile default
    Supported input parameters:
    -r | --region : AWS Region to fetch the cluster, service, task
    -p | --profile : AWS Profile for credentials and region.
    -c | --command : Command to execute, defaults to '/bin/sh'/
    -r | --region : AWS Region to fetch the cluster, service, task
    -p | --profile : AWS Profile for credentials and region.
    -c | --command : Command to execute, defaults to '/bin/sh'/
    --port : Port number for port forward.
    --local-port : Local port number for port forward.
    The default command executed on the selected container is '/bin/sh'.
    If a region is not provided, the script will attempt to use your region set in the profile.
    @@ -143,71 +145,87 @@ END
    }

    main(){
    command='/bin/sh'
    while [[ "$#" -gt 0 ]]; do
    case $1 in
    -h|--help)
    print_help
    exit
    ;;
    -r|--region)
    shift
    region="${1:?Region must be specified in --region}"
    shift
    ;;
    -p|--profile)
    shift
    profile="${1:?Profile must be specified in --profile}"
    shift
    ;;
    -c|--command)
    shift
    command="${1:?Command must be specified in --command}"
    shift
    ;;
    *)
    die "Unknown param $1"
    ;;
    esac
    done

    echo_stderr "Validating pre-requisites...."
    validatePrereq

    # spaces matter :)
    if [[ $AWS_CLI_VERSION =~ ^2 ]] ; then
    echo_stderr "Select AWS Profile."
    profile=`selectProfile`
    colorEcho profile: $profile
    else echo_stderr "[INFO] AWS CLI is not v2, unable to select profile. --region or --profile must be set."
    fi
    echo_stderr

    echo_stderr "Select cluster."
    cluster=`selectCluster`
    colorEcho cluster: $cluster
    echo_stderr

    echo_stderr "Select service."
    service=`selectService`
    colorEcho service: $service
    echo_stderr

    echo_stderr "Select task."
    task=`selectTask`
    colorEcho task: $task
    echo_stderr

    echo_stderr "Select container."
    container=`selectContainer`
    colorEcho container: $container
    echo_stderr

    echo_stderr "Executing command (you might have to upgrade your AWS CLI if this fails)"
    cmd="aws ecs execute-command $(params) --cluster $cluster --container $container --task $task --interactive --command '$command'"
    colorEcho $cmd

    $cmd
    command='/bin/sh'
    while [[ "$#" -gt 0 ]]; do
    case $1 in
    -h|--help)
    print_help
    exit
    ;;
    -r|--region)
    shift
    region="${1:?Region must be specified in --region}"
    shift
    ;;
    -p|--profile)
    shift
    profile="${1:?Profile must be specified in --profile}"
    shift
    ;;
    -c|--command)
    shift
    command="${1:?Command must be specified in --command}"
    shift
    ;;
    --port)
    shift
    port="${1:?port must be specified in --port}"
    shift
    ;;
    --local-port)
    shift
    localPort="${1:?local-port must be specified in --local-port}"
    shift
    ;;
    *)
    die "Unknown param $1"
    ;;
    esac
    done

    echo_stderr "Validating pre-requisites...."
    validatePrereq

    # spaces matter :)
    if [[ $AWS_CLI_VERSION =~ ^2 ]] ; then
    echo_stderr "Select AWS Profile."
    profile=`selectProfile`
    colorEcho profile: $profile
    else echo_stderr "[INFO] AWS CLI is not v2, unable to select profile. --region or --profile must be set."
    fi
    echo_stderr

    echo_stderr "Select cluster."
    cluster=`selectCluster`
    colorEcho cluster: $cluster
    echo_stderr

    echo_stderr "Select service."
    service=`selectService`
    colorEcho service: $service
    echo_stderr

    echo_stderr "Select task."
    task=`selectTask`
    colorEcho task: $task
    echo_stderr

    echo_stderr "Select container."
    container=`selectContainer`
    colorEcho container: $container
    echo_stderr

    # Both port and localPort parameters are not supplied.
    if [ -z ${port+x} ] && [ -z ${localPort+x} ]; then
    cmd="aws ecs execute-command $(params) --cluster $cluster --container $container --task $task --interactive --command '$command'"
    else
    taskId=$(echo $task | awk -F '/' '{print $3}')
    containerId=$(aws ecs describe-tasks $(params) --cluster $cluster --task $task | jq -r --arg container $container '.tasks[0].containers[] | select(.name == $container).runtimeId')
    cmd="aws ssm start-session $(params) --target ecs:${cluster}_${taskId}_${containerId} --document-name AWS-StartPortForwardingSession --parameters {\"portNumber\":[\"$port\"],\"localPortNumber\":[\"$localPort\"]}"
    fi

    colorEcho $cmd
    $cmd
    }

    # Execute main function and pass all params over
  6. @yuki777 yuki777 revised this gist Nov 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sssh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # Usage: See --help.
    #
    # Installation: Download the script and `chmod u+x the script`.
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/c2be2fac364c3f3d4be59774c29358ed1b370c02/sssh -o sssh && chmod u+x sssh
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/5bfa8242ba00ed336da76711d602e6df4f76a452/sssh -o sssh && chmod u+x sssh
    # ./sssh
    #
    # Prerequisites (validated)
  7. @yuki777 yuki777 revised this gist Nov 8, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # Usage: See --help.
    #
    # Installation: Download the script and `chmod u+x the script`.
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/7158dfae99e7277d4f4614f81092a5dafaa16fed/sssh -o sssh && chmod u+x sssh
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/c2be2fac364c3f3d4be59774c29358ed1b370c02/sssh -o sssh && chmod u+x sssh
    # ./sssh
    #
    # Prerequisites (validated)
    @@ -16,7 +16,7 @@
    # Special thanks to contributor
    # - leewc

    set -eux
    set -eu

    selectProfile(){
    # profile parameter not supplied.
  8. @yuki777 yuki777 revised this gist Nov 8, 2021. 1 changed file with 188 additions and 71 deletions.
    259 changes: 188 additions & 71 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -1,97 +1,214 @@
    #!/bin/bash
    set -eu

    # Prerequisite
    # Bash script to run ecs-exec on Amazon ECS Fargate containers.
    #
    # Usage: See --help.
    #
    # Installation: Download the script and `chmod u+x the script`.
    # curl https://gist.github.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/7158dfae99e7277d4f4614f81092a5dafaa16fed/sssh -o sssh && chmod u+x sssh
    # ./sssh
    #
    # Prerequisites (validated)
    # - aws cli
    # - session-manager-plugin
    # - jq
    #
    # Special thanks to contributor
    # - leewc

    set -eux

    selectProfile(){
    select selected in `aws configure list-profiles`
    do
    break
    done
    echo $selected
    # profile parameter not supplied.
    if [ -z ${profile+x} ]; then
    # only works with AWS CLIv2.
    select selected in `aws configure list-profiles`
    do
    break
    done
    echo $selected
    else
    echo $profile
    fi
    }

    params(){
    echo "$(profileParam) $(regionParam)"
    }

    profileParam() {
    [[ $profile ]] &>/dev/null && echo "--profile $profile"
    }

    regionParam() {
    [[ $region ]] &>/dev/null && echo "--region $region"
    }

    selectCluster(){
    select selected in `aws ecs list-clusters --profile $profile|jq -r ".clusterArns[]"|sort|cut -d "/" -f 2`
    do
    break
    done
    echo $selected
    select selected in $(aws ecs list-clusters $(params)|jq -r ".clusterArns[]"|sort|cut -d "/" -f 2)
    do
    break
    done
    echo $selected
    }

    selectService(){
    select selected in `aws ecs list-services --profile $profile --cluster $cluster|jq -r ".serviceArns[]"|sort`
    do
    break
    done
    echo $selected
    select selected in $(aws ecs list-services $(params) --cluster $cluster|jq -r ".serviceArns[]"|sort)
    do
    break
    done
    echo $selected
    }

    selectTask(){
    select selected in `aws ecs list-tasks --cluster $cluster --profile $profile --service-name $service --desired-status RUNNING |jq -r '.taskArns[]'|sort`
    do
    break
    done
    echo $selected
    select selected in $(aws ecs list-tasks $(params) --cluster $cluster --service-name $service --desired-status RUNNING |jq -r '.taskArns[]'|sort)
    do
    break
    done
    echo $selected
    }

    selectContainer(){
    select selected in `aws ecs describe-tasks --cluster $cluster --tasks $task --profile $profile|jq -r ".tasks[].containers[].name"|sort`
    do
    break
    done
    echo $selected
    select selected in $(aws ecs describe-tasks $(params) --cluster $cluster --tasks $task | jq -r ".tasks[].containers[].name"|sort)
    do
    break
    done
    echo $selected
    }

    colorEcho(){
    red='\033[0;31m'
    green='\033[0;32m'
    yellow='\033[0;33m'
    reset='\033[0m'

    if echo $@ | egrep -q "prd|prod"; then
    color=$red
    elif echo $@ | egrep -q "stg|stage|staging"; then
    color=$yellow
    else
    color=$green
    fi

    echo -e "${color}$@${reset}"
    red='\033[0;31m'
    green='\033[0;32m'
    yellow='\033[0;33m'
    reset='\033[0m'

    if echo $@ | egrep -q "prd|prod|production"; then
    color=$red
    elif echo $@ | egrep -q "stg|stage|staging|beta|devo"; then
    color=$yellow
    else
    color=$green
    fi

    echo -e "${color}$@${reset}"
    }

    echo_stderr() {
    echo -e "$@" >&2
    }

    die() {
    echo_stderr "$@"
    exit 1
    }

    validatePrereq() {
    command -v jq &>/dev/null || die "jq not installed on host. Please install jq. See https://stedolan.github.io/jq/download/"
    command -v session-manager-plugin &>/dev/null || die "session-manager-plugin not installed. See https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
    command -v aws &>/dev/null || die "AWS CLI not found, AWS CLI version 1.16.12 or later must be installed. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    # Checks if AWS CLI is outdated or not., v1 of AWS CLI pipes to std error, redirect
    AWS_CLI_VERSION=$(aws --version 2>&1 | awk '{ print $1 }' | cut -d/ -f2)
    echo_stderr "You have AWS CLI v$AWS_CLI_VERSION installed."
    # Do a best effort check for v1 (so that it's at least 1.10 and up.
    [[ $AWS_CLI_VERSION =~ ^1.1[0-9] || $AWS_CLI_VERSION =~ ^2 ]] &>/dev/null || die "AWS CLI version 1.16.12 or later must be installed to support ecs-exec, Run 'aws --version' to see what you have. See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
    }

    function print_help() {
    cat >&2 <<-END
    This script simplifies the process of getting the required information to drop into an
    interactive shell script on your container hosted on Fargate/ECS.
    Example:
    ./sssh --region us-west-2
    ./sssh --profile default
    Supported input parameters:
    -r | --region : AWS Region to fetch the cluster, service, task
    -p | --profile : AWS Profile for credentials and region.
    -c | --command : Command to execute, defaults to '/bin/sh'/
    The default command executed on the selected container is '/bin/sh'.
    If a region is not provided, the script will attempt to use your region set in the profile.
    If you want to execute a different command or shell, you can pass it in like so:
    ./sssh --command '/bin/bash'
    You need active (unexpired) AWS credentials, otherwise, the script will crash.
    Updates on https://gist.github.com/leewc/e4c3a16551b06c2b0b4640fa5a3d9c00
    END
    }

    main(){
    echo "Select aws profile."
    profile=`selectProfile`
    colorEcho profile: $profile
    echo

    echo "Select cluster."
    cluster=`selectCluster`
    colorEcho cluster: $cluster
    echo

    echo "Select service."
    service=`selectService`
    colorEcho service: $service
    echo

    echo "Select task."
    task=`selectTask`
    colorEcho task: $task
    echo

    echo "Select container."
    container=`selectContainer`
    colorEcho container: $container
    echo

    cmd="aws ecs execute-command --profile $profile --cluster $cluster --container $container --task $task --interactive --command '/bin/sh'"
    colorEcho $cmd

    $cmd
    command='/bin/sh'
    while [[ "$#" -gt 0 ]]; do
    case $1 in
    -h|--help)
    print_help
    exit
    ;;
    -r|--region)
    shift
    region="${1:?Region must be specified in --region}"
    shift
    ;;
    -p|--profile)
    shift
    profile="${1:?Profile must be specified in --profile}"
    shift
    ;;
    -c|--command)
    shift
    command="${1:?Command must be specified in --command}"
    shift
    ;;
    *)
    die "Unknown param $1"
    ;;
    esac
    done

    echo_stderr "Validating pre-requisites...."
    validatePrereq

    # spaces matter :)
    if [[ $AWS_CLI_VERSION =~ ^2 ]] ; then
    echo_stderr "Select AWS Profile."
    profile=`selectProfile`
    colorEcho profile: $profile
    else echo_stderr "[INFO] AWS CLI is not v2, unable to select profile. --region or --profile must be set."
    fi
    echo_stderr

    echo_stderr "Select cluster."
    cluster=`selectCluster`
    colorEcho cluster: $cluster
    echo_stderr

    echo_stderr "Select service."
    service=`selectService`
    colorEcho service: $service
    echo_stderr

    echo_stderr "Select task."
    task=`selectTask`
    colorEcho task: $task
    echo_stderr

    echo_stderr "Select container."
    container=`selectContainer`
    colorEcho container: $container
    echo_stderr

    echo_stderr "Executing command (you might have to upgrade your AWS CLI if this fails)"
    cmd="aws ecs execute-command $(params) --cluster $cluster --container $container --task $task --interactive --command '$command'"
    colorEcho $cmd

    $cmd
    }
    main

    # Execute main function and pass all params over
    main $@
  9. @yuki777 yuki777 revised this gist Oct 22, 2021. No changes.
  10. @yuki777 yuki777 revised this gist Sep 15, 2021. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    #!/bin/bash
    set -eu

    # Requirements
    # Prerequisite
    # - aws cli
    # - session-manager-plugin
    # - github cli 2.0
    # - jq

    selectProfile(){
    @@ -53,9 +52,9 @@ colorEcho(){
    yellow='\033[0;33m'
    reset='\033[0m'

    if echo $@ | grep -q "prd"; then
    if echo $@ | egrep -q "prd|prod"; then
    color=$red
    elif echo $@ | grep -q "stg"; then
    elif echo $@ | egrep -q "stg|stage|staging"; then
    color=$yellow
    else
    color=$green
  11. @yuki777 yuki777 revised this gist Sep 15, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion sssh
    Original file line number Diff line number Diff line change
    @@ -93,7 +93,6 @@ main(){
    cmd="aws ecs execute-command --profile $profile --cluster $cluster --container $container --task $task --interactive --command '/bin/sh'"
    colorEcho $cmd

    # Run command
    $cmd
    }
    main
  12. @yuki777 yuki777 created this gist Sep 15, 2021.
    99 changes: 99 additions & 0 deletions sssh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    #!/bin/bash
    set -eu

    # Requirements
    # - aws cli
    # - session-manager-plugin
    # - github cli 2.0
    # - jq

    selectProfile(){
    select selected in `aws configure list-profiles`
    do
    break
    done
    echo $selected
    }

    selectCluster(){
    select selected in `aws ecs list-clusters --profile $profile|jq -r ".clusterArns[]"|sort|cut -d "/" -f 2`
    do
    break
    done
    echo $selected
    }

    selectService(){
    select selected in `aws ecs list-services --profile $profile --cluster $cluster|jq -r ".serviceArns[]"|sort`
    do
    break
    done
    echo $selected
    }

    selectTask(){
    select selected in `aws ecs list-tasks --cluster $cluster --profile $profile --service-name $service --desired-status RUNNING |jq -r '.taskArns[]'|sort`
    do
    break
    done
    echo $selected
    }

    selectContainer(){
    select selected in `aws ecs describe-tasks --cluster $cluster --tasks $task --profile $profile|jq -r ".tasks[].containers[].name"|sort`
    do
    break
    done
    echo $selected
    }

    colorEcho(){
    red='\033[0;31m'
    green='\033[0;32m'
    yellow='\033[0;33m'
    reset='\033[0m'

    if echo $@ | grep -q "prd"; then
    color=$red
    elif echo $@ | grep -q "stg"; then
    color=$yellow
    else
    color=$green
    fi

    echo -e "${color}$@${reset}"
    }

    main(){
    echo "Select aws profile."
    profile=`selectProfile`
    colorEcho profile: $profile
    echo

    echo "Select cluster."
    cluster=`selectCluster`
    colorEcho cluster: $cluster
    echo

    echo "Select service."
    service=`selectService`
    colorEcho service: $service
    echo

    echo "Select task."
    task=`selectTask`
    colorEcho task: $task
    echo

    echo "Select container."
    container=`selectContainer`
    colorEcho container: $container
    echo

    cmd="aws ecs execute-command --profile $profile --cluster $cluster --container $container --task $task --interactive --command '/bin/sh'"
    colorEcho $cmd

    # Run command
    $cmd
    }
    main