From 0c762792610d5b4790d5992dac6ab60865ed91d8 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Tue, 9 Jul 2013 14:04:06 +0200 Subject: [PATCH 01/10] Support tags that include whitespaces. --- bin/ec2-ssh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ec2-ssh b/bin/ec2-ssh index 3afd98b..816405f 100755 --- a/bin/ec2-ssh +++ b/bin/ec2-ssh @@ -59,7 +59,7 @@ else fi # get host from ec2-host command -host=$(eval "$cmd $inst") +host=$(eval "$cmd '$inst'") # pass all other parameters (${@:2}) to ssh allowing # things like: ec2-ssh nginx uptime From 337f3500a8ee6785dde44d2c0522adc231d021c4 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Wed, 10 Jul 2013 10:20:12 +0200 Subject: [PATCH 02/10] Add options (-a, -u) to select default user (ec2-user, ubuntu). Add option (-S) to disable SSH host-key checks. --- bin/ec2-ssh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bin/ec2-ssh b/bin/ec2-ssh index 816405f..7b8599c 100755 --- a/bin/ec2-ssh +++ b/bin/ec2-ssh @@ -15,6 +15,9 @@ For a list of instances, run ec2-host without any paramteres -s, SECRET Amazon EC2 Secret, defaults to \$AWS_SECRET_ACCESS_KEY -r, REGION Amazon EC2 Region, defaults to us-east-1 -t, TAG Tag name for searching, defaults to 'Name' + -u Set default user to 'ubuntu' (eg. Ubuntu) + -a Set default user to 'ec2-user' (eg. Amazon Linux) + -S Disable SSH security (host key checks) EOF } @@ -23,13 +26,17 @@ test $# -eq 0 && { usage; exit; } # Process options cmd="ec2-host" -while getopts ":hk:s:r:t:" opt; do +default_user="ubuntu" +while getopts ":hk:s:r:t:auS" opt; do case $opt in h ) usage; exit 1;; k ) cmd="$cmd -k $OPTARG";; s ) cmd="$cmd -s $OPTARG";; r ) cmd="$cmd -r $OPTARG";; t ) cmd="$cmd -t $OPTARG";; + a ) default_user="ec2-user";; + u ) default_user="ubuntu";; + S ) ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=false";; \? ) usage; exit 1 esac done @@ -43,7 +50,7 @@ user="${hostparts[0]}" if [ -z "$inst" ]; then inst="$1" - user="ubuntu" + user="$default_user" fi # support tag:value format for identifying instances @@ -67,4 +74,4 @@ cmd="echo \\\". ~/.bashrc && PS1='\[\e]0;$inst: \w\\\a\]\[\\\033[01;32m\]$inst\[ if test "${@:2}"; then cmd="${@:2}" fi -test -n "$host" && echo "Connecting to $host." && exec sh -c "ssh -t $user@$host \"$cmd\"" +test -n "$host" && echo "Connecting to $host." && exec sh -c "ssh $ssh_opts -t $user@$host \"$cmd\"" From cebdefce7fdfcab512506da336b16fbc61fa5c01 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Thu, 11 Jul 2013 11:04:43 +0200 Subject: [PATCH 03/10] Introduce generic "-U" switch to set default user. --- bin/ec2-ssh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bin/ec2-ssh b/bin/ec2-ssh index 7b8599c..bc15393 100755 --- a/bin/ec2-ssh +++ b/bin/ec2-ssh @@ -15,8 +15,7 @@ For a list of instances, run ec2-host without any paramteres -s, SECRET Amazon EC2 Secret, defaults to \$AWS_SECRET_ACCESS_KEY -r, REGION Amazon EC2 Region, defaults to us-east-1 -t, TAG Tag name for searching, defaults to 'Name' - -u Set default user to 'ubuntu' (eg. Ubuntu) - -a Set default user to 'ec2-user' (eg. Amazon Linux) + -U, USER Set default user to 'USER' (default 'ubuntu') -S Disable SSH security (host key checks) EOF } @@ -27,15 +26,14 @@ test $# -eq 0 && { usage; exit; } # Process options cmd="ec2-host" default_user="ubuntu" -while getopts ":hk:s:r:t:auS" opt; do +while getopts ":hk:s:r:t:U:S" opt; do case $opt in h ) usage; exit 1;; k ) cmd="$cmd -k $OPTARG";; s ) cmd="$cmd -s $OPTARG";; r ) cmd="$cmd -r $OPTARG";; t ) cmd="$cmd -t $OPTARG";; - a ) default_user="ec2-user";; - u ) default_user="ubuntu";; + U ) default_user="$OPTARG";; S ) ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=false";; \? ) usage; exit 1 esac From f34385dc2897171eaa05ef990cb2edb78e87ea76 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Tue, 16 Jul 2013 00:34:19 +0200 Subject: [PATCH 04/10] Add bash completion for ec2-ssh. --- bin/ec2-host | 14 +++++++++----- completion.bash | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 completion.bash diff --git a/bin/ec2-host b/bin/ec2-host index f1637b0..e6e84c1 100755 --- a/bin/ec2-host +++ b/bin/ec2-host @@ -19,14 +19,15 @@ Try `ec2-ssh --help' for more information.""" def full_usage(): - print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [NAME] + print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [-S sep] [NAME] Prints server host name. --help display this help and exit -k, --aws-key KEY Amazon EC2 Key, defaults to ENV[AWS_ACCESS_KEY_ID] -s, --aws-secret SECRET Amazon EC2 Secret, defaults to ENV[AWS_SECRET_ACCESS_KEY] -r, --region REGION Amazon EC2 Region, defaults to us-east-1 or ENV[AWS_EC2_REGION] - -t, --tag TAG Tag name for searching, defaults to 'Name'""" + -t, --tag TAG Tag name for searching, defaults to 'Name' + -S, --separator SEP Separator to use between hostname and dns""" def ec2_active_instances(label_tag, filters): @@ -48,8 +49,8 @@ def ec2_active_instances(label_tag, filters): def main(argv): try: - opts, args = getopt.getopt(argv, "hLk:s:r:t:", - ["help", "aws-key=", "aws-secret=", "region=", "tag="]) + opts, args = getopt.getopt(argv, "hLk:s:r:t:S:", + ["help", "aws-key=", "aws-secret=", "region=", "tag=", "separator="]) except getopt.GetoptError, err: print >>sys.stderr, err short_usage() @@ -59,6 +60,7 @@ def main(argv): aws_secret = os.environ.get("AWS_SECRET_ACCESS_KEY") region = os.environ.get("AWS_EC2_REGION") tag = "Name" + separator="\t" for opt, arg in opts: if opt in ("-h", "--help"): @@ -72,6 +74,8 @@ def main(argv): region = arg elif opt in ("-t", "--tag"): tag = arg + elif opt in ("-S", "--separator"): + separator = arg if not aws_key or not aws_secret: if not aws_key: @@ -115,7 +119,7 @@ def main(argv): return elif numinstances == 0 or numinstances > 1: for pair in sorted(instances, key=lambda p: p[0]): - print "%s\t%s" % pair + print "%s%s%s" % (pair[0], separator, pair[1]) sys.exit(0) return diff --git a/completion.bash b/completion.bash new file mode 100644 index 0000000..991f76b --- /dev/null +++ b/completion.bash @@ -0,0 +1,24 @@ +_ec2_ssh() { + local cur opts IFS + declare -a hosts + + COMPREPLY=() + cur="${COMP_WORDS[$COMP_CWORD]}" + + opts="-k -s -r -t" + if [ "${cur}" == "-" ]; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + + IFS=$'\n' + if [ "$cur" != "" ]; then + hosts=( $(ec2-host --separator ';' | cut -f1 -d';' | grep $cur) ) + else + hosts=( $(ec2-host --separator ';' | cut -f1 -d';') ) + fi + COMPREPLY=( $(printf '%q\n' "${hosts[@]}") ) + return 0 +} + +complete -F _ec2_ssh ec2-ssh From 372c6d9d7c74b77b56e44e646926cc0e8b34b5ca Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Tue, 16 Jul 2013 21:42:45 +0200 Subject: [PATCH 05/10] Add zsh completion. --- completion.zsh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 completion.zsh diff --git a/completion.zsh b/completion.zsh new file mode 100644 index 0000000..4eaff5d --- /dev/null +++ b/completion.zsh @@ -0,0 +1,26 @@ +#compdef ec2-ssh + +_ec2_ssh() { + local state IFS + typeset -A opt_args + + _arguments -s -S \ + "-k+[KEY]" \ + "-s+[SECRET]" \ + "-t+[TAG]" \ + "-r+[REGION]" \ + "*: :->hosts" + + case $state in + hosts) + IFS=$'\n' + hosts=( $(ec2-host --separator ';' | cut -f1 -d';') ) + compadd "$@" "${hosts[@]}" + ;; + *) + # nothing for now + ;; + esac +} + +_ec2_ssh "$@" From 47309c43ee1949d853049a4f1b5c7ae179cee8d7 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Tue, 16 Jul 2013 21:42:57 +0200 Subject: [PATCH 06/10] Add some small documentation. --- Readme.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Readme.md diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..4945307 --- /dev/null +++ b/Readme.md @@ -0,0 +1,13 @@ +This is `ec2-ssh`, a command line tool to simplify SSH connections to AWS EC2 machines. + +See the original (blog post from Instagram)[http://instagram-engineering.tumblr.com/post/11399488246/simplifying-ec2-ssh-connections] + +# What changed in this fork? + +- add an option to supply to default ssh username +- bash and zsh completion +- add script-readable output to `ec2-host` + +# How to use shell completions? + +Document me... From f7a7b57427b7a4980a5b74a5e1c1e0b6e2396ae0 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Tue, 16 Jul 2013 21:43:58 +0200 Subject: [PATCH 07/10] Ignore temp and distfiles. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac2f535 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +build/ +dist/ From 3263084a2da5e6171aaf61d88a01aa6edd862113 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Fri, 19 Jul 2013 10:50:49 +0200 Subject: [PATCH 08/10] Introduce '-R' option, that will trigger 'sudo -i'. --- bin/ec2-ssh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/ec2-ssh b/bin/ec2-ssh index bc15393..a126f15 100755 --- a/bin/ec2-ssh +++ b/bin/ec2-ssh @@ -17,6 +17,7 @@ For a list of instances, run ec2-host without any paramteres -t, TAG Tag name for searching, defaults to 'Name' -U, USER Set default user to 'USER' (default 'ubuntu') -S Disable SSH security (host key checks) + -R Become root via 'sudo -i' after login EOF } @@ -26,7 +27,7 @@ test $# -eq 0 && { usage; exit; } # Process options cmd="ec2-host" default_user="ubuntu" -while getopts ":hk:s:r:t:U:S" opt; do +while getopts ":hk:s:r:t:U:SR" opt; do case $opt in h ) usage; exit 1;; k ) cmd="$cmd -k $OPTARG";; @@ -35,6 +36,7 @@ while getopts ":hk:s:r:t:U:S" opt; do t ) cmd="$cmd -t $OPTARG";; U ) default_user="$OPTARG";; S ) ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=false";; + R ) cmd_extra="&& sudo -i";; \? ) usage; exit 1 esac done @@ -68,7 +70,8 @@ host=$(eval "$cmd '$inst'") # pass all other parameters (${@:2}) to ssh allowing # things like: ec2-ssh nginx uptime -cmd="echo \\\". ~/.bashrc && PS1='\[\e]0;$inst: \w\\\a\]\[\\\033[01;32m\]$inst\[\\\033[00m\]:\[\\\033[01;34m\]\w\[\\\033[00m\]\\\$ '\\\" > ~/.ec2sshrc; /bin/bash --rcfile .ec2sshrc -i" + +cmd="echo \\\". ~/.bashrc && PS1='\[\e]0;$inst: \w\\\a\]\[\\\033[01;32m\]$inst\[\\\033[00m\]:\[\\\033[01;34m\]\w\[\\\033[00m\]\\\$ '${cmd_extra}\\\" > ~/.ec2sshrc; /bin/bash --rcfile .ec2sshrc -i" if test "${@:2}"; then cmd="${@:2}" fi From 148ee33726b68d102f3db1c33347d6c754b990f4 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Wed, 15 Oct 2014 15:39:43 +0200 Subject: [PATCH 09/10] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 4945307..7565dc4 100644 --- a/Readme.md +++ b/Readme.md @@ -10,4 +10,4 @@ See the original (blog post from Instagram)[http://instagram-engineering.tumblr. # How to use shell completions? -Document me... +Document me.... From 33f13ea45d5479bbdf42e492a6dc8ae6dff1b369 Mon Sep 17 00:00:00 2001 From: Jens Braeuer Date: Thu, 16 Oct 2014 16:02:53 +0200 Subject: [PATCH 10/10] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 7565dc4..2209fd4 100644 --- a/Readme.md +++ b/Readme.md @@ -10,4 +10,4 @@ See the original (blog post from Instagram)[http://instagram-engineering.tumblr. # How to use shell completions? -Document me.... +Document me.....