Skip to content

Instantly share code, notes, and snippets.

@sorah
Created October 16, 2014 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sorah/7a0d7faf94ea7a6e952e to your computer and use it in GitHub Desktop.
Save sorah/7a0d7faf94ea7a6e952e to your computer and use it in GitHub Desktop.
Shell script assigns given private IPv4 addresses (e.g. secondary private IP) to a EC2 instance
if_macs="$(ip -o link show up | grep -v lo: | grep link/ether | sed -re 's|^[0-9]+: (.+?): .+link/ether ([0-9a-f:]+?).*$|\1,\2|')"
for if_mac in $if_macs; do
ifname="$(echo "$if_mac" | cut -d, -f 1)"
mac="$(echo "$if_mac" | cut -d, -f 2)"
ifmeta="http://169.254.169.254/latest/meta-data/network/interfaces/macs/${mac}"
cidr="$(curl -s ${ifmeta}/subnet-ipv4-cidr-block)"
netlen="$(echo "$cidr" | cut -d/ -f 2)"
local_ips="$(curl -s ${ifmeta}/local-ipv4s)"
for ip in $local_ips; do
echo "${ip}/${netlen} dev ${ifname}"
sudo ip addr add ${ip}/${netlen} dev ${ifname}
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment