1
2

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 5 years have passed since last update.

Digコマンドを打つためのシェルスクリプト

1
Posted at

自分のネットワークに障害があったとき、大体一番か二番目くらいにDNSが正常かどうかを確認しますよね。

Digって戻り値でエラーが掴みづらいので、
AnserSectionが0だったらエラーとするようにしました。

# !/bin/bash
# set -x

HOME_DIR="${PWD}"

# ssh check関数

dig_check () {
    anschk=$( dig $1 @$2 | grep ANSWER | cut -d"," -f2 | cut -d":" -f2; )
    if [ $anschk = 0 ]; then
      res=`echo ${res} | sed "s/^M/ /g" `
      echo "NG : dig $1 @$2 にてANSWERがありません"
    else
      res=$( dig $1 @$2 | grep Query | cut -d":" -f2; )
      status_code=$?
      #dig ameba.jp @$1 +short

      if [ $status_code = 0 ]; then
        res=`echo ${res} | sed "s/^M/ /g" `
        echo -n "OK : dig($2) : $1 : ${res}"
      else
        echo "NG : dig command : $2"
      fi
    fi

}

echo "----DNS決め打ちでDigします----"
# dnsサーバのIPアドレスをここに記載
dns_list=(
    192.168.1.254
    8.8.8.8
)
echo '```'
for cache_sv in ${dns_list[@]}; do
echo "`dig_check "yahoo.co.jp" "${cache_sv}" `"
echo "`dig_check "google.com" "${cache_sv}" `"
done

echo '```'
exit

出力ログ

----DNS決め打ちでDigします----

OK : dig(192.168.1.254) : yahoo.co.jp : 3 msec
OK : dig(192.168.1.254) : google.com : 0 msec
OK : dig(8.8.8.8) : yahoo.co.jp : 0 msec
OK : dig(8.8.8.8) : google.com : 0 msec

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?