如果忘記了替 Domain Name 續期的話, 被 Domain Registrar 停掉 Domain, 網站及電郵服務會暫停, 甚至失去網域的使用權。當持有網域的數量較多, 難免會忘記, 以下是介紹用 Shell Script 自動檢查網域的到期日及註冊機構, 並自動傳送續期通知都電郵。
這個 Shell Script 使用 whois 檢查網域資料, 及 mail 傳送電郵, 先安裝這兩個小工具:
RHEL / CentOS / Fedora:
# yum install jwhois
# yum install mailx
# yum install mailx
Debian / Ubuntu:
# apt-get install python-software-properties
# add-apt-repository ppa:nathan-renniewaldock/ppa
# apt-get update
# apt-get install whois
# apt-get install bsd-mailx
# add-apt-repository ppa:nathan-renniewaldock/ppa
# apt-get update
# apt-get install whois
# apt-get install bsd-mailx
whois 會回傳很多資訊, 但我們只需要 Domain 的到期日, 可以用 grep 過濾出來, 指令是這樣:
# whois yourdomain.com | grep -i Expir
知道指令後, Shell Script 是這樣寫的, 只有幾行十分簡單:
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/sh DOMAIN_LIST="mydomain.com yourdomain.com" EMAIL_BODY="" for domain in $DOMAIN_LIST do EMAIL_BODY="$EMAIL_BODY$domain - $(/usr/bin/whois $domain | grep -i Expir)\n" done echo -e $EMAIL_BODY | /bin/mail -s "Domain Expiration Date" your@email.com |
將以上 Shell Script 第 3 行的 DOMAIN_LIST 設定成要檢查的網域, 網域以空格分開, 以及最後一行改成自己的電郵, 再將 Script 放到 Crontab 自動執行即可。