Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Created March 31, 2020 22:28
Show Gist options
  • Select an option

  • Save mamemomonga/3b0d327b775777798434ceac7dbccd1f to your computer and use it in GitHub Desktop.

Select an option

Save mamemomonga/3b0d327b775777798434ceac7dbccd1f to your computer and use it in GitHub Desktop.
EC2で接続されているネットワーク情報をインスタンス内で取得
#!/bin/bash
set -eu
get_meta_data() {
curl --retry 3 --silent --fail http://169.254.169.254/latest/meta-data/$1
}
PUB_MAC=$( get_meta_data mac )
for i in $( ls /sys/class/net ); do
if [ "$i" == "lo" ]; then continue; fi
if [ "$(cat /sys/class/net/$i/address)" == "$PUB_MAC" ]; then
VPC_CIDR_RANGE=$( get_meta_data network/interfaces/macs/$PUB_MAC/vpc-ipv4-cidr-block )
PUB_IF=$i
fi
done
PUB_IP=$( ip addr show $PUB_IF | perl -nlE 'if(m#inet ([^/]+)#) { say $1 }' )
echo "PUB_IF: $PUB_IF"
echo "PUB_MAC: $PUB_MAC"
echo "PUB_IP: $PUB_IP"
echo "VPC_CIDR_RANGE: $VPC_CIDR_RANGE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment