-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc.functions
779 lines (712 loc) · 17.2 KB
/
.bashrc.functions
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/usr/bin/env bash
logPrint "${GREEN}Reading ~/.bashrc.functions$DEFAULT"
#TQ-TODO: Create sections for different types of functions?
function consoleReset()
{
export PS_OLD="$PS1"
export PS1="\u@\h:\W]\n> "
resize
tput init
resize
echo "Old PS1 ==> [$PS_OLD]"
}
function ps1Git()
{
if [ $USER == root ]
then
RH="$RED"
export PS1="\$(printGitBranchForPS1IfAvail)${GREEN}\u${DEFAULT}@\h:\W\n${RH}\\$> ${DEFAULT}"
else
RH="$GREEN"
export PS1="\$(printGitBranchForPS1IfAvail)${GREEN}\u${DEFAULT}@\h:\W\n${RH}\\$> ${DEFAULT}"
fi
}
function gPush()
{
if [[ $1 =~ -all ]]
then
git add .
shift
else
git add -u
fi
git commit -m "$*"
git push
}
function pf-sniff()
{
DASH_N=${NO_DNS:--n}
cecho "Using DASH_N=${DASH_N} (Set via \${NO_DNS:-n})"
sleep 0.2
if [[ $1 =~ file|read|-r ]]
then
if [[ $1 =~ all ]] || [[ $2 =~ all ]] # DONT 'shift' before
then
cecho "Processing ALL pflogs.*.gz in /var/log" 1>&2
sleep 0.1
shift
if [[ $1 =~ all ]]
then
shift
fi
for i in $(ls -t /var/log/pf*gz)
do
zcat $i | tcpdump $DASH_N -etttr - $* 2>/dev/null
done
else
shift
tcpdump $DASH_N -etttr /var/log/pflog $*
fi
else
tcpdump $DASH_N -nettti pflog0 $*
fi
}
export -f pf-sniff
function pf-strip()
{
cat - | perl -pe "s#.*on [a-z]+[0-5]{1,2}: ##" | perl -pe 's#.[0-9]{1,5} > # > #' | perl -pe 's#\.[0-9]{1,5}:#:#' | perl -pe 's#: S.*##'
cecho "tcp and 'tcp[tcpflags]=tcp-syn'"
}
function pf-rules()
{
FILTER=""
if [ -z $1 ]
then
pfctl -vv -s r | egrep "@"
else
for i in $*
do
FILTER="egrep $i|$FILTER"
done
PF_FILTER=${FILTER%|}
cecho cyan "Will eval: [pfctl -vv -s r | grep @ | $PF_FILTER]"
eval "pfctl -vv -s r | grep @ | $PF_FILTER"
fi
}
export -f pf-rules
function qgrep()
{
FILTER=""
if [ -z $1 ]
then
cecho red "grep for WHAT?"
else
for i in $*
do
FILTER="egrep $i|$FILTER"
done
GREP_FILTER=${FILTER%|}
cecho cyan "Will eval: [pfctl -vv -s r | $GREP_FILTER]"
eval "$GREP_FILTER"
fi
}
function isSuse()
{
if [ -s /etc/os-release ]
then
grep -qi suse /etc/os-release
else
return 1
fi
}
export -f isSuse
function isBsd()
{
uname -a | egrep -iq bsd
}
export -f isBsd
function is()
{
TYPE_IS=$(type $1)
if echo $TYPE_IS | egrep --color=auto -iq "function"
then
type $1;
elif echo $TYPE_IS | egrep -q alias
then
ali\as $1;
elif which $1 >/dev/null
then
which $1
else
echo "$1 is $TYPE"
fi
}
export -f is
function stripcom()
{
egrep -v "^(\s*#|$)" $1
}
function ttr() {
find . -mount -name "*~" | xargs -t -r rm
}
export -f ttr
function tf()
{
if isBsd
then
FOLLOW="-f"
else
FOLLOW="-F"
fi
if [ $# -eq 1 ]
then
tail $FOLLOW $1
elif [ -z $1 ]
then
tail $FOLLOW /var/log/messages
fi
}
function pl()
{
if ! isBsd
then
journalctl -b $* | egrep -i "postfix|smtp"
else
if [ x"$1" == x"-f" ]
then
CAT="tail -f"
else
CAT=cat
fi
$CAT /var/log/maillog
fi
}
export -f pl
function rl()
{
if ! isBsd
then
if [[ "$1" = [0-9]+ ]]
then
echo "Last $1 lines..."
LAST="-n $1"
shift
fi
journalctl $LAST $*
elif [[ $1 =~ -f ]]
then
shift
tail -f /var/log/messages
else
cat /var/log/messages
fi
}
function dl()
{
if ! isBsd
then
if [[ "$1" = [0-9]+ ]]
then
echo "Last $1 lines..."
LAST="-n $1"
shift
fi
journalctl $LAST $*
elif [[ $1 =~ -f ]]
then
shift
tail -f /var/log/daemon
else
cat /var/log/daemon
fi
}
export -f rl dl
function eeh()
{
if [ -z "$1" ]
then
emacs -nw $HA/configuration.yaml
return
else
cecho -bold "emacs -nw $HA/${1}*yaml"
sleep 0.4
fi
if [[ $1 =~ auto ]]
then
emacs -nw $HA/auto*yaml
else
emacs -nw $HA/$1*yaml
fi
}
function psG()
{
PS_PARAMS="-elF" #or alx?
echo -e $YELLOW"Full List:"$DEFAULT
ps $PS_PARAMS | grep $1 | grep -v grep
echo ""
echo -e $GREEN "Filtered:" $DEFAULT
ps $PS_PARAMS | grep $1 | awk '{print $3" "$4" "$17}'
}
function colstrip()
{
perl -ane "print \$F[$1] . \"\n\""
}
export -f colstrip
function cecho()
{
DEFAULT="\e[0m";
RST="$DEFAULT"
BOLD="\e[1m";
NOTBOLD="\e[22m";
UNBOLD="\e[22m";
BLACK="\e[30m";
RED="\e[31m";
GREEN="\e[32m";
YELLOW="\e[33m";
BLUE="\e[34m";
PINK="\e[35m";
CYAN="\e[36m";
WHITE="\e[37m";
UNDERLINE="\e[4m";
BLINK="\e[5m";
INVERSE="\e[7m";
if [ -z "$1" ]
then
echo -en $DEFAULT;
return;
fi
if ! [ -z "$RUNNING_FROM_CRON" ]
then
echo "$*"
return
fi
if [[ $1 =~ ^[-]*bold$ ]]
then
SET_BOLD=$BOLD;
shift;
else
SET_BOLD=$NOTBOLD;
fi
if [[ $1 =~ ^red$ ]]; then
COLOR=$RED;
shift;
else
if [[ $1 =~ ^green$ ]]; then
COLOR=$GREEN;
shift;
else
if [[ $1 =~ ^yellow$ ]]; then
COLOR=$YELLOW;
shift;
else
if [[ $1 =~ ^blue$ ]]; then
COLOR=$BLUE;
shift;
else
if [[ $1 =~ ^white$ ]]; then
COLOR=$WHITE;
shift;
else
if [[ $1 =~ ^pink$ ]]; then
COLOR=$PINK;
shift;
else
if [[ $1 =~ ^cyan$ ]]; then
COLOR=$CYAN;
shift;
else
if [[ $1 =~ ^black$ ]]; then
COLOR=$BLACK;
shift;
else
COLOR=$CYAN;
fi;
fi;
fi;
fi;
fi;
fi;
fi;
fi;
if test -t 1; then
echo -e ${SET_BOLD}${COLOR}"$*"$DEFAULT;
else
echo -e "$*";
fi
}
declare -fx cecho
export -f cecho
###
# GIT STUFF
###
function printGitBranchForPS1IfAvail
{
GIT_BRANCH=`parse_git_branch`
if [ $PWD == $HOME ]
then
return 0
fi
if ! [ -z $GIT_BRANCH ]
then
echo -e $BOLD[${UNBOLD}git:$BLINK$BLUE${GIT_BRANCH}$RST$BLACK$BOLD]$RST
else
echo -e $DEFAULT # TODO: CHeck if it messess upp screen-positions...
fi
}
function parse_git_branch()
{
ref=$(git-symbolic-ref HEAD 2> /dev/null) || return;
echo ${ref#refs/heads/}
}
declare -fx parse_git_branch
function getBranchPoint()
{
full_ref=$(git-symbolic-ref HEAD 2> /dev/null)
local_branch=${full_ref#refs/heads/}
diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "${1:-origin/sv}") <(git rev-list --first-parent "${local_branch:-bugfix}") | head -1
}
declare -fx getBranchPoint
function reMatch {
# TODO: This needs tweaking
typeset ec
unset -v reMatch # initialize output variable
if [ -z $1 -o -z $2 ]
then
echo "Usage: reMatch <input> <regexp>"
return -1
fi
cecho "String is: [$GREEN$1$CYAN] - Regexp: [$DEFAULT$BOLD$2$CYAN]"
[[ $1 =~ $2 ]] # perform the regex test
ec=$? # save exit code
if [[ $ec -eq 0 ]]
then # copy result to output variable
echo -e "1=${BASH_REMATCH[1]}, 2=${BASH_REMATCH[2]}, 3=${BASH_REMATCH[3]}, $BOLD${BLUE}ALL=$GREEN${BASH_REMATCH[@]}$DEFAULT"
[[ -n $BASH_VERSION ]] && reMatch=( "${BASH_REMATCH[@]}" )
[[ -n $KSH_VERSION ]] && reMatch=( "${.sh.match[@]}" )
[[ -n $ZSH_VERSION ]] && reMatch=( "$MATCH" "${match[@]}" )
else
echo NoMatch 1>&2
fi
return $ec
}
################################################################################
# HISTORY STUFF
################################################################################
function log_bash_persistent_history()
{
local HISTTIMEFORMAT="{%Y} - {%a, %d %b, %H:%M} : "
HISTTIMEFORMAT_REQ="$HISTTIMEFORMAT"
if [ "$HISTTIMEFORMAT" != "$HISTTIMEFORMAT_REQ" ]; then
echo -e $RED"HISTTIMEFORMAT is not valid for log_bash_persistent_history() $HISTTIMEFORMAT != $HISTTIMEFORMAT_REQ"$DEFAULT;
return;
fi;
LAST_CMD="$(history 1)"
# 32069 [2023] - {Sat, 21 Oct, 12:51} : is log_bash_persistent_history
#NR {YEAR} - {DATE_AND_TIME} : COMMAND
#local regexp="[0-9]{1,6}[ ]+\[([0-9]{4,4})\] - {(.*?)}[[:blank:]]+: (.*)"
local regexp=${PERSISTENT_REGEXP}
[[ $LAST_CMD =~ $regexp ]]
reqexp_result=$?
local full_match="${BASH_REMATCH[0]}"
local year_part="${BASH_REMATCH[1]}"
local date_part="${BASH_REMATCH[2]}"
local command_part="${BASH_REMATCH[3]}"
if [[ $command_part =~ ^cliGrep|reMatch ]]; then
return;
fi
if [ $reqexp_result -ne 0 -o -z "$command_part" ]
then
cecho bold red "Failed to log ~/.persistent_history($reqexp_result)"
cecho bold red "historyCmd = '${BLINK}$LAST_CMD${RST}'"
cecho bold red "regexp = '$regexp'"
cecho "BASH_REMATCH[0] = ${BASH_REMATCH[0]}"
return
fi
if [ $reqexp_result -eq 0 -a x${PERSISTENT_HIST_DEBUG} == x"YES" ]
then
cecho red "LastCmd: $BLACK'$LAST_CMD'\n${CYAN}RegExp: $BLACK'$regexp'"
cecho "ALL = $BLACK [ $full_match ] $RST"
cecho "year_part = $BLACK [ $year_part ] $RST"
cecho "date_part = $BLACK [ $date_part ] $RST"
cecho "command_part = $BLACK[ $command_part ] $RST"
fi
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
then
LINE="$date_part | $command_part"
echo $LINE >> ~/.persistent_history.$year_part;
export PERSISTENT_HISTORY_LAST="$command_part";
else
if [ "$HIST_DEBUG" != "" ]; then
cecho "Not logging ($PERSISTENT_HISTORY_LAST == $command_part)";
fi;
fi
}
declare -fx log_bash_persistent_history
function cliGrep()
{
PERM_HIST=~/.persistent_history.20`date +%y`;
if [[ $1 =~ ^-a ]]
then
shift
if ! [ -z "$*" ]
then
# echo "Running [egrep "$*" $PERM_HIST]"
egrep "$*" $PERM_HIST;
return
fi
elif [[ $1 =~ ^-m ]]
then
INTERVAL=${1#-m};
INTERVAL=$((INTERVAL*100))
shift
cecho "interval = $INTERVAL"
egrep "$@" $PERM_HIST | tail -$INTERVAL;
return
elif ! [ -z $* ]
then
DAY_NAME=`date +%a`;
DAY_NR=`date +%d`; # 0-Indexed
cecho "Date-based ($*) [grep \"^$DAY_NAME, $DAY_NR\" $PERM_HIST]"
egrep "^$DAY_NAME, $DAY_NR" $PERM_HIST | egrep "$*"
return
fi
cecho -bold red "What do you want to grep for??"
}
declare -fx cliGrep
function doneDate()
{
PERM_HIST=~/.persistent_history.20`date +%y`;
if [[ $1 =~ -m ]]; then
MONTH=$2;
shift;
shift;
else
MONTH=`date +%h`;
fi;
if [ -z $1 ]; then
DAY_NR=`date +%d`;
else
DAY_NR="$1";
shift;
fi;
if ! [ "$DAY_NR" -ge 1 -a "$DAY_NR" -le 31 ]; then
cecho red "Invalid Day_nr($DAY_NR), using today: $DAY_NR";
fi;
if [[ $1 =~ all ]]; then
ALL=yes;
shift;
else
ALL="";
fi;
# if [ $DAY_NR -lt 10 ]; then
# DAY_NR="$DAY_NR";
# fi;
if [ -z $MONTH ]; then
MONTH=`date +%b`;
fi;
YEAR=`date +%Y`;
ALL_DAY="/tmp/doneDate.$DAY_NR.$MONTH.all";
egrep --color=never "^[a-zA-Z]{3,3}, $DAY_NR $MONTH," $PERM_HIST | sort -n > $ALL_DAY;
head -15 $ALL_DAY;
tail -15 $ALL_DAY;
if [ -z $ALL ]; then
echo "cat $ALL_DAY";
wc -l $ALL_DAY;
echo "";
else
echo ====================;
cat $ALL_DAY;
fi
}
declare -fx doneDate
############################
# Docker stuff
#######################
function dps()
{
TABLE="table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Image}}\t{{.Networks}}"
# echo table=$TABLE
if [ -z $1 ]
then
docker ps --format "$TABLE"
elif [[ "$1" =~ -all ]]
then
shift
docker ps --all --format "$TABLE" | egrep "(CONTAINER ID|$*)"
else
docker ps --format "$TABLE" | egrep "(CONTAINER ID|$*)"
fi
}
#ml()
#{
# LIST=`find /var/lib/docker/volumes/kolla_logs/_data/ | grep $1`
# for i in $LIST
# do
# echo cat $i
# done
#}
function dshell()
{
if [ $# -lt 2 ]
then
CMD_RUN=bash
CONTAINER=$1
else
CONTAINER=$1
shift
CMD_RUN="$*"
fi
echo "docker exec -it $CONTAINER $CMD_RUN"
docker exec -it $CONTAINER $CMD_RUN
}
function dll()
{
local TABLE="table {{.ID}}\t{{.Names}}\t{{.Status}}"
if [ -z $1 ]; then
echo "Which container?";
dps
return 1;
fi;
echo "Searching for containers matching [$1]...";
FOUND="";
FILTER=$1;
shift;
for c in `docker ps --format "{{.Names}}"`;
do
if [[ $c =~ $FILTER ]]; then
echo -e "=========== Found container: $CYAN$BOLD$c$DEFAULT ==============";
sleep 1;
docker logs $c $*;
echo "---------------- End -------------------------";
FOUND="TRUE";
fi;
done;
if [ -z $FOUND ]; then
echo "Container matching [$FILTER] NOT found";
docker ps --all --format "$TABLE"
fi
}
function dDump()
{
local TABLE="table {{.ID}}\t{{.Names}}\t{{.Status}}"
if [ -z $1 ]
then
echo "Which container?"
dps
return 1
fi
echo "Searching for containers matching [$1]..."
FOUND=""
for c in `docker ps --format "{{.Names}}"`
do
if [[ $c =~ $1 ]]
then
echo "=========== Found container: $c =============="
sleep 1
docker inspect $c
echo "---------------- End -------------------------"
FOUND="TRUE"
fi
done
if [ -z $FOUND ]
then
echo "Container($1) NOT found"
docker ps --all --format "$TABLE"
fi
}
export -f dDump
function cdLL()
{
if [ -z $1 ]
then
echo "Listing logs in /var/lib/docker/volumes/kolla_logs/_data"
echo ""
find /var/lib/docker/volumes/kolla_logs/_data -name "*.log"
else
echo "Will search for logfiles matching name [$1]..."
FILE=`find /var/lib/docker/volumes/kolla_logs/_data -name "*.log" | grep $1`
if [ -s "$FILE" ]
then
echo more $FILE
elif [ -s "$FILE.log" ]
then
echo more $FILE
else
for i in $FILE
do
echo more $i
done
fi
echo "------end-----"
fi
}
LoopPfLogs ()
{
if [[ "$1" =~ -m[0-9]+ ]]
then
MTIME="-mtime ${1#-m}"
CMD="find /var/log/ -name \"pflo*\" $MTIME"
shift
else
CMD="find /var/log/ -name \"pflo*\""
fi
cecho green "Treating [$*] as tcpdump filter"
sleep 0.5
cecho blue FindCMD:[$CMD]
sleep 1
MONTH=$(date '+%h')
for i in `eval $CMD`
do
FILE=$(ls -l $i | perl -pe "s#.*$MONTH#$MONTH#")
cecho "Reading $FILE" >&2
sleep 0.1
# continue
if [[ $i =~ pf.*gz ]]
then
zcat $i | tcpdump -netttr - $*;
else
tcpdump -netttr $i $*
fi
done
}
export -f LoopPfLogs
function ZZZ()
{
if ! isSuse
then
cecho -bold red "This aint Suse, so you need to run something else for updates:"
cecho -green "Maybe: [apt-get update && apt-get upgrade]"
return
fi
zypper -t lu >/tmp/zypper.out
ZUPP=$(cat /tmp/zypper.out | grep ^v | cut -d"|" -f3 | grep -v "kernel|dracut|system|udev")
if [ -z "$ZUPP" ]
then
if grep -q "No updates found" /tmp/zypper.out
then
cecho -bold green "No updates (at all)"
else
cecho "Mainly kernel updates..."
cecho -----------------
cat /tmp/zypper.out
cecho -----------------
fi
return
fi
ZLIST="$(echo $ZUPP)"
echo "ZLIST = $ZLIST"
cecho -bold "To update, run:\n zypper up -y --auto-agree-with-licenses --auto-agree-with-product-licenses --allow-vendor-change $ZLIST"
}
function pCat()
{
if [ -z "$1" ]
then
cecho -bold red "PathCat Which File??"
return 1
fi
if [ "$1" == "-m" -o "$2" == "-m" ]
then
cecho "Using more..."
CAT_OR_MORE="more"
if [ "$1" == "-m" ]
then
shift
fi
fi
PATH_TO_SCRIPT=$(which $1)
cecho blue "File $1 was found at: [$BOLD$PATH_TO_SCRIPT$UNBOLD]"
${CAT_OR_MORE:-cat} $PATH_TO_SCRIPT
cecho blue "File $1 was found at: [$BOLD$PATH_TO_SCRIPT$UNBOLD]"
}
complete -c pCat
export -f pCat