-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloudflare-CLI-Domain-Search.sh
173 lines (155 loc) · 5.57 KB
/
Cloudflare-CLI-Domain-Search.sh
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
#!/bin/bash
###############################################################################
# Cloudflare-CLI #
# https://github.com/cvc90/Cloudflare-CLI/ #
# #
# CLI utility that manages Cloudflare services through the Cloudflare API #
# #
# Script file Cloudflare-CLI-Domain-Search.sh #
# A simple script to search for domains in your Cloudflare account #
# #
# ** Requires jq (https://stedolan.github.io/jq/) ** #
# #
# Usage: ./Cloudflare-CLI-Domain-Search.sh <domain> #
# Examples: ./Cloudflare-CLI-Domain-Search.sh example.com #
# ./Cloudflare-CLI-Domain-Search.sh -f domains.txt #
###############################################################################
# -- Variables
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # -- Get current script directory
CACHE_FILE="$HOME/.Cloudflare-CLI-Domain-Search.cache"
# -- Check if jq is installed
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed.' >&2
exit 1
fi
# -- usage
usage() {
USAGE=\
"Usage: ./Cloudflare-CLI-Domain-Search.sh (-f file.txt|<domain>) [-h] [-t] [-d]
Example: ./Cloudflare-CLI-Domain-Search.sh example.com or ./Cloudflare-CLI-Domain-Search.sh -f domains.txt
Options
-h, --help Display this help and exit
-f, --file File containing list of domains to search for
-t, --test Test file containing list of domains to search for
-d, --debug Enable debug mode
-c, --cache Enable cache mode
"
echo "$USAGE"
}
# -- Debug
_debug () {
[[ $DEBUG ]] && echo "DEBUG: $1"
}
_success () { echo -e "\033[0;42m${*} \e[0m"; }
_fail () { echo -e "\e[41m\e[97m${*} \e[0m"; }
# -- All args
ALL_ARGS="${*}"
# -- Process arguments
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
HELP=YES
shift # past argument
;;
-t|--test)
TEST="1"
shift # past argument
;;
-f|--file)
DOMAIN_FILE="$2"
shift # past argument
shift # past value
;;
-c|--cache)
CACHE=1
shift # past argument
;;
-d|--debug)
DEBUG="1"
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
# -- All args
_debug "ALL_ARGS: $ALL_ARGS"
_debug "\$1: $1"
_debug "TEST: $TEST"
_debug "DOMAIN_FILE: $DOMAIN_FILE"
_debug "DEBUG: $DEBUG"
[[ -n $1 ]] && DOMAIN="$1"
# -- Check if argument is passed
if [[ -z "$DOMAIN" && -z $DOMAIN_FILE ]]; then
usage
echo "Error: No domain specified"
exit 1
elif [[ $HELP ]]; then
usage
exit 1
fi
# --------------------
# -- Main
# --------------------
echo "Getting list of domains using ./cloudflare"
if [[ $TEST ]]; then
CF_DOMAINS=$(cat $SCRIPT_DIR/tests/domains.txt)
_debug "CF_DOMAINS: $CF_DOMAINS"
else
if [[ $CACHE ]]; then
if [[ -f $CACHE_FILE ]]; then
# -- Check if cache file is older than an hour
if [[ $(find $CACHE_FILE -mmin +60) ]]; then
echo "-- Cache file is older than an hour, deleting cache file and pulling in fresh cache"
rm $CACHE_FILE
CF_DOMAINS=$($SCRIPT_DIR/cloudflare list zones | awk '{print $1}')
echo "-- Caching domains to $HOME/.Cloudflare-CLI-Domain-Search.cache"
echo "$CF_DOMAINS" > $HOME/.Cloudflare-CLI-Domain-Search.cache
else
echo "-- Cache file is not older than an hour, using cache file"
CF_DOMAINS=$(cat $CACHE_FILE)
fi
else
echo "-- Cache file does not exist, pulling in fresh cache"
CF_DOMAINS=$($SCRIPT_DIR/cloudflare list zones | awk '{print $1}')
echo "-- Caching domains to $HOME/.Cloudflare-CLI-Domain-Search.cache"
echo "$CF_DOMAINS" > $HOME/.Cloudflare-CLI-Domain-Search.cache
fi
else
echo "-- Grabbing domains from Cloudflare API"
CF_DOMAINS=$($SCRIPT_DIR/cloudflare list zones | awk '{print $1}')
fi
fi
echo ""
if [[ $DOMAIN_FILE ]]; then
if [[ -f $DOMAIN_FILE ]]; then
# -- Search for matching domains from $FILE in Cloudflare account
echo "Search for matching domains from the file $DOMAIN_FILE in Cloudflare account"
for domain in $(cat $DOMAIN_FILE); do
SEARCH=$(grep '^'$domain'$' <<< "${CF_DOMAINS[@]}")
if [[ $? == 0 ]]; then
_success "$domain - FOUND"
else
_fail "$domain - NOTFOUND"
fi
done
else
echo "Error: $DOMAIN_FILE does not exist"
exit 1
fi
else
# -- Search for matching domain from stdin in Cloudflare account
echo "Search for matching domain $DOMAIN in Cloudflare account"
SEARCH=$(echo "${CF_DOMAINS[@]}" | grep "$DOMAIN")
if [[ $? == 0 ]]; then
_success "$DOMAIN - FOUND"
else
_fail "$DOMAIN - NOTFOUND"
fi
fi