Skip to content

Instantly share code, notes, and snippets.

@andrewmichaelsmith
Created July 18, 2012 19:29
Show Gist options
  • Save andrewmichaelsmith/3138288 to your computer and use it in GitHub Desktop.
Save andrewmichaelsmith/3138288 to your computer and use it in GitHub Desktop.
EC2 Allocation Script
#!/bin/bash
# This script is used to allocate a large number of IP addresses on EC2
# and associate them with network interfaces
#
# It gets the list of network interfaces
#TODO: You don't actually need instance_id, i don't think
if [ $# -eq 0 ] ; then
echo "Usage: $0 <instance_id>"
exit 1
fi
#Instance id
instance_id=$1
#Number of IPs to register
ips=1
#Maximum number of IPs per interface. I believe this is 30. I could be wrong.
max_ip_per_interfaces=30
#Get a list of ENIs
eni_list=( $(ec2-describe-network-interfaces | grep eni | nawk -F"\\t" '{print $2}') )
#Get the number of ENIs
eni_count=${#eni_list[@]}
echo "EC2 IP Allocation Script Starting.."
echo ""
echo "[+] Using instance_id: ${instance_id}"
echo "[+] Got ENIs: ${eni_list[*]}"
assigned_count=-1
for ((i = 0; i < ips; i++))
do
if [ $[$i % $max_ip_per_interfaces] == 0 ]; then
((assigned_count++))
echo "[+] Using network interface: ${eni_list[$[assigned_count]]}"
fi
#Assign ip
output=($(ec2-allocate-address -d vpc | nawk -F"\\t" '{print $2 " " $5}'))
eip="${output[1]}"
ip="${output[0]}"
echo "[+] Allocated ( eip , ip ) : ( ${eip} , ${ip} ) "
echo "[+] Associating.."
ec2-associate-address -n ${eni_list[$[assigned_count]]} -a ${eip}
if [ "${?}" -gt 0 ]; then
echo "[-] Error occurred. See above, fix it and run again. You may need to delete and start over."
exit 1
fi
echo "[+] Association completed without error"
done
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment