-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_aliases
371 lines (298 loc) · 8.91 KB
/
.bash_aliases
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# ------------------------------------------------------------------------------
#
# .bash_aliases
#
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
#
# Set aliases based on the kernel.
#
# ------------------------------------------------------------------------------
KERNEL=$(uname)
if [[ "$KERNEL" = "Linux" ]]; then
# Alias tmux to use Ubuntu config file
#
alias tmux='tmux -f ~/.ubu.tmux.conf'
elif [[ "$KERNEL" = "Darwin" ]]; then
# Alias tmux to use OSX config file
#
alias tmux='tmux -f ~/.osx.tmux.conf'
if [[ -f ~/Scripts/macos-functions.sh ]]; then
. ~/Scripts/macos-functions.sh
fi
fi
# ------------------------------------------------------------------------------
#
# Variables
#
# ------------------------------------------------------------------------------
export BOLD=$(tput bold)
export NORMAL=$(tput sgr0)
export BLUE=$(tput setaf 21)
export CYAN=$(tput setaf 78)
export GREEN=$(tput setaf 40)
export ORANGE=$(tput setaf 208)
export RED=$(tput setaf 196)
# ------------------------------------------------------------------------------
#
# Aliases
#
# ------------------------------------------------------------------------------
# start tmux in 256 color mode
#
alias t2='tmux -2'
# Useful shortcuts for hsqldb.
#
alias dbmgr='java -jar $HOME/hsqldb-2.4.0/hsqldb/lib/hsqldb.jar'
alias sqltool='java -jar $HOME/hsqldb-2.4.0/hsqldb/lib/sqltool.jar'
alias sqltoolc='java -cp $HOME/hsqldb-2.4.0/hsqldb/lib/sqltool.jar:$HOME/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar org.hsqldb.cmdline.SqlTool'
alias ap8='autopep8'
alias changeu='echo "changeme-$(uuidgen)"'
# ------------------------------------------------------------------------------
#
# Functions
#
# ------------------------------------------------------------------------------
# Development function only. Call like this:
#
# color {1..255}
#
# to see all the colors the terminal can produce.
#
color() {
for c; do
printf '\e[48;5;%dm%03d' $c $c
done
printf '\e[0m \n'
}
print_msg() {
LEVEL=$1
shift
COLOR=
case "$LEVEL" in
DEBUG) COLOR=$BLUE
;;
ERROR) COLOR=$RED
;;
INFO) COLOR=$GREEN
;;
WARN) COLOR=$ORANGE
;;
*) COLOR=$NORMAL
;;
esac
FORMAT_STRING="$1"
shift
printf "$BOLD$COLOR[%-5s]:$NORMAL $FORMAT_STRING" "$LEVEL" "$@"
}
debug() {
if [[ $# -gt 0 ]]; then
print_msg "DEBUG" "$@"
fi
}
error() {
if [[ $# -gt 0 ]]; then
print_msg "ERROR" "$@"
fi
}
inform() {
if [[ $# -gt 0 ]]; then
print_msg "INFO" "$@"
fi
}
warn() {
if [[ $# -gt 0 ]]; then
print_msg "WARN" "$@"
fi
}
s2s_scp() {
if [ $# -lt 3 ]; then
cat <<-ENDOFHELP
Secure copy a file from one remote host to another using a directory
on this computer as temporary storage.
Usage: $FUNCNAME <src-server> <dst-server> <filename> [<group-name>]
<src-server> The remote host that has the file you want to
copy.
<dst-server> The remote host where you want to copy the file
to.
<filename> The name of the file (full path).
<group-name> OPTIONAL group name to set on <dst-server> for
the file.
Use this function when admins lock down the servers at your
installation to prevent scp working the way it ought to (for
security reasons).
The file will be copied to exactly the same path on <dst-server> as
it is on <src-server>.
The file is copied to a temporary directory on this machine created
by the mktemp command. The directory and file are deleted after the
file is copied to <dst-server>.
ENDOFHELP
return 1
fi
local SRC_SERVER=$1
local DST_SERVER=$2
local FILENAME=$3
local GRPNAME=$4
TEMP_DIR=$(mktemp -d ${TMPDIR}${FUNCNAME}.XXXXXX)
scp $SRC_SERVER:$FILENAME $TEMP_DIR/$(basename ${FILENAME})
scp $TEMP_DIR/$(basename ${FILENAME}) $DST_SERVER:$FILENAME
rm -rf $TEMP_DIR
ssh $DST_SERVER chmod 660 $FILENAME
if [[ ! -z $GRPNAME ]]; then
ssh $DST_SERVER chgrp $GRPNAME $FILENAME
fi
}
scp_grw() {
if [ $# -ne 3 ]; then
cat <<-ENDOFHELP
Secure copy a file to a remote host, then grant group read-
write permissions to it.
Usage: $FUNCNAME <path-to-file> <server-name> <server-path>
<path-to-file> Fully qualified filename of local file to copy.
<server-name> The remote host.
<server-path> Directory to copy the file to on the remote
host.
ENDOFHELP
return 1
fi
local FNAME=$(basename $1)
scp $1 $2:$3/$FNAME
ssh $2 chmod 660 $3/$FNAME
}
ssh_chgrp() {
if [ $# -ne 3 ]; then
cat <<-ENDOFHELP
Run chgrp on a file that exists on a remote host.
Usage: $FUNCNAME <server-name> <grp-name> <path-to-file-on-server>
<server-name> The remote host.
<grp-name> Name for the group on the remote
host.
<path-to-file-on-server> Location of the file on the remote
host.
ENDOFHELP
return 1
fi
ssh $1 chgrp $2 $3
}
headers() {
if [[ $# -lt 1 ]]; then
cat <<-ENDOFHELP
Request HTTP headers from a remote server.
Usage: $FUNCNAME <server> [<port>]
<server> The remote server.
<port> OPTIONAL port to communicate on (default is 80).
ENDOFHELP
fi
SERVER=$1
PORT=${2:-80}
# "Open a read-write file descriptor (5) to PORT on SERVER using tcp"
#
exec 5<> /dev/tcp/$SERVER/$PORT
echo -e "HEAD / HTTP/1.1\nHost: ${SERVER}\n\n" >&5
cat 0<&5
(( $? == 0 )) && exec 5>&-
}
test_port() {
if [ $# -lt 2 ]; then
cat <<-ENDOFHELP
Connect to a remote host on a given port using the given protocol.
Usage: $FUNCNAME <server-or-ip> <port> [<protocol>]
<server-or-ip> The remote host address or name.
<port> Port to communicate on.
<protocol> OPTIONAL protocol to use (default is tcp).
ENDOFHELP
return 1
fi
SERVER=$1
PORT=$2
PROTO=${3:-tcp}
# "Open a read-write file descriptor (5) to PORT on SERVER using PROTOCOL"
#
exec 5<>/dev/$PROTO/$SERVER/$PORT
(( $? == 0 )) && exec 5<&-
}
alias tp='test_port'
alias tpl='test_port localhost'
file_size() {
if [ $# -eq 0 ]; then
echo "Oh dear"
fi
FNAME=$1
printf "%d\n" $(wc -c <$FNAME)
}
alias fsz='file_size'
man_md() {
if [[ $# -eq 0 ]]; then
echo "Usage: man_md <md-filename>"
return 1
fi
$(command -v pandoc) >/dev/null 2>&1
PANDOC_INSTALLED=$(echo $?)
$(command -v lynx) >/dev/null 2>&1
LYNX_INSTALLED=$(echo $?)
if [[ $PANDOC_INSTALLED -eq 0 ]]; then
if [[ $LYNX_INSTALLED -eq 0 ]]; then
pandoc ${1} | lynx -stdin
else
echo "lynx not installed! Install lynx and try again."
return 1
fi
else
echo "pandoc not installed! Install pandoc and try again."
return 1
fi
}
# Show all the names (CNs and SANs) listed in the SSL certificate
# for a given domain
getcertnames() {
if [ -z "${1}" ]; then
echo "ERROR: No domain specified.";
return 1;
fi;
local domain="${1}";
echo "Testing ${domain}…";
echo ""; # newline
local tmp=$(echo -e "GET / HTTP/1.0\nEOT" \
| openssl s_client -connect "${domain}:443" -servername "${domain}" 2>&1);
if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then
local certText=$(echo "${tmp}" \
| openssl x509 -text -certopt "no_aux, no_header, no_issuer, no_pubkey, \
no_serial, no_sigdump, no_signame, no_validity, no_version");
echo "Common Name:";
echo ""; # newline
echo "${certText}" | grep "Subject:" | sed -e "s/^.*CN=//" | sed -e "s/\/emailAddress=.*//";
echo ""; # newline
echo "Subject Alternative Name(s):";
echo ""; # newline
echo "${certText}" | grep -A 1 "Subject Alternative Name:" \
| sed -e "2s/DNS://g" -e "s/ //g" | tr "," "\n" | tail -n +2;
return 0;
else
echo "ERROR: Certificate not found.";
return 1;
fi;
}
# ------------------------------------------------------------------------------
# Terrgrunt and Terraform
# ------------------------------------------------------------------------------
alias tg_apply='terragrunt apply'
alias tg_out='terragrunt output'
alias tg_plan='terragrunt plan'
alias tg_reset='rm -rf ~/.terragrunt'
# ------------------------------------------------------------------------------
#
# Source convenience functions by "category"
#
# ------------------------------------------------------------------------------
if [[ ! -z $(which docker) ]] && [[ -f $HOME/Scripts/docker-functions.sh ]]
then
source $HOME/Scripts/docker-functions.sh
fi
if [[ ! -z $(which docker-compose) ]] && [[ -f $HOME/Scripts/docker-compose-functions.sh ]]
then
source $HOME/Scripts/docker-compose-functions.sh
fi
if [[ ! -z $(which aws) ]] && [[ -f $HOME/Scripts/aws-functions.sh ]]
then
source $HOME/Scripts/aws-functions.sh
fi