-
Notifications
You must be signed in to change notification settings - Fork 0
/
tnlooker.sh
executable file
·339 lines (298 loc) · 10.6 KB
/
tnlooker.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
# This script will accept one or more TN's and pull information about it.
function help()
{
echo -e "
TNLooker - Telephone Number Lookup Tool
Desc: This tool will lookup various information, given one or more telephone numbers.
Usage: tnlooker [OPTION...] ARGUEMENT...
Examples:
tnlooker 14158586270
tnlooker 14158586270 14158586271 +1415-858-6271
tnlooker -n 14158586270 14158586271 +1415-858-6271
tnlooker -vnli 14158586270 14158586271 +1415-858-6271
tnlooker -F TN_FILE.txt
Options:
ENV
-A - access_key Specifies the access_key to be used when making API calls.
-E - endpoint Specifies the endpoint the API Calls are made to.
-D - DF_FILE Specifies the mysql client configuration file.
INPUT
-F - TN_FILE Input a file containing telephone numbers seperated by whitespace.
OUTPUT
-v - valid Display whether the number is a valid telephone number.
-n - number Display the number in 11 digit format.
-l - local_format Display the number in local format (without the country prefix)
-i - int_format Display the number in
-p - country_prefix Display the country prefix number
-o - country_code Display the 2 letter country code
-a - country_name Display the entire country name
-u - location Display the town/city/state of the number.
-c - carrier Display the carrier name.
-t - line_type Display the line type (mobile, landline, or VOIP)
-h - help Display this help message
"
}
##################################################################################
# GET ENV VARIABLES
##################################################################################
CFG_FILE=./config/tnlooker.cfg
function makeConfig()
# Create the default config file if it does not exist.
{
if [[ ! -f $CFG_FILE ]]
then
echo "baseURL='http://apilayer.net'" > $CFG_FILE
echo "endpoint='/api/validate'" >> $CFG_FILE
echo "access_key=''" >> $CFG_FILE
echo "DB_FILE='config/mysql_connect.cfg'" >> $CFG_FILE
fi
}
makeConfig
function updateConfig()
# Update the config file. Accepts 2 arguments: [Parameter] [value].
{
param=$1
value=$2
sed -i "s/$param='.*'/$param='$value'/g" $CFG_FILE
if [[ $? -ne 0 ]]
then
echo "There was an issue updating the config file. Check your params/values and CFG_File."
else
. $CFG_FILE
fi
}
# Source the current config.
. $CFG_FILE
##################################################################################
# GET -OPTIONS
##################################################################################
# If no arguments display help.
if [[ -z "$@" ]]
then
help; exit
fi
# Create an array to store the output options.
declare -a options=()
while getopts "hvnlipoauctA:E:D:F:" option
do
case "${option}" in
v) options=(${options[@]} 'valid');;
n) options=(${options[@]} 'number');;
l) options=(${options[@]} 'local_format');;
i) options=(${options[@]} 'int_format');;
p) options=(${options[@]} 'country_prefix');;
o) options=(${options[@]} 'country_code');;
a) options=(${options[@]} 'country_name');;
u) options=(${options[@]} 'location');;
c) options=(${options[@]} 'carrier');;
t) options=(${options[@]} 'line_type');;
A) access_key=${OPTARG}; updateConfig access_key $access_key;;
E) endpoint=${OPTARG}; updateConfig endpoint $endpoint;;
D) DB_FILE=${OPTARG}; updateConfig DB_FILE $DB_FILE;;
F) TN_FILE=${OPTARG};;
h) help; exit;;
\?) help; exit;;
esac
done
columns=$(echo -e "${options[@]}" | sed 's/ /,/g')
shift $(($OPTIND -1))
# Set columns to all by default.
if [[ -z "$columns" ]]
then
columns=*
fi
# Make sure we have an access key.
if [[ -z "$access_key" ]]
then
echo -e "No Access Key Set. Run ./tnlooker.sh -A [access key]"
exit 101
fi
##################################################################################
# Create an array to store the TNs.
if [[ -n "$TN_FILE" ]]
then
declare -a tns=($(cat $TN_FILE))
else
declare -a tns=($@)
fi
# Define acceptable input formats.
numberFormat="^[1-9][0-9]{10,14}$"
localFormat="^[0-9]{10}$"
function checkDB()
# Checks the DB for records matching the current TN and stores the id. Returns 1 if no match is found.
{
local QUERY="SELECT id FROM tnlooker_db.numbers WHERE $column = ${num} $@"
id=$(echo $QUERY | mysql --defaults-extra-file=$DB_FILE -s)
if [[ $? -ne 0 ]]
then
echo -e "\nFatal MySQL Error Received. Exiting."
exit 500
fi
if [[ -z $id ]]
then
return 1
else
return 0
fi
}
function checkRecord_age()
# Checks whether the current TN's record needs to be updated (Max: 7 days). Returns 1 if an update is needed.
{
local QUERY="SELECT
CASE
WHEN TIMESTAMPDIFF(DAY,updated_on,now()) > 7 THEN 1
ELSE 0
END record_age
FROM numbers WHERE id = $id;"
local record_age=$(echo $QUERY | mysql --defaults-extra-file=$DB_FILE -s)
if [[ $record_age -ne 0 ]]
then
return 1
else
return 0
fi
}
function getData()
# Parses the json data and stores values in variables.
{
number=$(echo $json | jq -r .number)
local_format=$(echo $json | jq -r .local_format)
int_format=$(echo $json | jq -r .international_format)
country_prefix=$(echo $json | jq -r .country_prefix)
country_code=$(echo $json | jq -r .country_code)
country_name=$(echo $json | jq -r .country_name)
location=$(echo $json | jq -r .location)
carrier=$(echo $json | jq -r .carrier)
line_type=$(echo $json | jq -r .line_type)
valid=$(echo $json | jq -r .valid)
}
function insertData()
# Inserts data from the APICall into the DB.
{
local QUERY="INSERT INTO numbers
(
number,
local_format,
int_format,
country_prefix,
country_code,
country_name,
location,
carrier,
line_type,
valid
)
VALUES
(
'$number',
'$local_format',
'$int_format',
'$country_prefix',
'$country_code',
'$country_name',
'$location',
'$carrier',
'$line_type',
$valid
);"
echo $QUERY | mysql --defaults-extra-file=$DB_FILE -s
}
function updateData()
# Updates an existing record in the DB with data from the API Call.
{
local QUERY="UPDATE numbers
SET
number = '$number',
local_format = '$local_format',
int_format = '$int_format',
country_prefix = '$country_prefix',
country_code = '$country_code',
country_name = '$country_name',
location = '$location',
carrier = '$carrier',
line_type = '$line_type',
valid = $valid,
updated_on = NOW()
WHERE id = $id;"
echo $QUERY | mysql --defaults-extra-file=$DB_FILE -s
}
function selectData()
# Selects the TN's information from the DB.
{
local QUERY="SELECT $columns FROM numbers WHERE id = $id;"
output=$(echo -e "${QUERY}" | mysql --defaults-extra-file=$DB_FILE -s --table)
# if [[ $record_age -ne 0 ]];
# then
# return 1
# else
# return 0
# fi
}
for ((i=0; i < ${#tns[@]}; i++))
do
num=$(echo ${tns[$i]} | tr -dc '[:digit:]|\n')
if [[ ${num} =~ $numberFormat ]]
then
echo "Checking DB for TN $num"
column='number'; checkDB
if [[ $? -ne 0 ]]
then
# echo "Record does not exist! Doing 11 digit curl!"
# json=$(cat wip/${num}.json); getData
json=$(curl -s "${baseURL}${endpoint}?access_key=${access_key}&number=${num}"); sleep 2
getData; insertData # && echo "DB Updated!"
checkDB; selectData; echo -e "$output \n"
else
# echo "Checking Record Age"
checkRecord_age
if [[ $? -ne 0 ]]
then
# echo "Record too old! Doing 11 digit curl!"
# json=$(cat wip/${num}.json); getData
json=$(curl -s "${baseURL}${endpoint}?access_key=${access_key}&number=${num}"); sleep 2
getData; updateData # && echo "DB Updated!"
checkDB; selectData; echo -e "$output \n"
else
# echo "Record exists and is not too old. Selecting info"
selectData; echo -e "$output \n"
fi
fi
elif [[ ${num} =~ $localFormat ]]
then
read -p "TN $num is only 10 digits. Enter 2 letter Country Code: " country_code
if [[ "$country_code" =~ ^[a-zA-Z]{2}$ ]]
then
echo "Checking DB for TN $num"
column='local_format'; checkDB "AND country_code = '$country_code'"
if [[ $? -ne 0 ]];
then
# echo "Record does not exist! Doing 10 digit curl!"
# json=$(cat wip/${num}.json); getData
json=$(curl -s "${baseURL}${endpoint}?access_key=${access_key}&number=${num}&country_code=${country_code}"); sleep 2
getData; insertData # && echo "DB Updated!"
checkDB "AND country_code = '$country_code'"; selectData; echo -e "$output \n"
else
# echo "Checking Record Age"
checkRecord_age
if [[ $? -ne 0 ]]
then
# echo "Record too old! Doing 10 digit curl!"
# json=$(cat wip/${num}.json); getData
json=$(curl -s "${baseURL}${endpoint}?access_key=${access_key}&number=${num}&country_code=${country_code}"); sleep 2
getData; updateData # && echo "DB Updated!"
checkDB "AND country_code = '$country_code'"; selectData; echo -e "$output \n"
else
# echo "Record exists and is not too old. Selecting info"
selectData; echo -e "$output \n"
fi
fi
else
echo "Error: Invalid Country Code Format"
continue
fi
else
echo "Error: TN ${num} is in an invalid format."
continue
fi
done