-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomains
executable file
·276 lines (240 loc) · 4.9 KB
/
domains
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
set -e
# version
domains_version="0.0.1"
# domains file
domains_file="$HOME/.domains"
# terminal color
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Make sure the domains file exists, create it if it doesn't
if [ ! -e $domains_file ]
then
touch $domains_file
fi
is_domain() {
local re_domain="(?=^.{4,253}$)(^(?:[a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]){0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$)"
echo "$1" | grep -qsP "$re_domain"
}
is_install() {
command -v "$1" 2>&1 >/dev/null
}
check_via_curl() {
local domain="$1"
local suffix=${domain:$(expr index "$domain" '.')}
local registry=$(echo $suffix | curl telnet://whois.iana.org:43 -s | grep whois | tr -d ' ' | cut -d':' -f2)
local expiry=$(echo $domain | curl telnet://$registry:43 -s | grep -i expiry)
if [ $? -ne 0 ]
then
echo "Error: check_via_curl"
exit 1
fi
expiry=${expiry:$(expr index "$expiry" ':')}
echo "$domain|$expiry"
}
check_via_whois() {
local domain="$1"
local expiry=$(whois $domain | grep -i expiry)
if [ $? -ne 0 ]
then
wohis $domain
exit 1
fi
expiry=${expiry:$(expr index "$expiry" ':')}
echo "$domain|$expiry"
}
whois_domain() {
local domain="$1"
if is_install whois
then
check_via_whois $domain
elif is_install curl
then
check_via_curl $domain
else
echo "Neither curl or whois is installed. Need one of them to check domain's infomation"
return 1
fi
}
format_domain() {
local line="$*"
local domain="$(echo $line | cut -d'|' -f1)"
local expiry="$(echo $line | cut -d'|' -f2)"
local month_diff=$(( `date +%s -d'next month'` - `date +%s` ))
local diff=$(( `date +%s -d"$expiry"` - `date +%s` ))
local diff_in_day=$(( $diff / $((60*60*24)) ))
# one month may not be 30 days
if [ $diff -gt $month_diff ]
then
# greater then a month
printf "$domain\t$diff_in_day\t$expiry\n"
elif [ $diff -gt 0 ]
then
# less then a month but do not expired
printf "${YELLOW}$domain\t$diff_in_day\t$expiry${NC}\n"
else
# Already expired
printf "${RED}$domain\t$diff_in_day\t$expiry${NC}\n"
fi
}
add_domain() {
if [ -z "$*" ]
then
usage
return 1
else
local domain="$1"
if ! is_domain $domain
then
echo "$domain not a vaild domain"
else
local record=$(grep -s $domain $domains_file)
if [ -z "$record" ]
then
echo "Checking $domain..."
record=$(whois_domain $domain)
if [ $? -ne 0 ]
then
return 1
fi
echo "$record" >> $domains_file
fi
format_domain "$record"
fi
# process next argument
shift
if [ $# -eq 0 ]
then
# no argument less
return 0
else
add_domain $*
fi
fi
}
remove_domain() {
local domain="$1"
sed -i "/$domain/d" $domains_file
}
show_domain() {
local domain="$1"
if [ -n "$domain" ]
then
local record=$(grep "$domain" $domains_file)
if [ $? -ne 0 ]
then
echo "Domain $domain not found"
return 1
else
format_domain "$record"
fi
else
while read -u 10 line
do
format_domain "$line"
done 10< <(cat $domains_file | sort -t '|' -k2)
fi
}
update_domain() {
local domain="$1"
if [ -z "$domain" ]
then
while read -u 10 line
do
domain=$(echo $line | cut -d'|' -f1)
expiry=$(echo $line | cut -d'|' -f2)
diff=$(( `date +%s -d"$expiry"` - `date +%s` ))
if [ $diff -le 0 ]
then
#echo "Updating $domain..."
## domain expired, update
#record=$(whois_domain $domain)
#if [ $? -ne 0 ]
#then
# return 1
#fi
## first delete domain
#sed -i "/$domain/d" $domains_file
## then insert domain
#echo $record >> $domains_file
#format_domain "$record"
update_domain $domain
fi
done 10< <(cat $domains_file | sort -t '|' -k2)
else
local record=$(grep "$domain" $domains_file)
if [ $? -ne 0 ]
then
echo "Domain $domain not found"
return 1
else
echo "Updating $domain..."
record=$(whois_domain $domain)
if [ $? -ne 0 ]
then
return 1
fi
# first delete domain
sed -i "/$domain/d" $domains_file
# then insert domain
echo $record >> $domains_file
format_domain "$record"
fi
fi
}
usage() {
local name=$(basename $0)
cat <<EOF
$name is a command line domain management tool.
Usage:
$name add [domain] Add domain
$name rm [domain] Remove domain
$name show <domain> Show domain or list all domains if no domian provided
$name update <domain> Force update domain or update expired domains if no domain provided
Your domains file is $domains_file.
EOF
}
version() {
echo $domains_version
}
main() {
local ret=0
local cmd=""
if [ -z "$1" ]
then
printf "No command specified\n\n"
usage
exit 1
fi
case "$1" in
"add")
cmd="add_domain"
;;
"rm")
cmd="remove_domain"
;;
"show")
cmd="show_domain"
;;
"update")
cmd="update_domain"
;;
--help | -help | -h)
cmd="usage"
;;
--version | -version | -v)
cmd="version"
;;
*)
printf "$1 is not a recognized command.\n\n"
cmd="usage"
ret=1
;;
esac
shift
$cmd "$@"
ret=$[$ret+$?]
exit $ret
}
main "$@"