Skip to content

Instantly share code, notes, and snippets.

@benjojo benjojo/wow.sh Secret
Last active Feb 6, 2019

Embed
What would you like to do?
build a bgp config for backing anycast
#!/bin/bash
apt-get install -y bird jq
# Ask for the metadata of the VM to build the bird config
curl http://169.254.169.254/v1.json -o /tmp/serverdata.json
RID=$(cat /tmp/serverdata.json | jq -r '.bgp.ipv4."my-address"')
echo "router id $RID;" > /tmp/bird6.conf
REGION=$(echo -n 0)
DEPLOYMENT=$(echo -n 0)
LOCALAS=$(cat /tmp/serverdata.json | jq -r '.bgp.ipv6."my-asn"')
LOCALIP=$(cat /tmp/serverdata.json | jq -r '.bgp.ipv6."my-address"')
AIRPORTCODE=$(cat /tmp/serverdata.json | jq -r .region.regioncode)
case "$AIRPORTCODE" in
LHR)
REGION=1 # EU
DEPLOYMENT=1
;;
AMS)
REGION=1 # EU
DEPLOYMENT=2
;;
CDG)
REGION=1 # EU
DEPLOYMENT=3
;;
FRA)
REGION=1 # EU
DEPLOYMENT=4
;;
EWR) # New Jersy USA
REGION=2 # E USA
DEPLOYMENT=1
;;
MIA) # Miami USA
REGION=2 # E USA
DEPLOYMENT=2
;;
ATL) # Atlanta USA
REGION=2 # E USA
DEPLOYMENT=3
;;
ORD) # Chicago USA
REGION=2 # E USA
DEPLOYMENT=4
;;
SJC) # Bay area USA
REGION=3 # W USA
DEPLOYMENT=1
;;
LAX) # Los Angeles USA
REGION=3 # W USA
DEPLOYMENT=2
;;
SEA) # Seattle USA
REGION=3 # W USA
DEPLOYMENT=3
;;
DFW) # Dallas USA
REGION=3 # W USA
DEPLOYMENT=4
;;
NRT) # Tokyo Asia
REGION=4 # Asia
DEPLOYMENT=1
;;
SGP) # Singapore
REGION=4 # Asia
DEPLOYMENT=2
;;
SYD) # Sydney, Kind of Asia for the case of testing
REGION=4 # Asia
DEPLOYMENT=3
;;
esac
cat <<EOF | tee /tmp/generic
protocol kernel {
persist; # Don't remove routes on bird shutdown
scan time 20; # Scan kernel routing table every 20 seconds
export none;
import none;
}
protocol device {
scan time 10; # Scan interfaces every 10 seconds
}
EOF
cat <<EOF | tee /tmp/bgpsession
protocol bgp vultr
{
local as $LOCALAS;
source address $LOCALIP;
import none;
graceful restart on;
multihop 2;
neighbor 2001:19f0:ffff::1 as 64515;
password "xxxxxxxxx";
EOF
echo " export filter {" >> /tmp/bgpsession
cat /tmp/generic >> /tmp/bird6.conf
echo "protocol static" > /tmp/statics
echo "{" >> /tmp/statics
HACK="0"
for UserGroup in {1..2};
do
printf "\troute 2a07:1500:$UserGroup$HACK$HACK$HACK::/36 via $LOCALIP;\n"
ip -6 route add local "2a07:1500:$UserGroup$HACK$HACK$HACK::/36" dev lo
printf "\troute 2a07:1500:$UserGroup$REGION$HACK$HACK::/40 via $LOCALIP;\n"
printf "\troute 2a07:1500:$UserGroup$REGION$DEPLOYMENT$HACK::/44 via $LOCALIP;\n"
for Link in {"1,2914","2,174","3,3356","4,1299","5,2516","6,3257","7,6939","8,63956","9,17819","A,7922","B,0"};
do
address=$(echo $Link | awk -F "," '{print $1}')
upstream=$(echo $Link | awk -F "," '{print $2}')
printf "\troute 2a07:1500:$UserGroup$REGION$DEPLOYMENT$address::/48 via $LOCALIP;\n"
if [ "$upstream" -eq "0" ]
then
printf "\tif (net = 2a07:1500:$UserGroup$REGION$DEPLOYMENT$address::/48) then {$TRANSITLIST }\n" >> /tmp/bgpsession
else
printf "\tif (net = 2a07:1500:$UserGroup$REGION$DEPLOYMENT$address::/48) then { bgp_community.add((20473,6000)); bgp_community.add((64699,$upstream)); }\n" >> /tmp/bgpsession
TRANSITLIST=$(printf "$TRANSITLIST bgp_community.add((64600,$upstream));")
fi
done
done >> /tmp/statics
echo "}" >> /tmp/statics
echo "accept;};}" >> /tmp/bgpsession
cat /tmp/statics >> /tmp/bird6.conf
cat /tmp/bgpsession >> /tmp/bird6.conf
cp /tmp/bird6.conf /etc/bird/bird6.conf
systemctl restart bird6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.