-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bash_functions
418 lines (356 loc) · 11.7 KB
/
.bash_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
#!/usr/bin/env bash
function aws_session_validity(){
local expiration_ts now_ts expires_in
[[ $AWS_SESSION_EXPIRATION ]] || return
expiration_ts=$(date -j -f "%FT%T%z" "${AWS_SESSION_EXPIRATION%:*}${AWS_SESSION_EXPIRATION##*:}" '+%s')
now_ts=$(date +%s)
expires_in=$((expiration_ts - now_ts))
if [[ $expires_in -ge 1800 ]]
then
printf ✅
elif [[ $expires_in -lt 1800 && $expires_in -gt 300 ]]
then
printf ⚠️
elif [[ $expires_in -lt 300 && $expires_in -gt 0 ]]
then
printf ‼️
else
printf ❌
fi
}
function _jj(){
find . -type f -name '_jj*' | fzf --exact --select-1 -i ${@:+--query "$*"} | xargs view
}
function webcamsnap(){
local outputfile
outputfile="$(mktemp).jpg"
ffmpeg -ss 00:00:00 -f avfoundation -r 30.000030 -i "0" -t 1 -frames:v 1 -q:v 2 "$outputfile"
printf 'Snapshot saved to:\n%s\n' "$outputfile"
}
function functionfinder(){
shopt -s extdebug
declare -F "$1"
shopt -u extdebug
}
function doh(){
local doh_url="${DOH_BASE_URL:-https://cloudflare-dns.com/dns-query}"
doh_url=$(trurl --url "$doh_url" --append "query=name=$1")
[[ -n $2 ]] && doh_url=$(trurl --url "$doh_url" --append "query=type=$2")
curl -H "accept: application/dns-json" "$doh_url"
}
function cdfzf(){
local __projectdir
__projectdir=$(
find "$HOME/$__rootdir" -type d -execdir test -d {}/.git \; -prune -print \
| fzf --exact --select-1 -i ${@:+--query "$*"}
)
cd "$__projectdir" || return
}
function ,mstp(){
,cklb "gitlab.com/mailstep" "${@:-}"
}
function ,cklb(){
local -x __rootdir="${FUNCNAME[0]/,/}"
cdfzf "$@"
}
function ,projects(){
local -x __rootdir="${FUNCNAME[0]/,/}"
cdfzf "$@"
}
function ,sbks(){
local -x __rootdir="${FUNCNAME[0]/,/}"
cdfzf "$@"
}
# {{{ terraform
function tfkubeconfig(){ # get kubeconfig from terraform output and save it as .kubeconfig.NAME
terraform output --json "$1" | jq .kubeconfig | yq e . > ".kubeconfig.$1"
}
function tf(){ # terraform wrapper with environment picker
local TF_cmd=$1
shift
local -x TF_CLI_ARGS
if [[ -d ./variables ]]
then
var_file=$(find ./variables -type f | fzf -1 -q "$@")
tf_environment=$(awk -F '"' '/^environment[[:space:]]/ { print $2}' "$var_file")
TF_CLI_ARGS+=" -var-file=$var_file"
export TF_WORKSPACE=$tf_environment
direnv allow
eval "$(direnv export bash)"
unset TF_WORKSPACE
fi
terraform "$TF_cmd"
unset TF_CLI_ARGS
}
function tfa(){ # terraform apply wrapper utilizing tf()
tf apply "$@"
}
function tfp(){ # terraform plan wrapper utilizing tf()
tf plan "$@"
}
function tfpnl(){ # terraform plan -lock=false wrapper utilizing tf()
local -x TF_CLI_ARGS_plan="-lock=false"
tfp "$@"
}
# }}}
__local_getMFA () {
totp "$1"
}
export -f __local_getMFA
# {{{ Functions used in PS1.
function ec(){ # get exitcode
local EC=$?
[[ "$EC" -eq 0 || "$EC" -eq 130 ]] && return
printf '\x01%b\x02[\x01%b\x02\x01%b\x02%d\x01%b\x02\x01%b\x02]\x01%b\x02' "$__c1" "$crst" '\e[1;31m' "$EC" "$crst" "$__c1" "$crst"
}
function aws_ps1(){
[[ -z $AWS_PROFILE || $AWS_PROFILE == 'default' || ${AWS_PROFILE@a} != *x* ]] && return
printf "\x01%b\x02[\x01%b\x02\x01%b\x02%s\x01%b\x02\x01%b\x02]\x01%b\x02" "$__c1" "$crst" '\e[1;31m' "$AWS_PROFILE" "$crst" "$__c1" "$crst"
}
function nr_sessions(){ # get number of tmux+screen sessions
local TMUX_SESSIONS SCREEN_SESSIONS
TMUX_SESSIONS=$(tmux list-sessions 2> /dev/null | wc -l)
SCREEN_SESSIONS=$(screen -list 2> /dev/null | grep -c $'\t')
[[ $TERM == screen* ]] && SCREEN_SESSIONS=$(( SCREEN_SESSIONS - 1 ))
(( SCREEN_SESSIONS + TMUX_SESSIONS == 0 )) && return
printf '\x01%b\x02[\x01%b\x02%d\x01%b\x02]\x01%b\x02' "$__c1" "$crst" "$(( SCREEN_SESSIONS + TMUX_SESSIONS ))" "$__c1" "$crst"
}
# }}}
function source_all_files_from_dir(){
[[ ! -d "$1" ]] && return
local comp
for comp in "$1/"${2:-*}
do
# shellcheck source=/dev/null
[[ -f $comp ]] && source "$comp"
done
}
function totp(){ gopass totp -o "$1" ; }
export -f totp
function totpc(){ gopass totp -c "$1" ; }
function aws_pick_profile(){
export AWS_PROFILE
AWS_PROFILE=$(aws configure list-profiles | fzf --sort)
}
function aws_add_creds(){
aws configure set "profile.$1.aws_access_key_id" "$2"
aws configure set "profile.$1.aws_secret_access_key" "$3"
printf 1>&2 'Created aws profile %s and swithced to it temporarily' "$1"
aws sts get-caller-identity && export AWS_PROFILE=$1
}
function aws_ips(){
local region=$1 service=$2 filter
if [[ $region && $service ]]
then
filter=".prefixes[]|select((.network_border_group==\"$region\") and .service==\"$service\")"
elif [[ $service ]]
then
filter=".prefixes[]|select(.service==\"$service\")"
elif [[ $region ]]
then
filter=".prefixes[]|select(.network_border_group==\"$region\")"
else
filter=""
fi
curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq "$filter"
}
function gl_path(){
p 627f2c5 <<<"
\set QUIET on
\timing off
\t
\a
\f '\t'
\set QUIET off
SELECT n.path AS namespace,
p.path AS project,
concat('/srv/git/repositories/'||disk_path||'.git') AS path
FROM project_repositories pr
LEFT JOIN projects p ON p.id=pr.project_id
LEFT JOIN namespaces n ON n.id=p.namespace_id
WHERE concat(n.path||'/'||p.path) LIKE '%$1%' ORDER BY 1,2" | column -t
}
function hf(){ # history fuzzy search
local -a cmds
local cmd
local -x FZF_DEFAULT_OPTS
FZF_DEFAULT_OPTS+='--preview "/bin/echo {} | fold -w $COLUMNS" '
FZF_DEFAULT_OPTS+='--preview-window=down:33% '
FZF_DEFAULT_OPTS+='--multi '
FZF_DEFAULT_OPTS+='--print0 '
FZF_DEFAULT_OPTS+='--tac '
FZF_DEFAULT_OPTS+='--bind="esc:execute(pbcopy <<<{})" '
FZF_DEFAULT_OPTS+='--header="When multiple commands selected with [TAB] they'\''re executed in the same oreder as selected"'
readarray -d '' -t cmds < <(cat ~/.bash_history_files/* | fzf)
for cmd in "${cmds[@]}"
do
$cmd
done
}
function kk(){ ps -ef | fzf -m --preview='cat {}' --preview-window down,wrap | awk '{print $2}' | xargs kill; }
function dt(){ date -j -f %Y%m%d%H%M%S "$1" +%s; } # date time to ts
function td(){ date -j -f %s "$1" +'%F %R:%S'; } # ts to date time
function cd_wifi(){
local ip
ip=$(dig +short cdwifi.cz "@$(route -n get default | awk '/gateway/{printf $2}')")
curl "http://$ip/portal/api/vehicle/gateway/user/authenticate?category=internet&url=http%3A%2F%2Fcdwifi.cz%2Fportal%2Fapi%2Fvehicle%2Fgateway%2Fuser%2Fsuccess&onerror=http%3A%2F%2Fcdwifi.cz%2Fportal%2Fapi%2Fvehicle%2Fgateway%2Fuser%2Ferror" \
-H 'Host: cdwifi.cz' \
-H 'Accept: application/json' \
-H 'Referer: http://cdwifi.cz/captive' \
--compressed \
-INSsk
while :
do
curl "http://$ip/portal/api/vehicle/gateway/user" | jq
sleep 300
done
}
function zlatestranky(){
local pattern=$1
[[ -z $pattern ]] && { cat -; return; }
shift
[[ ${#FUNCNAME[*]} -eq 1 ]] && {
rg -z -i "$pattern" "$HOME/Downloads/fb_cz.txt.gz" | zlatestranky "$@"
return
}
rg -i "$pattern" | zlatestranky "$@"
}
function __wifiqr_complete(){
readarray -t ssids < <(
security dump-keychain | \
grep 'AirPort network password' -B4 | \
awk -F= '/acct/ {print $2}'
)
complete -W "${ssids[*]}" wifiqr
}
function wifiqr(){ qrencode -t ANSIUTF8 -o - "WIFI:T:WPA;S:$*;P:$(security find-generic-password -D 'AirPort network password' -a "$*" -w);;"; }
function ldapaudit(){
local filter
[[ "$#" -eq 0 ]] && printf 'usage: %s timeRangeHigherThen [timeRangeLowerThen] [filter]\ntimeRanges are in YYYYMMDDHHMMSS.uuuuuuZ\nFilter is &ed with the timeranges filter (&(timeRangeHigherThen)(timeRangeLowerThen)(filter))\a\n' "$0" && return
[[ "$#" -eq 1 ]] && filter="reqStart>=$1"
[[ "$#" -eq 2 ]] && filter="(&(reqstart>=$1)(reqStart<=$2))"
[[ "$#" -ge 3 ]] && filter="(&(reqstart>=$1)(reqStart<=$2)$3)"
ldapsearch -W -S reqStart -bcn=accesslog "$filter" reqStart reqMod reqDn reqType
}
function sshmux(){ ssh -vt "${1:-hopnode}" 'tmux attach || tmux'; }
function moshmux(){ TERM=xterm-256color mosh "${1:-hopnode}" tmux attach; }
function removedia(){ iconv -f utf8 -t ascii//TRANSLIT | sed 's/[^a-zA-Z 0-9,]//g'; }
function hst2(){ # create histogram from output of `uniq -c`
local -x RATIO=${1:-1}
awk '{print $1" "$2}' | perl -e '
use POSIX;
while(<>){
chomp;
my @a=split(/ /);
print $a[1] . " " . "█" x (ceil($a[0]/$ENV{'RATIO'})) . "\n";
}'
}
function hst(){
local -x RATIO=${1:-1}
awk '{print $1" "$2}' | perl -e '
use POSIX;
while(<>){
chomp;
my @a=split(/ /);
print $a[1] . " " . "+" x (ceil($a[0]/$ENV{'RATIO'})) . "\n";
}'
}
function psg(){ pgrep -i -a -l -f "$@"; }
function odjebat(){ ssh-keygen -R "$1" && ssh -o 'StrictHostKeyChecking=no' "$1"; }
function genpasswd(){ LC_CTYPE=C tr -dc '[:print:]' < /dev/urandom | head -c "${1:-20}"; } # password generator
function rpbcopy(){ # copy to system clipboard from remote hosts
printf '\033]1337;CopyToClipboard=;\a'
cat -
pintf '\033]1337;EndCopy\a'
}
function prefix(){ # prefix stdout with date,time or whatever you want
local prefix=
function __prefix_helper(){
case $1 in
"ts") prefix=$(date +%s) ;;
"date") prefix=$(date +%Y-%m-%dT%H:%M:%S) ;;
"time") prefix=$(date +%H:%M:%S) ;;
*) prefix="$1" ;;
esac
[[ "$2" = "-n" ]] || prefix="$(echo -e "\\x01\\e[1;37m\\x02${prefix}\\x01\\e[0m\\x02:\t")"
}
while read -r
do
__prefix_helper "$@"
printf '%s%s\n' "$prefix" "$REPLY"
done
}
function keychain_get_internet_password(){ # not using anymore
[[ $# -ne 2 ]] && { printf 'usage:\n %s <server> <user>\n' "${FUNCNAME[0]}"; return 1; }
security find-internet-password -gs "$1" -a "$2" -w | tr -d '\n'
}
function proxy(){
local proxyport=1080
local proxyhost=127.0.0.1
local sshhost=hopnode
function __proxy_unset(){
unset all_proxy no_proxy
sed -i "" '/_proxy=/d' ~/.bash_local
}
function __proxy_export(){
export all_proxy=socks://$proxyhost:$proxyport/
export no_proxy=localhost,127.0.0.1,::1,ccl
cat <<-EOF >> ~/.bash_local
export all_proxy=socks://$proxyhost:$proxyport/
export no_proxy=localhost,127.0.0.1,::1,ccl
EOF
}
function __proxy_start(){
ssh $sshhost -o VisualHostKey=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PermitLocalCommand=no -Nfnaxqv -M -S /tmp/ssh_proxy_sock -D $proxyport 2>/tmp/ssh_proxy_stats && \
networksetup -setsocksfirewallproxy Wi-Fi $proxyhost $proxyport # -setproxybypassdomains localhost 127.0.0.1 ::1 ccl&& \
printf '\e[1;32mProxy started\e[0m\n'
}
function __proxy_stop(){
ssh $sshhost -o VisualHostKey=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PermitLocalCommand=no -q -S /tmp/ssh_proxy_sock -O exit > /dev/null && \
networksetup -setsocksfirewallproxystate Wi-Fi off -setproxybypassdomains Empty && \
printf '\e[0;33mProxy statistics:\e[0m\n'
tail /tmp/ssh_proxy_stats | grep -e second -e compress --color=no
rm /tmp/ssh_proxy_stats
printf '\e[1;31mProxy stopped\e[0m\n'
}
function __proxy_status(){
printf '\e[0;33mSystem SOCKS5 configuration:\e[0m\n'
networksetup -getsocksfirewallproxy Wi-Fi
printf '\e[0;33mbash environment variables:\e[0m\n'
env | grep --colour=never "_proxy=" || true
printf '\e[0;33mssh dynamic tunnel:\e[0m\n'
ssh $sshhost -o VisualHostKey=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PermitLocalCommand=no -q -S /tmp/ssh_proxy_sock -O check || true
}
function __proxy_help(){
cat <<-EOF
proxy <start|stop|status|clean>
start: start ssh dynamic tunnel, enable socks5 proxy, export variables
stop: stop ssh tunnel, disable socks5 proxy , unset variables
status: print information about proxying
clean: unset variables
EOF
}
case "$1" in
"status")
__proxy_status;;
"clean")
__proxy_unset;;
"start")
__proxy_start
__proxy_export
__proxy_status;;
"stop")
__proxy_stop
__proxy_unset
__proxy_status;;
*)
__proxy_help;;
esac
__proxy_help
printf "\a"
return 1
}
export -f \
ec \
nr_sessions
# vim:foldmethod=indent:foldlevel=0:tabstop=4:shiftwidth=4:softtabstop=0:noexpandtab