Skip to content

Instantly share code, notes, and snippets.

@int2xx9
Last active May 25, 2017 14:40
Show Gist options
  • Save int2xx9/6304105 to your computer and use it in GitHub Desktop.
Save int2xx9/6304105 to your computer and use it in GitHub Desktop.
#!/bin/bash
#################################################
# ieserver.net 更新スクリプト
# https://gist.github.com/int2xx9/6304105
# 必要なコマンド: bash, curl, grep, sed, expr
# 自動更新させる場合cronも必要です
#################################################
#################################################
# Copyright (c) 2013 int512(@int512)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#################################################
#################################################
# 初期設定
# 前回取得したグローバルIPアドレスを保存するファイル
previpfile="previousip"
# 動作ログを記録するファイル
# 必要ない場合空にする
logfile="ieserver.log"
# ユーザ名とパスワードの配列
# 例
# * ドメイン名:example.dip.jp パスワード:password
# * ドメイン名:example.fam.cx パスワード:pass1234
# の2つのドメインを更新したい場合以下のように設定
# username=(example.dip.jp example.fam.cx)
# password=(password pass1234)
username=()
password=()
#################################################
#################################################
# 関数
# グローバルIPアドレスの取得
get_global_ipaddr() {
#curl http://checkip.dyndns.org/ 2> /dev/null | grep -oP "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
curl http://ieserver.net/ipcheck.shtml 2> /dev/null
}
# FQDNからユーザ名を取り出し
split_username_username() {
echo $1 | sed 's/^\(.[^\.]\+\)\.\(.\+\)$/\1/'
}
# FQDNからドメイン名を取り出し
split_username_domain() {
echo $1 | sed 's/^\(.[^\.]\+\)\.\(.\+\)$/\2/'
}
do_btn_str=`echo -e "\\x8e\\xc0\\x8d\\x73"`
# IPアドレスを登録 (ドメインの有効化)
register_ipaddr() {
username=`split_username_username $1`
domain=`split_username_domain $1`
password=$2
response=$(curl -X POST \
-d username=$username -d domain=$domain -d password=$password \
-d updatehost="$do_btn_str" \
https://ieserver.net/cgi-bin/dip.cgi \
2> /dev/null)
[ "`echo $response | grep Error`" != "" ] && return 1
return 0
}
# IPアドレスの登録解除 (ドメインの無効化)
unregister_ipaddr() {
username=`split_username_username $1`
domain=`split_username_domain $1`
password=$2
response=$(curl -X POST \
-d username=$username -d domain=$domain -d password=$password \
-d offline="$do_btn_str" \
https://ieserver.net/cgi-bin/dip.cgi \
2> /dev/null)
[ "`echo $response | grep Error`" != "" ] && return 1
return 0
}
# 現在設定されているIPアドレスの取得
current_ipaddr() {
username=`split_username_username $1`
domain=`split_username_domain $1`
password=$2
response=$(curl -X POST \
-d username=$username -d domain=$domain -d password=$password \
-d login=`echo -e "\\x83\\x8d\\x83\\x4f\\x83\\x43\\x83\\x93"` \
https://ieserver.net/cgi-bin/dip.cgi \
2> /dev/null)
echo $response | grep -oP '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | sed -n "1p"
[ "`echo $response | grep Error`" != "" ] && return 1
return 0
}
# ログに書き込み
log_write() {
text=$1
if [ "$logfile" != "" ]; then
echo `date "+%Y-%m-%d %H:%M:%S"`: $text >> "$logfile"
fi
}
#################################################
#################################################
# メイン処理
log_write "Launched (pid:$$)."
# 初期設定がおかしくないか少しだけチェック
if [ ${#username[*]} -ne ${#password[*]} ]; then
log_write "\$username length and \$password length are different."
log_write "exit (pid:$$)."
exit
fi
# グローバルIPアドレスを取得
global_ipaddr=`get_global_ipaddr`
# IPアドレスが前回から変更されているか確認
ipaddr_changed=0 # 0:変更されている 1:変更されていない
if [ -f "$previpfile" ]; then
if [ "`cat "$previpfile"`" = "$global_ipaddr" ]; then
# 前回とIPアドレスが同じ
ipaddr_changed=1
fi
fi
if [ $ipaddr_changed -eq 0 ]; then
[ -f "$previpfile" ] && log_write "IP Address has been changed (`cat "$previpfile"` -> $global_ipaddr)."
# IPアドレスが変更されているので更新
i=0
while [ $i -lt ${#username[*]} ]; do
register_ipaddr ${username[$i]} ${password[$i]}
if [ $? -eq 0 ]; then
log_write "Succeeded updating ${username[$i]}."
else
log_write "Failed updating ${username[$i]}."
fi
i=`expr $i + 1`
done
# 新しいIPアドレスを記録しておく
echo $global_ipaddr > "$previpfile"
else
log_write "IP Address is not changed. Nothing to do."
fi
log_write "exit (pid:$$)."
#################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment