-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.sh
executable file
·264 lines (235 loc) · 6.57 KB
/
functions.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
# REQUIRED COMMAND MAPPINGS:
RM=$(which rm); FIND=$(which find);
ECHO=$(which echo); TPUT=$(which tput);
PS=$(which ps); GREP=$(which grep);
MAIL=$(which mail); SSH=$(which ssh);
WGET=$(which wget); TCPDUMP=$(which tcpdump);
CURL=$(which curl); PING=$(which ping);
# OPTIONAL COMMAND MAPPINGS:
# mainly for snippets
#RSYNC=$(which rsync); TAR=$(which tar);
#DC3DD=$(which dc3dd); DD=$(which dd);
#PV=$(which pv); TIME=$(which time);
#MYSQL=$(which mysql); YES=$(which yes);
# function debug() # echo debug information to screen and log if DEBUG is set to on
# usage: debug "the program broke"
#
function debug(){
local msg="[debug] - $1"
[ "$DEBUG" == "on" ] && $ECHO $msg
[ "$DEBUG" == "on" ] && $ECHO $msg >> $LOGFILE
return
}
# function info() # post info to screen and log if INFO is set to on
# usage: info "text to output in case INFO is on"
#
function info(){
local msg="[info] - $1"
[ "$INFO" == "on" ] && $ECHO $msg
[ "$INFO" == "on" ] && $ECHO $msg >> $LOGFILE
return
}
# function cleanup() # run a cleanup before exiting
# usage: cleanup
#
function cleanup(){
debug "Starting cleanup..."
# isset?
$RM -f $TMPFILE
debug "Finished cleanup"
exit
}
# function failed() # error handling with trap
# usage: none really
#
function failed(){
local r=$?
set +o errtrace
set +o xtrace
$ECHO "An error occurred..."
cleanup
}
# function usage() # show the usage.dat file
# usage: usage
#
function usage(){
source usage.dat
}
# function mini_usage() # show the mini_usage.dat file
# usage: mini_usage
#
function mini_usage(){
source mini_usage.dat
}
# function change_ifs() # change the default field seperator
# usage: change_ifs ":"
# NOTE: this is an environment-wide change! so be sure to undo it at the end
# of the script. I only include it because i use it often. Dont use this.
function change_ifs(){
new_ifs=$1
OLDIFS="${IFS}"
IFS=$new_ifs
return
}
# function revert_ifs() # revert the default field seperator
# usage: revert_ifs ":"
# NOTE: this is an environment-wide change! so be sure to undo it at the end
# of the script. I only include it because i use it often. Dont use this.
function revert_ifs(){
IFS=$OLDIFS
return
}
# function check_regex() # look for a regex in a string, if match return true
# usage: if $(check_regex $some_var $some_regex_pattern) then; echo "true"
#
function check_regex(){
local input=$1
local regex=$2
if [[ $input =~ $regex ]]; then
# echo "found some regex"
return true
else
# echo "did not find some regex"
return false
fi
}
# function check_reqs() # check that needed programs are installed
# usage: none really (system)
#
function check_reqs(){
for x in "${REQUIRED_PROGS[@]}"
do
type "$x" >/dev/null 2>&1 || { $ECHO "$x is required and is NOT installed. Please install $x and try again. Exiting."; exit; }
done
}
# function check_env() # set tracing if dev or test environment
# usage: none really (system)
#
function check_env(){
if [ $ENV != "prod" ] && [ $1 == "set" ]; then
set -o errtrace # fail and exit on first error
set -o xtrace # show all output to console while writing script
fi
if [ $ENV != "prod" ] && [ $1 == "unset" ]; then
set +o errtrace
set +o xtrace
fi
}
# function alert() # alert sysadmin email/pager with an email
# usage: alert "the program broke" "really bad" yes yes
#
function alert(){
local error_subject=$1
local error_message=$2
local email=$3
local pager=$4
[ "$email" == "yes" ] && $MAIL -s '$error_subject' "$SYSADMIN_EMAIL" < "$error_message";
[ "$pager" == "yes" ] && $MAIL -s '$error_subject' "$SYSADMIN_PAGER" < "$error_message";
}
# function only_run_as() # only allow script to continue if uid matches
# usage: only_run_as 0
#
function only_run_as(){
if [[ $EUID -ne $1 ]]; then
$ECHO "script must be run as uid $1" 1>&2
exit
fi
}
# function only_run_in() # check that script is run from /root/bin
# usage: only_run_in "/home/user"
#
function only_run_in(){
local cwd=`pwd`
if [ $cwd != "$1" ]; then
$ECHO "script must be run from $1 directory";
exit
fi
}
# function only_run_for() # Runs a command for a specified number of seconds
# usage: only_run_for [number of seconds] [command]
#
function only_run_for(){
local runtime=${1:-1m}
mypid=$$
shift
$@ &
local cpid=$!
sleep $runtime
kill -s SIGTERM $cpid
}
# function text() # output text ERROR or OK with color (good for cli output)
# usage: text error "there was some sort of error"
# usage: text ok "everything was ok"
#
function text(){
local color=${1}
shift
local text="${@}"
case ${color} in
error ) $ECHO -en "["; $TPUT setaf 1; $ECHO -en "ERROR"; $TPUT sgr0; $ECHO "] ${text}";;
ok ) $ECHO -en "["; $TPUT setaf 2; $ECHO -en "OK"; $TPUT sgr0; $ECHO "] ${text}";;
esac
$TPUT sgr0
}
# function mygrants() # Displays all grant imygrantsnformation
# usage: mygrants [-h -u -p]
#
function mygrants(){
$MYSQL -B -N $@ -e "SELECT DISTINCT CONCAT(
'SHOW GRANTS FOR ''', user, '''@''', host, ''';'
) AS query FROM mysql.user" | mysql $@ | sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'; }
# function myspace() # Displays disk usage of tables
# usage: myspace [ -h -u -p ]
#
function myspace(){
$MYSQL -B -N $@ -e "SELECT table_schema, count(*) TABLES,
concat(round(sum(table_rows)/1000000,2),'M')
rows,concat(round(sum(data_length)/(1024*1024*1024),2),'G')
DATA,concat(round(sum(index_length)/(1024*1024*1024),2),'G')
idx,concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G')
total_size,round(sum(index_length)/sum(data_length),2) idxfrac
FROM information_schema.TABLES group by table_schema;"; }
# function googl() #Creates a shortened URL from a longer one
# usage: googl [some url]
#
function googl(){
$CURL -s -d "url=${1}"http://goo.gl/api/url| sed -n "s/.*:\"\([^\"]*\).*/\1\n/p" ;}
# function checksu() # Checks a shortened URL's actual destination
# usage: checksu [some shortened url]
#
function checksu(){
$CURL -sI $1 | sed -n 's/Location:.* //p';}
# function getextip() # get your external ip address in text
# usage: getextip
#
function getextip(){
$WGET -qO- icanhazip.com; }
# function tcp() # dump tcp packets
# usage: tcp
#
function tcp(){
$TCPDUMP -nUs0 -w- -iinterface $1|tcpdump -n${2-A}r- ; }
# # #
# TO BE WRITTEN #
# # #
# function check_ip() # check ip, if alive do command
# usage: check_ip 192.168.1.1 ALIVE=1
#
function check_ip(){
# check if ip is alive, if so then true and do this
$PING -c 1 -w 5 $1 &>/dev/null
if [ $? -ne 0 ] ; then
debug "host $1 down. . ."
else
$2
fi
}
function wait_til_done(){
$1
wait
}
function paralell_exec(){
# fire off multiple bg jobs
$ECHO
}
# never has so little been documented so well . . .