LoginSignup
5
6

More than 5 years have passed since last update.

VyOSでDDNSの更新を行う

Last updated at Posted at 2015-09-02

ieserver.netさんを利用されている方で、VyOSでUpdateしたい方向けのScriptです。
VyOS 1.1.5 (helium)、VyOS 1.1.6 (helium) で動作確認済み。

VyOSはDebianのSqueezeベースで開発されているソフトウェアルータです

実装している処理と流れは、下記の3つです。

  1. ISPから付与されたGlobalIPの変更をチェック(外部サイトを利用)
    • GlobalIPを取得するサイトは2つ準備し、1箇所目がダウンしていた場合も2箇所目で取得させています
  2. 変更があったらUpdate
  3. 変更されたIPとプロバイダで付与されているFQDNをメール通知

ログは/var/log/messagesに書き込まれるようにしてあります。
需要のある方は、参考にして下さい。(という自分用のメモ)

No.3は新IPで名前解決できるようになるまで待てないような、急ぐ時対策なのでマストではないです。
(ISPがOCNな人向けの記述になっています。)

ディストリビューションが違うと勝手が違ってドハマりした。。。orz


VyOSのバージョンアップ時の考慮点

  • 基本的に全工程を再度実施します。
  • VyOS 1.1.5から1.1.6へのアップデート時は、リポジトリの追加は不要でした。(既に追加されていると表示される)
  • 楽をする場合、編集・作成するファイルをバックアップ→リストアで凌げます。
  • dbファイルは再作成が必要です。リストア時のファイルの所有者・権限に注意が必要です。

[2015.10.06 編集]
  • 動作確認済みバージョンに VyOS 1.1.6 (helium) を追加
  • VyOSのバージョのアップ時の考慮点を追記
  • aliase.dbの作成手順をStep1に追加
  • Step2のScript作成のパス(誤記)を修正

[Step.1]まずは準備

1.Debian(squeeze)のリポジトリ追加

set system package repository squeeze components 'main contrib non-free'
set system package repository squeeze distribution 'squeeze'
set system package repository squeeze url 'http://mirrors.kernel.org/debian'
set system package repository squeeze-lts components 'main contrib non-free'
set system package repository squeeze-lts distribution 'squeeze-lts'
set system package repository squeeze-lts url 'http://mirrors.kernel.org/debian'
set system package repository squeeze-backports components main
set system package repository squeeze-backports distribution squeeze-backports
set system package repository squeeze-backports url 'http://backports.debian.org/debian-backports'

2.必要モジュールのインストール

apt-get update
apt-get install postfix
apt-get install wget
apt-get install bsd-mailx
apt-get install sasl2-bin
aptitude install libsasl2-modules

3.メール関連の設定ファイルを編集

/etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587 
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
/etc/postfix/sasl_passwd
[smtp.gmail.com]:587 [Gmailメールアドレス]:[Gmailパスワード]
/etc/default/saslauthd
START=yes

4.aliases.dbを作成

newaliases

5.認証用ファイルの生成

chown root:root /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

6.サービスの再起動

/etc/init.d/postfix restart
/etc/init.d/saslauthd restart

7.cronにUpdate用Scriptを登録

crontab -e
*/5 * * * * /home/[username]/ddns_updater/ieServerDDNSupdater.sh


[Step.2]Update用Script準備

1.Script配置用パスと空データを作成

mkdir /home/[username]/ddns_updater/
touch /home/[username]/ddns_updater/ieServerDDNSupdater.sh
chmod 755 /home/[username]/ddns_updater/ieServerDDNSupdater.sh

2.Script作成

vi /home/[username]/ddns_updater/ieServerDDNSupdater.sh

/home/[username]/ddns_updater/ieServerDDNSupdater.sh
#!/bin/bash


# Destination Addresses
mail_to="宛先メールアドレス(複数の宛先の場合はコンマで区切る)"


