GCP VMで静的パブリックIPを使っていない、つまり、動的(エフェメラル)IPなので、ダイナミックDNSが必要になる。
OCIでは、mydnsを使用しているが、GCPではno-ipを使用してみた。
・mydnsとの違いでは、ip-notify.shのパラメータは3つ、ユーザ、パスワード、DNS名を与える
気づいた修正点
・dipとcurlのパブリックip取得には差が発生するため、curlのみでパブリックIPを取得
以下の「hogeName hogePass hogeHost.ddns.net」の箇所は登録したno-ipのユーザ、パスワード、ホスト名
/etc/no-ip/notify-ip.sh
#!/bin/sh
# usage check
if [ $# != 3 ]; then
echo "usage: notify-ip.sh user password hostname"
exit;
fi
# notify ip to no-ip
date >> /var/log/notify-ip.log
curl -s "https://$1:$2@dynupdate.no-ip.com/nic/update?hostname=$3" >> \
/var/log/notify-ip.log
/etc/no-ip/notify-ip-change.sh
#!/bin/sh
# usage check
if [ $# != 3 ]; then
echo "usage: notify-ip.sh user password domain_name"
exit -1
fi
# compare ip address
IP_CURRENT=$(curl inet-ip.info)
FILE_DIR="/etc/no-ip/"
FILE_OLD="${FILE_DIR}old-$3"
CMD="${FILE_DIR}notify-ip.sh $1 $2 $3"
# exec notify ip shell
if [ -f $FILE_OLD ]; then
IP_OLD=$(cat $FILE_OLD)
if [ $IP_CURRENT != $IP_OLD ]; then
eval $CMD
fi
else
eval $CMD
fi
eval "echo $IP_CURRENT > $FILE_OLD"
/etc/systemd/system/noip.timer
[Unit]
Description=no-ip notify ip
[Timer]
OnCalendar=*-*-* 6:00:00
Unit=noip.service
[Install]
WantedBy=timers.target
/etc/systemd/system/noip.service
[Unit]
Description=no-ip notify ip
[Service]
Type=simple
ExecStart=/etc/no-ip/notify-ip.sh hogeName hogePass hogeHost.ddns.net
/etc/systemd/system/noip-short.timer
[Unit]
Description=no-ip notify ip short cycle
[Timer]
OnBootSec=10s
OnActiveSec=10s
OnUnitActiveSec=1min
Unit=noip-short.service
[Install]
WantedBy=timers.target
/etc/systemd/system/noip-short.service
[Unit]
Description=no-ip notify ip short cycle
[Service]
Type=simple
ExecStart=/etc/no-ip/notify-ip-change.sh hogeName hogePass hogeHost.ddns.net
登録
# systemctl daemon-reload
# systemctl enable noip.timer
# systemctl enable noip-short.timer
# systemctl start noip.timer
# systemctl start noip-short.timer
確認
# systemctl list-timers