Skip to content

Instantly share code, notes, and snippets.

@wpc009
Last active February 18, 2019 08:23
Show Gist options
  • Select an option

  • Save wpc009/791ada99da28906908ec290568572edc to your computer and use it in GitHub Desktop.

Select an option

Save wpc009/791ada99da28906908ec290568572edc to your computer and use it in GitHub Desktop.
aliyun vpn gateway cloud-init user-data
#cloud-config
write_files:
- content: |
#!/bin/bash
META_URL=http://100.100.100.200/2016-01-01/meta-data
VPC_CIDR_BLOCK=$(curl $META_URL/vpc-cidr-block)
MASK=$(echo ${VPC_CIDR_BLOCK} | sed -E 's%([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)%\5%')
if [ $MASK == '8' ];then
PREFIX=$(echo ${VPC_CIDR_BLOCK} | sed -E 's%([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)%\1.253.253%')
elif [ $MASK == '16' ];then
PREFIX=$(echo ${VPC_CIDR_BLOCK} | sed -E 's%([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)%\1.\2.253%')
elif [ $MASK == '24' ];then
PREFIX=$(echo ${VPC_CIDR_BLOCK} | sed -E 's%([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)%\1.\2.\3%')
fi
if [ ! -d /etc/dnsmasq.d ];then
mkdir -p /etc/dnsmasq.d/
fi
cat <<EOF >/etc/dnsmasq.d/dnsmasq-dhcp
dhcp-range=${PREFIX}.100,${PREFIX}.200,255.255.255.0,6h
EOF
cat <<EOF >/etc/netplan/99-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
dhcp6: no
vpn_0:
dhcp4: yes
dhcp6: no
optional: true
dhcp4-overrides:
use-dns: no
tap_0:
dhcp4: no
dhcp6: no
addresses:
- ${PREFIX}.253/24
EOF
netplan generate
netplan apply
owner: root:root
path: /root/dnsmasq-init.sh
permissions: '0644'
runcmd:
- /bin/bash /root/dnsmasq-init.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment