-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-hosts-file.sh
executable file
·15 lines (12 loc) · 1.11 KB
/
generate-hosts-file.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
# This is a simple command that will update the /etc/hosts file (usually on a bastion host)
# with the internal IP addresses of running instances.
#
# This works... most of the time. This should be broken out into a script that can do some
# error checking. The command breaks when a host has a null value for the name tag.
#
# It's a single command to just add to cron like this:
#
# */10 * * * * echo -e '127.0.0.1\tlocalhost localhost.localdomain' > /etc/hosts && /usr/bin/aws ec2 describe-instances --region us-east-1 --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/\t/' |sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 >> /etc/hosts
# The command by itself
echo -e '127.0.0.1\tlocalhost localhost.localdomain' > /etc/hosts && /usr/bin/aws ec2 describe-instances --region us-east-1 --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/\t/' |sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 >> /etc/hosts