# DDNS Information
ddns_url="https://ieserver.net/cgi-bin/dip.cgi"
user_name="?username=[ieserverで登録したユーザ名]"
passwd_str="&password=[ieserverで登録したパスワード]"
domain_name="&domain=[ieserverで取得したドメイン]"
update_action="&updatehost=Go"
update_url=$ddns_url$user_name$passwd_str$domain_name$update_action


# Get Global IP
new_global_ip1=`curl http://forts.jp/ip 2> /dev/null`
new_global_ip2=`curl http://inet-ip.info 2> /dev/null`
if [ ! -e /usr/local/bin/old_global_ip.txt ]; then
 touch /usr/local/bin/old_global_ip.txt
 old_global_ip=`cat /usr/local/bin/old_global_ip.txt`
else
 old_global_ip=`cat /usr/local/bin/old_global_ip.txt`
fi


# DDNS Update & e-mail notification
if [ -n "$new_global_ip1" ]; then
 logger "ieServerDDNSupdater.sh ---- Get IP URL1"
 isp_fqdn=$(nslookup $new_global_ip1 | grep ocn.ne.jp | awk '{print $4}')
 if [ "$old_global_ip" != "$new_global_ip1" ]; then
  wget -q -O - "$update_url" > /usr/local/bin/updatechk.txt
  stat=`cat /usr/local/bin/updatechk.txt|grep $new_global_ip1`
  if [ -n "$stat" ]; then
   logger "ieServerDDNSupdater.sh ---- Updated DDNS"
   echo "New IP = $new_global_ip1" > /usr/local/bin/mailbody.txt
   echo "ISP FQDN = $isp_fqdn" >> /usr/local/bin/mailbody.txt
   mail -s "Change IP nortification" $mail_to < /usr/local/bin/mailbody.txt
   logger "ieServerDDNSupdater.sh ---- Nortify IP Changed"
   echo $new_global_ip1 > /usr/local/bin/old_global_ip.txt
   rm /usr/local/bin/updatechk.txt
   rm /usr/local/bin/mailbody.txt
  else 
   logger "ieServerDDNSupdater.sh ---- Missed Update DDNS"
   echo "127.0.0.1" > /usr/local/bin/old_global_ip.txt
  fi
 else
  logger "ieServerDDNSupdater.sh ---- Get IP URL1 No Changed"
 fi
elif [ -n "$new_global_ip2" ]; then
 logger "ieServerDDNSupdater.sh ---- Get IP URL2"
 isp_fqdn=$(nslookup $new_global_ip2 | grep ocn.ne.jp | awk '{print $4}')
 if [ "$old_global_ip" != "$new_global_ip2" ]; then
  wget -q -O - "$update_url" > /usr/local/bin/updatechk.txt
  stat=`cat /usr/local/bin/updatechk.txt|grep $new_global_ip2`
  if [ -n "$stat" ]; then
   echo "New IP = $new_global_ip2" > /usr/local/bin/mailbody.txt
   echo "ISP FQDN = $isp_fqdn" >> /usr/local/bin/mailbody.txt
   logger "ieServerDDNSupdater.sh ---- Updated DDNS"
   mail -s "Change IP nortification" $mail_to < /usr/local/bin/mailbody.txt
   logger "ieServerDDNSupdater.sh ---- Nortify IP Changed"
   echo $new_global_ip2 > /usr/local/bin/old_global_ip.txt
   rm /usr/local/bin/updatechk.txt
   rm /usr/local/bin/mailbody.txt
  else 
   logger "ieServerDDNSupdater.sh ---- Missed Update DDNS"
   echo "127.0.0.1" > /usr/local/bin/old_global_ip.txt
  fi
 else
  logger "ieServerDDNSupdater.sh ---- Get IP URL2 No Changed"
 fi
else
 echo "127.0.0.1" > /usr/local/bin/old_global_ip.txt
 logger "ieServerDDNSupdater.sh ---- Can't Get IP"
fi
5
6
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
5
6