首先,在/root下建立network.sh,内容如下:
#!/bin/sh
ip route add 169.254.42.42 dev eth0 2>/dev/null
serverinfo=$(curl -s -X GET "http://169.254.42.42/conf?format=json")
ipv6info=$(echo $serverinfo | grep -Eo '"ipv6": {[^}]+}')
address=$(echo $ipv6info | sed -r 's/^.*"address": "([^"]+)".*$/\1/g')
gateway=$(echo $ipv6info | sed -r 's/^.*"gateway": "([^"]+)".*$/\1/g')
netmask=$(echo $ipv6info | sed -r 's/^.*"netmask": "([^"]+)".*$/\1/g')
if [ -n "$address" ] && [ -n "$gateway" ] && [ -n "$netmask" ]; then
ip addr flush dev eth0
ip addr add $address/$netmask dev eth0
ip -6 route add $gateway dev eth0
ip -6 route add default via $gateway
fi
然后修改/etc/network/interfaces的内容为如下:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
up sh /root/network.sh
是研究了cloud-init的源码,才找到了获取星辰ip的方法。
其他平台的可以自己修改下实现方法。