2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DDNSを定期更新するまわりくどいスクリプト

Last updated at Posted at 2020-10-03

DDNS更新をするまわりくどいシェルスクリプトです。
需要はほぼないでしょう。

ポイント

  • cronで1時間毎起動させる
  • わざわざ1時間前とIPが変わっているか判定してDDNS更新依頼をする
  • IPが変わっていない場合でも1日毎に更新依頼をする
  • わざわざURLパラメータでIPアドレスを通知

環境

  • MyDNS.JP
  • Raspberry Pi

前提条件

  • 独自ドメインを取得済み
  • MyDNS.JPに登録済み

手順

$ sudo vi /etc/cron.hourly/ddns-update
※ファイル名は拡張子なし("."を含まない)で!

ddns-update
# !/bin/bash
# MyDNS.JPのID
mydnsid="mydns999999"
# MyDNS.JPのPW
mydnspw="xxxxxxxxxxx"
# 前回取得したIPアドレスが書かれたファイル
gipfile=/tmp/prev_gip.txt
# 前回IPアドレス初期化(ファイルが無い場合のIPアドレス比較エラー回避用)
prev_gip="0"

# ファイル読み込み
if [ -e ${gipfile} ]; then
    read prev_gip < ${gipfile}
    # タイムスタンプ取得&フォーマット
    gipfile_date=`date -r ${gipfile} "+%Y/%m/%d %H:%M:%S"`
    #echo "前回更新日時:${gipfile_date}"
    # タイムスタンプ+1日を算出
    chkdate=`date -d "${gipfile_date} 1 days" "+%Y/%m/%d %H:%M:%S"`
    #echo "次回更新予定:${chkdate}"
fi
# echo "前回IP:${prev_gip}"

# 現在のIPアドレス取得
gip=`curl -s inet-ip.info`
# echo "現在IP:${gip}"

# 現在日時取得&フォーマット
nowdate=`date "+%Y/%m/%d %H:%M:%S"`
# echo "現在日時    :${nowdate}"

# 実行判定
if [ ${gip} != ${prev_gip} ] || \
   [ `date -d "${nowdate}" "+%Y%m%d%H%M%S"` -ge `date -d "${chkdate}" "+%Y%m%d%H%M%S"` ]; then
    curl -s https://ipv4.mydns.jp/directip.html?MID=${mydnsid}\&PWD=${mydnspw}\&IPV4ADDR=${gip} \
         -o /dev/null
    # ファイル更新
    echo ${gip} > ${gipfile}
fi

$ sudo chmod 755 /etc/cron.hourly/ddns-update

独り言

  • これで無駄な通信をMyDNS.JP様に流さないゾ
  • 思いのほかラズパイのファンがうるさかったので24時間運用は諦めるカモ
  • /etc/cron.hourly/に置くファイルは拡張子があると動かないなんて・・・ハマったわ!
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?