-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathec2ssh.sh
More file actions
executable file
·39 lines (33 loc) · 1.52 KB
/
Copy pathec2ssh.sh
File metadata and controls
executable file
·39 lines (33 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
# Decription
# ----------
# * Writes all running AWS EC2 instances with defined name to [SSH config file](https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/)
# * So you write `ssh instance_name` instead of `ssh -i ~/.ssh/gate.pem ec2-user@ec2-12-345-67-89.us-east-1.compute.amazonaws.com`
# * Autocompletion!
# * Command history is clean and reusable for `ssh` and `scp`
# * Instance IP change on reboot is not problem anymore
# * Works well with [sssh2](https://github.com/dmi3/bin/blob/master/sssh2).
# Author: Dmitry (http://dmi3.net)
# Source: https://github.com/dmi3/bin
# Instalation
# -----------
# * Setup [aws-cli](https://github.com/aws/aws-cli#aws-cli)
# * (Optional) Install [latest Openssh in 16.04](https://gist.github.com/stefansundin/0fd6e9de172041817d0b8a75f1ede677)
# * Change credentials in `TEMPLATE` ↓
# Usage
# -----
# * If you have Openssh > 7.3:
# - `ec2ssh.sh | tee ~/.ssh/aws_config`
# - Add [Include aws_config](https://superuser.com/a/1142813) to `~/.ssh/config`
# * Else (will overwrite file)
# - ec2ssh.sh | tee ~/.ssh/config
TEMPLATE="
Host \$1
HostName \$0
User ec2-user
IdentityFile ~/.ssh/gate.pem
IdentitiesOnly yes"
aws ec2 describe-instances \
--filter "Name=tag-key,Values=Name" "Name=tag-value,Values=*" "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*][NetworkInterfaces[0].Association.PublicDnsName,Tags[?Key=='Name'].Value[] | [0]]" \
--output text | xargs -L 1 bash -c "echo -e \"$TEMPLATE\""