Case 10. Detecting Domain Name Expiration

If you want to be a website, you first need to purchase a domain name, not buy it, but rent it, because you will have to pay a fee each year if you want to use the domain name.Since it is a lease, there is a time limit. If there is no renewal before the expiration date, the domain name will be withdrawn and others can register and use it.

When we have a lot of domain names under our names, it is easy to forget to renew them and they will expire and be recycled.

The requirement of this case is to write a shell script to monitor whether the specified domain name expires, as follows:

1) Write a function to which the domain name is passed as a parameter

2) One week before the expiration of the domain name and one week after the expiration (two weeks), alert mail is sent every day

3) Script is executed once a day


Point One: whois

Information about a domain name, such as the owner's mailbox, phone, address, and when it expires, is public and can be queried in a browser at https://www.whois.net.There are many similar websites in China that can query domain name information.How do I query from the Linux command line?

# 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: abuse@55hl.com
   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 <<<

This command is not available by default and the whois package needs to be installed.

This case needs to monitor the expiration time of the domain name, so the line of interest is Registry Expiry Date.Another problem to consider is that different domains (.com,.cn) query different results, such as.cn:

# whois aminglinux.cn
Domain Name: aminglinux.cn
ROID: 20160322s10001s82727381-cn
Domain Status: ok
Registrant ID: mr2272c32qw63z
Registrant: Personal User Name
Registrant Contact Email: 306798658@qq.com
Sponsoring Registrar: Beijing New Network Digital Information Technology Co., Ltd.
Name Server: ns11.xincache.com
Name Server: ns12.xincache.com
Registration Time: 2016-03-22 17:42:01
Expiration Time: 2020-03-22 17:42:01
DNSSEC: unsigned

So Expiration Time is the row of interest for.cn's expiration time.


Point 2: cut command

Syntax: cut-d'separator'[-cf] n This n is a number

-d: specify the separator character after, enclosed in single quotation marks

-c: specify the character after

-f: which block to specify later

# cat /etc/passwd |cut -d ':' -f 1 |head -n5
root
bin
daemon
adm
lp

Description: -d followed by a colon as a separator character, -f 1 means intercept the first paragraph, -f and spaces between 1 may or may not be present.

# 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:

Explanation: -c can be followed by a number n, an interval n1-n2, or multiple numbers n1, n2, n3.

# head -n2 /etc/passwd |cut -c1,3,10
ro0
bn:


Knowledge Point Three: Process Control

When a process is running, you can either pause it by pressing ctrl+z, restore it by using the fg command, run it in the background by using the bg command, or terminate the process by pressing ctrl+c.Enter the jobs command to see tasks paused or running in the background.

If you want to leave the suspended task running in the background, use the bg command.

# bg 
[1]+ vi test1.txt &
[1]+ Stopped        vi test1.txt

But vi doesn't support running in the background, so change to another command:

# sar 1 > /tmp/1.log
^Z    //Press ctrl+z here
[2]+  Stopped            sar 1 > /tmp/1.log
# jobs
[1]-  Stopped            vi test.txt
[2]+  Stopped            sar 1 > /tmp/1.log
# bg 2
[2]+  sar 1 > /tmp/1.log &

Description: Multiple suspended tasks are numbered. You can see two tasks using the jobs command. When using bg or fg, you need to add a number after them.In the example above, the second suspended task is thrown into the background by using bg 2. You can throw this command directly into the background by adding an ampersand at the end of the command line.

How do tasks dropped behind the scenes close?

If you do not exit the shell, first use the "fg number" to move the task to the foreground, then use "ctrl+c" to end the task:

# fg 2
sar 1 > /tmp/1.log
^C   //ctrl+c is used here

Another scenario is that when you close the current shell and open a new one again, the job command does not show tasks running in the background or being paused. To stop it, you need to know its pid first, then kill the process with the kill command.

# 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, multiple instructions are executed sequentially, that is, only the previous instructions are executed (whether successful or not) before the latter.If an instruction runs for a long time, it will prevent subsequent instructions from executing.If you don't want this slow-executing instruction to affect subsequent instructions, when executing the instruction, add an amp ersand to the back and drop it behind the scenes.Dropping a task in the background using the &symbol displays PID information, and if you forget the pid, you can use the ps aux command to find the process.To kill the process, you need to use the kill command:

# kill 9433
[1]+ Terminated     sar 1 > /tmp/1.log

The kill Command Syntax is simple, just add a pid after it.


Point 4: Determine if a variable is empty

In shell scripts, if a variable is referenced without a successful assignment, it will affect the normal execution of the script. There are two ways to determine if a variable's value is empty:

1) Use -z (zero means empty)

#B= //assign null to variable 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) Use -n (not null means not empty)

# 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.


Point 5: Determine if a process exists

Previously there was a ps view process, but it needed to be combined with grep, and it needed to count the number of rows. There is actually a simpler use:

# sleep 100 &
[1]  32744
# pgrep sleep
32744


Point 6: Kill the Process

The Kill Command was previously used to kill a process, but you need to know the pid of the process.Here's another simple use:

# sar 1 > /tmp/1.log &
# killall sar
[1]+  Terminated     sar 1 > /tmp/sar.log

Description: The difference between killall and killall is that killall can follow the process name directly.Kill all also supports the -9 option. Sometimes killall is not useful to kill a process. You need to bring -9 with you, but when a process cannot kill, use -9 with caution.


This case reference script

#!/bin/bash
#Detect domain name expiration
#Author:
#Date:
#Version: v0.2

mail_u=admin@admin.com
#Current date time stamp, used to compare with domain name expiration time
t1=`date +%s`

#Install the whois package if the whois command exists or does not exist
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`
    #The $1 above represents the domain name, iterating through it.
    #If the value of e_d is empty, filter the keyword'Expiration Time'
    if [ -z "$e_d" ]
    then
        e_d=`whois $1|grep 'Expiration Time' |awk '{print $3}'`
    fi
    #Convert domain name expiration date to timestamp
    e_t=`date -d "$e_d" +%s`
    #Calculate the total number of seconds a week
    n=`echo "86400*7" |bc`
    e_t1=$[$e_t-$n] #Time stamp one week before expiration
    e_t2=$[$e_t+$n] #Time stamp one week after expiration
    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    
}

#Detect the existence of the last running whois query process
#If present, the process needs to be killed to avoid affecting the execution of this script
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

Keywords: Linux shell Python Database

Added by WRa on Thu, 08 Aug 2019 07:14:27 +0300