If you want a site, you first need to purchase a domain name, not so much to buy as it is rented, because you want to use the domain name, need to pay a fee every year. Since it is rented, so there is time limit, no renewals prior to maturity, the domain name will be withdrawn, others can register and use the domain name.
When a lot of our time domain name, it is easy to forget these domain renewal leading to expire and recycling.
Demand in this case is whether to write a shell script to monitor the specified domain name expires, specific requirements are as follows:
1) Write a function, domain name passed as a parameter to this function
2) one week (two weeks) before and after the domain name expires one week and maturity, should send alarm messages per day
3) implementation of the script once a day
A knowledge point: whois
A domain name information, such as owner-mail, phone, address and when expirations are open, you can access https://www.whois.net query in the browser. There are also a lot of similar sites can query domain name information. How to check the Linux command line it?
# whois aminglinux.com Domain Name: AMINGLINUX.COM Registry Domain ID: 1800256822_DOMAIN_COM-VRSN Registrar WHOIS Server: whois.55hl.com Registrar URL: http://www.55hl.com Updated Date: 2018-05-04T22:57:37Z Creation Date: 2013-05-10T06:02:05Z Registry Expiry Date: 2021-05-10T06:02:05Z Registrar: Jiangsu Bangning Science & technology Co. Ltd. Registrar IANA ID: 1469 Registrar Abuse Contact Email: [email protected] Registrar Abuse Contact Phone: +86 025 86883426 1009 Domain Status: ok https://icann.org/epp#ok Name Server: F1G1NS1.DNSPOD.NET Name Server: F1G1NS2.DNSPOD.NET DNSSEC: unsigned URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/ >>> Last update of whois database: 2019-07-25T12:40:54Z <<<
The default system does not have this command, you need to install whois package.
This case requires monitoring time expired domain name, so the line to pay attention to is the Registry Expiry Date. Another problem had to consider different domains (.com, .cn) to query the results are different, such as .cn result is this:
WHOIS aminglinux.cn # Domain Name: aminglinux.cn ROID: 20160322s10001s82727381-CN Domain Status: the ok Registrant ID: mr2272c32qw63z Registrant: personal username Registrant Business Card Email: [email protected] Sponsoring Registrar: Beijing new network Digital Information Technology Co., Ltd. Name Server: ns11.xincache.com the Name Server: ns12.xincache.com Registration Time: 2016-03-22 17:42:01 Expiration Time: 2020-03-22 17:42:01 DNSSEC: unsigned
So .cn expiration time line is a concern Expiration Time.
Knowledge point two: cut command
Syntax: cut -d 'separator character' [-cf] n n is the number
-d: after a specified separator character, the delimiter character to use single quotes
-c: Specifies the first few characters behind
-f: Specifies the first few blocks behind
# cat /etc/passwd |cut -d ':' -f 1 |head -n5 root bin daemon adm lp
Description: Specifies the colon as the delimiter character behind -d, -f 1 represents a first segment taken, and the spaces between 1 -f optional.
# head -n2 /etc/passwd |cut -c2 o i # head -n2 /etc/passwd |cut -c1 r b # head -n2 /etc/passwd |cut -c1-10 root:x:0:0 bin:x:1:1: # head -n2 /etc/passwd |cut -c5-10 :x:0:0 x:1:1:
Description: -c later may be a digital n, may be a section n1-n2, may also be a plurality of digital n1, n2, n3.
# head -n2 /etc/passwd |cut -c1,3,10 ro0 bn:
Knowledge Point three: Process Control
When you run a process, you can press ctrl + z it to a halt, and then use the fg command to restore it, use the command bg to make it run in the background, you can also stop the process press ctrl + c. Enter the jobs command, you can see pause or tasks running in the background.
If you want to pause the task run up lost in the background, use the bg command.
# Bg [1] + we test1.txt & [1] + Stopped we test1.txt
But vi does not support running in the background, the other for a command:
SAR. 1 #> /tmp/1.log ^ // here the Z + Z press Ctrl [2] + SAR. 1 the Stopped> /tmp/1.log # Jobs [. 1] - the Stopped VI test.txt [2] + the Stopped . 1 SAR> /tmp/1.log # BG 2 [2] + SAR. 1> & /tmp/1.log
Description: Multiple tasks have been suspended number, use the jobs command to see two tasks, use bg or fg when you need to add a number to the end. Bg 2 used in the example bring a second suspended tasks thrown back run up, the command line using the command plus an ampersand in the final surface, these commands can be directly thrown into the background.
How to throw the background tasks shut down?
If you do not quit just shell, then, first use "fg number" the task to the foreground, and then use the "ctrl + c" End Task:
FG 2 # SAR. 1> /tmp/1.log ^ C // used herein ctrl + c
Another situation is, shut off the current shell, open again when a new shell, use the jobs command does not appear in the background or suspended tasks, in order to stop it, then you need to know its pid, then use the kill command to kill the process.
# sar 1 10 > /tmp/1.log & [1] 30218 # ps aux |grep sar root 30218 0.0 0.0 108036 760 pts/0 S 11:22 0:00 sar 1 10 root 30221 0.0 0.0 112724 984 pts/0 S+ 11:22 0:00 grep --color=auto sar
In shell scripts, execute multiple instructions is a sequential, that is only the first of instructions are executed (regardless of success or not), the following instruction will be executed. If there is an instruction to run relatively long time, following instructions will hinder execution. If you want this slow execution of instructions affect the following instruction, when the instruction is executed, plus a behind &, throw it back to. Use the ampersand task thrown into the background, it displays information pid, pid if you forget this, we can also use the command ps aux find that process. I want to close out the process, use the kill command:
The kill 9433 # [. 1] + has been terminated sar 1> /tmp/1.log
The kill command syntax is very simple, directly behind the increase in pid can be.
Knowledge Point four: to determine whether a variable is empty
In shell scripts, if a variable is not successful assignment was quoted, it will affect the normal execution of the script, to determine whether the value of a variable as it takes two approaches:
1) -z (zero meaning empty)
# b= //给变量b赋值为空 # if [ -z "$b" ]; then echo "The value of b is null.";else echo "The value of b is $b";fi The value of b is null. # b=1 # if [ -z "$b" ]; then echo "The value of b is null.";else echo "The value of b is $b.";fi The value of b is 1.
2)用-n(not null的意思,不为空)
# a=1 # if [ -n "$a" ]; then echo "The value of a is $a.";else echo "The value of a is null.";fi The value of a is 1. # a= # if [ -n "$a" ]; then echo "The value of a is $a.";else echo "The value of a is null.";fi The value of a is null.
知识点五:判断某个进程是否存在
之前有用到过ps查看进程,但是需要结合grep,而且需要统计行数,其实还有一个更简单的用法:
# sleep 100 & [1] 32744 # pgrep sleep 32744
知识点六:杀死进程
前面有用到kill命令杀死进程,但是需要知道进程的pid。再介绍一个简单的用法:
# sar 1 > /tmp/1.log & # killall sar [1]+ 已终止 sar 1 > /tmp/sar.log
说明:killall和kill不同的地方在于killall可以直接跟进程名。killall也支持-9选项,有时候用killall杀死进程不好用,需要带上-9,但是进程杀不死时,要慎用-9。
本案例参考脚本
#!/bin/bash #检测域名是否过期 #作者: #日期: #版本:v0.2 [email protected] #当前日期时间戳,用于和域名的到期时间做比较 t1=`date +%s` #检测whois命令是否存在,不存在则安装whois包 is_install_whois() { which whois >/dev/null 2>/dev/null if [ $? -ne 0 ] then yum install -y whois fi } notify() { e_d=`whois $1 |grep 'Expiry Date' |awk '{print $4}' |cut -d 'T' -f 1` #上面的$1代表域名,遍历循环出来的。 #如果e_d的值为空,则过滤关键词'Expiration Time' if [ -z "$e_d" ] then e_d=`whois $1|grep 'Expiration Time' |awk '{print $3}'` fi #将域名过期的日期转化为时间戳 e_t=`date -d "$e_d" +%s` #计算一周一共有多少秒 n=`echo "86400*7" |bc` e_t1=$[$e_t-$n] #过期时间一周前的时间戳 e_t2=$[$e_t+$n] #过期时间一周后的时间戳 if [ $t1 -ge $e_t1 ] && [ $t1 -lt $e_t ] then python mail.py $mail_u "Domain $1 will to be expired." "Domain $1 expire date is $e_d." fi if [ $t1 -ge $e_t ] && [ $t1 -lt $e_t2 ] then python mail.py $mail_u "Domain $1 has been expired." "Domain $1 expire date is $e_d." fi } #检测上次运行的whois查询进程是否存在 #若存在,需要杀死进程,以免影响本次脚本执行 if pgrep whois &>/dev/null then killall -9 whois fi is_install_whois for d in aaa.net aaa.com bbb.com aaa.cn ccc.com do notify $d & done