-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases
659 lines (598 loc) · 17.7 KB
/
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/bin/bash
# User specific aliases and functions
# Script include
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# OSX does not have realpath
if ! which realpath &> /dev/null; then
function realpath {
python -c "import os; print(os.path.realpath('$1'))"
}
fi
if ! command -v timeout &> /dev/null; then
if command -v gtimeout &> /dev/null; then
alias timeout=gtimeout
fi
fi
function json_escape { printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'; }
function file-age { FILE_CREATED_TIME=`date -r "$1" +%s`; TIME_NOW=`date +%s`; echo "$[ ${TIME_NOW} - ${FILE_CREATED_TIME} ]"; }
alias mark='MARK="$(realpath .)"'
alias back='cd "$MARK"'
alias flick='FLICK="$(realpath .)";cd "$MARK";MARK="$FLICK"'
alias flickto='MARK="$(realpath .)";cd'
function cdf { cd "$(dirname "$1")"; }
# Bash aliases
alias l='ls --color'
# OSX uses CLICOLOR to show color instead of --color
l &> /dev/null
if [[ "$?" == "1" ]]; then
alias l='ls'
export CLICOLOR=1
fi
alias ll='l -lah'
alias ll-sort-size='ll -S'
alias w='cd /var/www/html'
alias wr='cd ~/Work/realtalk'
function f {
grep "$1" -R .
}
function fn {
find . -name "$@"
}
alias hig='history | grep'
function m { if type "$1" | grep -q 'is a shell builtin'; then /usr/bin/man bash | less -p "^ $1 "; else /usr/bin/man "$1"; fi }
function bak {
FILE_TO_BACKUP=`realpath "$1"`
DIR_OF_FILE=`dirname "$FILE_TO_BACKUP"`
TIMESTAMP=`date-second`
BACKUP_DIR="/tmp/$TIMESTAMP$DIR_OF_FILE"
mkdir -p "$BACKUP_DIR"
echo "Backing up '$FILE_TO_BACKUP' to '$BACKUP_DIR'"
if [ -f "$FILE_TO_BACKUP" ]; then
cp "$FILE_TO_BACKUP" "$BACKUP_DIR/"
elif [ -d "$FILE_TO_BACKUP" ]; then
cp -r "$FILE_TO_BACKUP" "$BACKUP_DIR/"
fi
}
function all2unix {
FILEPATH="$1"
OUTFILEPATH="$1.$RANDOM.tmp"
cat "$FILEPATH" | sed -E $'s/\r$//' | tr '\r' '\n' > "$OUTFILEPATH"
mv -f "$OUTFILEPATH" "FILEPATH"
}
function md { mkdir $1; cd $1; }
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
alias .......='cd ../../../../../..'
alias cd..='cd ..'
function susu { sudo su - "${@}"; if [[ $? == 123 ]]; then exit 1; fi; }
function susub {
OTHER_USER="${1-root}"
if [[ "$OTHER_USER" == "root" ]]; then
sudo bash --rcfile "$BASHRC_DIR/main"
else
rsync -a --delete "$BASHRC_DIR/" "/tmp/bashrc/"
sudo -u root sudo -u "$OTHER_USER" bash --rcfile /tmp/bashrc/main
fi
if [[ $? == 123 ]]; then
exit 1;
fi;
}
alias die='exit 123'
# MySQL aliases
alias mq="mysql -uroot -p'$RDSPASS'"
alias mqd="mysqldump -uroot -p'$RDSPASS'"
alias mq-rds="mysql -uroot -p'$RDSPASS' -h'$RDSHOST'"
alias mqd-rds="mysqldump -uroot -p'$RDSPASS' -h'$RDSHOST'"
# Django aliases
function dm-path {
cwd="$(pwd)"
while [ ! -f manage.py ]; do
if [[ "$(pwd)" == "/" ]]; then
echo Hit the root.
break;
fi
cd ..
done
if [ -f manage.py ]; then
echo $(pwd)/manage.py
else
cd "$cwd"
fi
}
alias dm='python $(dm-path)'
alias dm-runserver='dm runserver 0.0.0.0:80'
alias dm-startapp='dm startapp'
alias dm-migrate='dm migrate'
alias dm-mig='dm makemigrations; dm migrate'
# Git aliases
function is_git_repo {
(
while [[ "$PWD" != "/" ]]; do
if [[ -d ".git" ]]; then
return
fi
cd ..
done
return 1
)
}
function git-branch-name {
if is_git_repo; then
git log --pretty='%C(yellow)%h %C(auto)%d' --max-count 1 --color=always 2> /dev/null || echo '<new>'
fi
}
alias gits='git status'
alias gita='git add -A'
alias gitan='git add -An'
alias gitc='git commit -m'
alias gitch='git checkout'
function gitchb {
NEW_BRANCH="$1"
git checkout -b "$NEW_BRANCH"
git push --set-upstream origin "$NEW_BRANCH"
}
alias gitpush='git push origin HEAD'
alias gitpull='git pull origin HEAD'
alias gitgraph='git log --pretty=format:"%C(yellow)%h %C(auto)%d %C(green)%an:%Creset %s %C(cyan) (%cI, %cr)" --all --graph'
alias gitgraphthisbranch='git log --pretty=format:"%C(yellow)%h %C(auto)%d %C(green)%an:%Creset %s %C(cyan) (%cI, %cr)" --graph'
function gitac { git add -A; git commit -m "$1"; }
function gitacp { git add -A; git commit -m "$1"; git push origin HEAD; }
function gitcp { git commit -m "$1" ; git push origin HEAD; }
function bashrc-update { cd $BASHRC_DIR; gitpull; . main; }
function git-can-ffwd-from {
FROM_REF="$1"
TO_REF="${2-HEAD}"
USAGE="git-can-ffwd-from <ancestor_ref> [future_ref]"
git fetch --all
FROM_BRANCH="$(git rev-parse --symbolic-full-name --abbrev-ref $FROM_REF 2>&1)"
if [[ $? != 0 ]]; then
msg-error "Error"
msg-error "$FROM_BRANCH"
return 10
fi
if [[ $FROM_BRANCH == "" ]]; then
msg-error "Branch not found $FROM_REF"
return 20
fi
TO_BRANCH="$(git rev-parse --symbolic-full-name --abbrev-ref $TO_REF 2>&1)"
if [[ $? != 0 ]]; then
msg-error "Error"
msg-error "$TO_BRANCH"
return 10
fi
if [[ $TO_BRANCH == "" ]]; then
msg-error "Branch not found $TO_REF"
return 20
fi
echo "
FROM_BRANCH='$FROM_BRANCH'
TO_BRANCH='$TO_BRANCH'
"
if git merge-base --is-ancestor "$FROM_BRANCH" "$TO_BRANCH"; then
echo "'$FROM_BRANCH' can be fast-forwarded to '$TO_BRANCH'"
elif git merge-base --is-ancestor "$TO_BRANCH" "$FROM_BRANCH"; then
echo "'$FROM_BRANCH' is ahead of '$TO_BRANCH'"
return 1
else
echo "'$FROM_BRANCH' is unrelated to '$TO_BRANCH'"
return 2
fi
}
# Mercurial aliases
function hg-clone { hg clone ssh://hg@spliceserver2:/$1; }
alias hgs='hg st | grep -vE "^\?"'
alias hga='hg add'
alias hgan='hg add -n'
alias hgar='hg addremove'
alias hgc='hg commit -m'
alias hgch='hg update -c'
alias hgchb='hg branch'
alias hgpush='hg push'
alias hgpull='hg pull -u'
alias hgb='hg branch'
alias hgheads='hg heads .'
HGTEMPLATE="{rev}{ifeq(branch,'default','',' {branch}')}{if(bookmarks,' {bookmarks}')}{if(tags,' {tags}')} - {desc|firstline}"
alias hggraph='hg log -l 30 --graph --template "$HGTEMPLATE"'
function hgpbn {
if [[ "$#" == "1" ]]; then
hg push --branch "SPLICE-$1" --new-branch
else
hg push -r . --new-branch
fi
}
function hgpb {
if [[ "$#" == "1" ]]; then
hg push --branch "SPLICE-$1"
else
hg push -r .
fi
}
function hgub {
if [[ "$#" == "1" ]]; then
hg update -r "SPLICE-$1"
fi
}
function hgcpb {
hgc "$1"
hgpb
}
# Service aliases
function sr { service "$1" restart; }
function srv { service "$2" "$1"; }
alias srh='sr httpd'
alias sra='sr asterisk'
alias srm='sr mysqld'
alias srn='sr neo4j-service'
alias srs='sr smb'
alias sall='service --status-all'
alias sc='systemctl'
function scr { systemctl restart $1; }
alias scrh='scr httpd'
alias scra='scr apache2'
alias scrm='scr mysqld'
alias scrn='scr neo4j-service'
alias scrs='scr smb'
function scs { systemctl status $1; }
alias scsh='scs httpd'
alias scsa='scs apache2'
alias scsm='scs mysqld'
alias scsn='scs neo4j-service'
alias scss='scs smb'
alias scall='systemctl'
# Ansible aliases
alias play='ansible-playbook'
# Work directory
alias work='cd $WORK_HOME'
# Common functions
function hush {
SVR="$1"
ssh "$SVR" -- 'touch ~/.hushlogin'
}
# You may need to run this to use sudo
function ssh-disable-tty {
ssh -t "$1" -- "sudo sed -i.bak -E 's/^Defaults requiretty/# Defaults requiretty/' /etc/sudoers"
}
# If you have a welcome banner and it is mucking with rsync, try: hush SERVERFQDN
function rsyncpath { echo rsync -av "'$(hostname -f):$(realpath "$1" | sed -E 's% %\\ %g')'" ./ ; }
alias rsync-sudo='rsync -e "ssh -tt" --progress --rsync-path="sudo rsync"'
alias rsync-limited='rsync --progress --bwlimit=1000'
alias rsync-sudo-limited='rsync-sudo --bwlimit=1000'
function rsync-ext { rsync -amv --progress --include="$1" --include='*/' --exclude='*' $2 $3; }
function rsync-sudo-ext { rsync-sudo -amv --progress --include="$1" --include='*/' --exclude='*' $2 $3; }
function limited-rsync-ext { rsync-limited -amv --progress --include="$1" --include='*/' --exclude='*' $2 $3; }
function limited-rsync-sudo-ext { rsync-sudo-limited -amv --progress --include="$1" --include='*/' --exclude='*' $2 $3; }
function clean-comments { cat "$1" | grep -Ev "^\s*;"; }
alias sync-time='ntpdate pool.ntp.org'
function fix-time { ntpdate time.nist.gov; chkconfig ntpd on; service ntpd start; }
function mst-time { if [ -f "/usr/share/zoneinfo/America/Edmonton" ]; then rm -f /etc/localtime; ln -fs /usr/share/zoneinfo/America/Edmonton /etc/localtime; fi; }
function utc-time { if [ -f "/usr/share/zoneinfo/UTC" ]; then rm -f /etc/localtime; ln -fs /usr/share/zoneinfo/UTC /etc/localtime; fi; }
alias encrypt-file='gpg --cipher-algo AES256 --symmetric $1'
alias decrypt-file='gpg'
alias regex='$BASHRC_DIR/regex.py'
function regex-lineswap { sed -i '/'"$1"'/c\'"$2" "$3"; }
function allow-ssh-root-login { regex-lineswap '^PermitRootLogin' 'PermitRootLogin without-password' /etc/ssh/sshd_config; service sshd restart; }
alias edit-bashrc='cd $BASHRC_DIR; vim'
#Warning: resets permissions
function prepend {
if [ -f "$2" ]; then
echo "$1" | cat - "$2" > /tmp/prepend && mv /tmp/prepend "$2";
else
echo "$1" > "$2"
fi
}
function setup-ssh-login {
if [ ! -d ~/.ssh ]; then
mkdir ~/.ssh;
fi
chmod 700 ~/.ssh
cd ~/.ssh
if [ ! -f ~/.ssh/mlkey.pub ]; then
cp $BASHRC_DIR/ssh/mlkey.pub ./
fi
cat mlkey.pub >> authorized_keys
chmod 600 *
cd ~
if [ -f /usr/sbin/restorecon ]; then
restorecon -FRvv ~/.ssh
fi
}
function setup-ssh-creds {
if [ ! -d ~/.ssh ]; then
setup-ssh-login
fi
cd ~/.ssh
cp $BASHRC_DIR/ssh/mlkey.pem.gpg ./
decrypt-file mlkey.pem.gpg
ask-yes "Set mlkey as id_rsa?"
if [[ "$ASK" != "n" ]]; then
cp mlkey.pem id_rsa
fi
ask-yes "Set gmail as github key?"
if [[ "$ASK" != "n" ]]; then
cp $BASHRC_DIR/ssh/gmail.pem.gpg ./
decrypt-file gmail.pem.gpg
if grep -L "github.com" config > /dev/null; then
echo "config already contains github entry, skipping"
else
cat $BASHRC_DIR/ssh/config-git >> ~/.ssh/config
fi
cd $BASHRC_DIR
git remote set-url origin [email protected]:maludwig/bashrc.git
git config --global user.name "Mitchell Ludwig"
git config --global user.email [email protected]
cd ~/.ssh
fi
rm *.gpg
chmod 700 .
chmod 600 *
}
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
function ensure_running_agent {
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
}
function prepend-ssh-agent-forwarding { prepend "ForwardAgent yes" ~/.ssh/config; }
function generate-ssh-key {
cd ~/.ssh
ask-default 'What username will this key be for?' "$USER"
FILENAME="$ASK-$(date +%F)";
ssh-keygen -t rsa -b 4096 -C "$ASK - $(date +%F)" -f "$FILENAME";
msg-info "Your public key is below, you can share this with anyone, but keep your private key safe";
cat "$FILENAME.pub";
ask-yes "Shall I add this new key ($FILENAME) to ~/.ssh/config for you?";
if [[ "$ASK" != "n" ]]; then
echo $'\n'"IdentityFile ~/.ssh/$FILENAME" >> ~/.ssh/config;
msg-success "Added identity to config"
fi
grep -q "ensure_running_agent" ~/.bashrc
if [[ $? != 0 ]]; then
ask-yes "It looks like you are not running the ssh-agent. Shall I add it to your bashrc?"
if [[ "$ASK" != "n" ]]; then
echo "ensure_running_agent" >> ~/.bashrc;
msg-success "Added agent to bashrc"
fi
fi
ask-yes "Shall I add this new key ($FILENAME) to ~/.bashrc for you?";
if [[ "$ASK" != "n" ]]; then
echo "ssh-add ~/.ssh/$FILENAME" >> ~/.bashrc;
msg-success "Added key to agent"
fi
}
function set-host-alias { ask-default "Enter a host alias for this machine" "$HOST_ALIAS"; echo 'export HOST_ALIAS="'${ASK}'"' > $BASHRC_DIR/hostvars; . $BASHRC_DIR/hostvars; }
# Login aliases
alias suw='sudo su apache'
function timed_query {
WHOLE_QUERY=`cat`
log-8601 "--- Running query ---"
log-8601 "$WHOLE_QUERY"
START_TIME=`date +%s`
echo "$WHOLE_QUERY" | mysql -h${RDS_HOST} -u${RDS_USER} -p${RDS_PASS}
# sleep 64
END_TIME=`date +%s`
ELAPSED=$[END_TIME-START_TIME]
MINS=$[ELAPSED/60]
SECS=$[ELAPSED-$[MINS*60]]
if [[ "$ELAPSED" == 0 ]]; then
msg-error "Query ran suspiciously quickly."
fi
log-8601 "--- Done, ran for ${MINS}m${SECS}s ---"
}
function get-user-home-dir {
THEUSER="$1"
getent passwd "$THEUSER" | cut -d: -f6
}
function allow-bash-login {
ask-default "Which user should I allow bash access for?" "$1"
THEUSER="$ASK"
echo usermod -s /bin/bash "$THEUSER"
THEHOMEDIR=`get-user-home-dir`
ask-yes "Recursively change permissions for $THEUSER home directory, $THEHOMEDIR ?"
}
function query_count {
DB_AND_TABLE="$1"
mysql -h${RDS_HOST} -u${RDS_USER} -p${RDS_PASS} -N -B -e "SELECT COUNT(*) FROM ${DB_AND_TABLE};"
}
function time_each_query_in_file {
FILEPATH="$1"
ORIGINAL_IFS="$IFS"
IFS=';'
for QUERY in `cat "$FILEPATH"`; do
echo "$QUERY;" | timed_query
done
IFS="$ORIGINAL_IFS"
}
function setup-sync {
if [ -f ~/.sync_options ]; then
source ~/.sync_options
else
SYNCSVR='localhost'
SYNCLOCALPARENT=`realpath .`
SYNCREMOTEPARENT=`realpath .`
SYNCSWITCHES="-iav --exclude '*.pyc' --checksum --ignore-times --no-times --no-owner --no-group --no-perms"
fi
ask-default "Which remote host do you want to sync to?" "$SYNCSVR"
export SYNCSVR="$ASK"
ask-default "What is the parent directory on your local?" "$SYNCLOCALPARENT"
export SYNCLOCALPARENT="$ASK"
ask-default "What is the parent directory on the remote server?" "$SYNCREMOTEPARENT"
export SYNCREMOTEPARENT="$ASK"
ask-default "What switches should I use?" "$SYNCSWITCHES"
export SYNCSWITCHES="$ASK"
echo "
export SYNCSVR='$SYNCSVR'
export SYNCLOCALPARENT='$SYNCLOCALPARENT'
export SYNCREMOTEPARENT='$SYNCREMOTEPARENT'
export SYNCSWITCHES='$SYNCSWITCHES'
" > ~/.sync_options
}
function syncup {
DIRNAME="$1"
echo "Syncing:"
echo " Remote: $SYNCREMOTEPARENT/$DIRNAME/"
echo " Local: $SYNCLOCALPARENT/$DIRNAME/"
rsync-sudo $SYNCSWITCHES "$SYNCLOCALPARENT/$DIRNAME/" "$SYNCSVR:$SYNCREMOTEPARENT/$DIRNAME/"
}
function leeroy {
echo -ne "$CCYAN"
echo -ne "LE"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "$CGREEN"
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "$CYELLOW"
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "$CRED"
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "E"
sleep 0.1
echo -ne "R"
sleep 0.1
echo -ne "O"
sleep 0.1
echo -ne 'Y!'
sleep 1
echo -ne "$CPINK"
sleep 0.1
echo -ne ' JE'
sleep 0.1
echo -ne 'E'
sleep 0.1
echo -ne 'E'
sleep 0.1
echo -ne 'NK'
sleep 0.1
echo -ne 'I'
sleep 0.5
echo -ne 'N'
sleep 0.2
echo -ne 'S!!!'
}
RAINBOW=(${CYELLOW} ${CGREEN} ${CCYAN} ${CBLUE} ${CPINK} ${CRED})
function rainbow-sleep {
sleep "$1"
HEAD_COLOR="${RAINBOW[0]}"
RAINBOW=("${RAINBOW[@]:1}" "$HEAD_COLOR")
echo -ne "${HEAD_COLOR}.${CDEFAULT}"
}
sort_inplace () {
THEFILE="$1";
shift;
mv "$THEFILE" "$THEFILE.unsorted";
cat "$THEFILE.unsorted" | sort "$@" > "$THEFILE";
}
lineseps () {
LINECOUNT="${1:-5}"
LINE_SEPARATOR="${2:-========================================}"
SEDCMD=': loop; '
if [ "$LINECOUNT" -ge 2 ]; then
for IDX in `seq 2 $LINECOUNT`; do
SEDCMD="${SEDCMD}n; "
done
fi
SEDCMD="${SEDCMD}a ${LINE_SEPARATOR}
n; b loop"
if command -v gsed &> /dev/null; then
cat - | gsed "$SEDCMD"
else
cat - | sed "$SEDCMD"
fi
}
alias wtfdocker='echo docker run -it -p HOST:CONTAINER -v HOSTDIR:CONTAINERDIR maludwig/mitchubuntu:1.2'
function rows_in_a_and_b { comm -12 "$@"; }
function rows_in_a_but_not_b { comm -23 "$@"; }
function rows_in_b_but_not_a { comm -13 "$@"; }
whats_listening_on () { local PORT="$1"; sudo lsof -i TCP:"$PORT" | grep -i listen; }
cd_find () {
FOUND_FILE=`find "$PWD" -name "$1" | head -n1`;
if [[ "$FOUND_FILE" == "" ]]; then
FOUND_FILE=`find "$PWD" -iname '*'"$1"'*' | head -n1`;
fi
msg-info "$FOUND_FILE";
cdf "$FOUND_FILE"
}
alias chmox='chmod +x'
millis_for_cmd () {
START_NANOS=`date +%s.%N`
"$@"
END_NANOS=`date +%s.%N`
DURATION_MILLIS="$( python -c 'print(1000 * ('"$END_NANOS - $START_NANOS"'))' )"
echo "Duration (ms): $DURATION_MILLIS"
}
function avg { python -c 'import sys; nums=[float(n) for n in sys.argv[1:]]; print(sum(nums) / len(nums))' "$@" ; }
run_n_times () {
local TIMES="$1";
shift 1;
ALL_DURATIONS=();
for X in `seq $TIMES`;
do
millis_for_cmd "$@";
ALL_DURATIONS+=("$DURATION_MILLIS");
done
echo "Durations:"
for DURATION_MILLIS in "${ALL_DURATIONS[@]}"; do
echo " = $DURATION_MILLIS"
done
echo "Average: $(avg "${ALL_DURATIONS[@]}")"
}
decrypt_p12 () {
if echo "$1" | grep -q ".p12"; then
INPUT_FILE_DIR="$(dirname "$1")"
INPUT_FILE_BASENAME="$(basename "$1")"
ORIGINAL_PWD="$PWD"
cd "$INPUT_FILE_DIR"
COMMON_NAME="${INPUT_FILE_BASENAME%%.*}"
mkdir -p "$COMMON_NAME"
cp "$INPUT_FILE_BASENAME" "$COMMON_NAME"
cd "$COMMON_NAME"
openssl pkcs12 -in "$INPUT_FILE_BASENAME" -out "$COMMON_NAME".crt.pem -clcerts -nokeys
cat "$COMMON_NAME".crt.pem
openssl pkcs12 -in "$INPUT_FILE_BASENAME" -out "$COMMON_NAME".key.pem -nocerts -nodes
cat "$COMMON_NAME".key.pem
cd ..
zip -r "$COMMON_NAME".zip "$COMMON_NAME/"
cd "$ORIGINAL_PWD"
else
msg-error "Missing input file path"
return 1
fi
}
what_is_my_ip () {
dig +short myip.opendns.com @resolver1.opendns.com | grep -E '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -n1
}