-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bash_functions
244 lines (206 loc) · 7.22 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
function error { IFS='\n' printf >&2 "[1;31mERROR: %s[0m\n" "$*" ;}
function success { IFS='\n' printf >&2 "[1;32mSUCCESS: %s[0m\n" "$*" ;}
function warning { IFS='\n' printf >&2 "[1;33mWARNING: %s[0m\n" "$*" ;}
function prompt { IFS='\n' printf >&2 "[1;36m%s[0m\n" "$*" ;}
function quit { prompt "Exiting..." ; exit 0 ;}
function git-grab {
# Pull from the specified repos all at once
targets=( ${*:-$(pwd)} )
for target in ${targets[*]} ; do
printf "%-20s: " ${target}
git -C $target pull --rebase
done
}
function tcp {
# Shortcut for making a TCP connection using bash's builtin capability
cat < /dev/tcp/"$1"/"$2"
}
function udp {
# Shortcut for making a UDP connection using bash's builtin capability
cat < /dev/udp/"$1"/"$2"
}
function csr {
# Parse a CSR and return it's full contents or specified values
certReq="$1" && shift 1
flags="${*--text}"
openssl req -in $certReq -noout $flags
}
function ssl {
# Parse a PEM certificate and return it's full contents or specified values
cert="$1" && shift 1
flags="${*--text}"
openssl x509 -in $cert -noout $flags
}
function sslv {
# Connect to a specified host and print some basic information about its certificate
hostname="$1"
port="${2:-443}"
openssl s_client -connect $hostname:$port -servername $hostname -showcerts < /dev/null 2> /dev/null |
openssl x509 -subject -issuer -dates -noout
}
function sslvv {
# Connect to a specified host and print all information about its certificate
hostname="$1"
port="${2:-443}"
openssl s_client -connect $hostname:$port -servername $hostname -showcerts < /dev/null 2> /dev/null |
openssl x509 -noout -text
}
function man {
# Environmental variables for colorized manpages
env \
LESS_TERMCAP_mb=$(printf "\e[1;35m") \
LESS_TERMCAP_md=$(printf "\e[1;36m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;47;30m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;33m") \
man "$@"
}
function grepe {
# Print entire file to stdout with regex highlighted
grep -E "$1|$" $2
}
function read_dom {
# Poor man's {X,HT}ML parser, shamelessly ganked from
# http://stackoverflow.com/a/6541324
local IFS='\>'
read -d \< entity content
local ret="$?"
tag=${entity%% *}
attributes=${entity#* }
return $ret
}
function scrape {
# Poor man's web scraper
target="$1"
url="$2"
while read_dom ; do
if [ "$tag" == "$target" ] ; then
eval local $attributes
echo $content
fi
done <<< $(curl -s $url)
}
function batt {
# Print kernel's understanding of the remaining charge in system battery
if [ -d /sys/class/power_supply/BAT0/ ] ; then
# sysfs doesn't enclose its variables in spaces. Working around by
# temporarily dumping to, sourcing and cleaning up a file
sed -e 's/=\(.*\)/="\1"/' /sys/class/power_supply/BAT0/uevent > /tmp/BAT0
source /tmp/BAT0 && rm -f /tmp/BAT0
if [ "$POWER_SUPPLY_MANUFACTURER" == 'Samsung SDI' ] ; then # Dell Latitude E7470
echo $(bc <<< "scale=2 ; 100 * $POWER_SUPPLY_CHARGE_NOW / $POWER_SUPPLY_CHARGE_FULL")% remaining
elif [ "$POWER_SUPPLY_MANUFACTURER" == 'LGC-LGC5.53' ] ; then # Dell Latitude E7370
echo $(bc <<< "scale=2 ; 100 * $POWER_SUPPLY_CHARGE_NOW / $POWER_SUPPLY_CHARGE_FULL")% remaining
elif [[ "$POWER_SUPPLY_MANUFACTURER" =~ 'SMP' ]] ; then # Chromebook Pixel 2, Latitude 5289
echo $(bc <<< "scale=2 ; 100 * $POWER_SUPPLY_CHARGE_NOW / $POWER_SUPPLY_CHARGE_FULL")% remaining
elif [ "$POWER_SUPPLY_MANUFACTURER" == 'SANYO' ] ; then # ThinkPad T420
echo $(bc <<< "scale=2 ; 100 * $POWER_SUPPLY_ENERGY_NOW / $POWER_SUPPLY_ENERGY_FULL")% remaining
elif [ "$POWER_SUPPLY_MANUFACTURER" == 'LGC' ] ; then # ThinkPad X1 Carbon Gen 7
echo $(bc <<< "scale=2 ; 100 * $POWER_SUPPLY_ENERGY_NOW / $POWER_SUPPLY_ENERGY_FULL")% remaining
else
echo "INFO: Battery manufacturer unknown; time to update this function?"
fi
else
echo 'WARNING: Could not detect battery... Are you on a laptop?'
fi
}
function randomstring {
# Sometimes you just need a string full of garbage
[ $1 -gt 0 ] 2> /dev/null && length=$1 || length='24'
cat /dev/urandom | tr -cd '[:alnum:]' | head -c $length ; echo
}
function convert-win-timestamp {
# Number of 100-nanosecond intervals since 1601 Jan 1 UTC
date -d @$(bc <<< "${1}/10000000-11644473600") '+%F %T %Z'
}
function convert-unix-timestamp {
# Number of seconds since 1970 Jan 1 UTC
date -d @${1} '+%F %T %Z'
}
function parse-yaml {
# Half-assed YAML parser for distinguishing keys from values
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
function stubborn-ssh {
# Continue attempting to ssh into a host until it finally lets you
# Usually used when waiting on a system to come back online
export TIMEFORMAT="2%R"
false
while [ $? -ne 0 ]; do
sleep 3;
time ssh $@ && true;
done
}
function time-page-load {
# Generate a TSV of timings for website page loads
siteUrl="$1"
prompt "Recording page load times, Ctrl+C to exit:"
while : ; do
export TIMEFORMAT=%2R ; date +%T | tr '\n' '\t' >> $siteUrl.tsv
{ time curl -sko /dev/null $siteUrl ;} 2>> $siteUrl.tsv
sleep 300
done
}
function ssh-fingerprint {
sshKey="$1"
for i in md5 sha1 sha256 ; do
ssh-keygen -l -f $sshKey -E $i
done
[[ $(head -n1 $sshKey) =~ PRIVATE ]] \
&& ssh-keygen -y -f $sshKey \
|| cat $sshKey
}
function ocr {
tesseract -l eng "$*" - 2> /dev/null
}
ssh() {
if [[ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" =~ "tmux" ]]; then
tmux rename-window "$(echo $* | cut -d . -f 1)"
command ssh "$@"
tmux set-window-option automatic-rename "on" 1>/dev/null
else
command ssh "$@"
fi
}
function mv-recent-downloads {
# Grab everything from $XDG_DOWNLOAD_DIR or equivalent and move to target dir
# Move everything within the last n minutes (or 5 by default)
interval=${1:-5}
# Move to the specified directory (or $PWD by default)
dest=${2:-.}
[ -r ~/.config/user-dirs.dirs ] \
&& . ~/.config/user-dirs.dirs \
|| XDG_DOWNLOAD_DIR=~/downloads
find $XDG_DOWNLOAD_DIR -type f -mmin -$interval -exec mv -i {} $dest \;
}
function mv-recent-screenshots {
# Grab everything from $XDG_DOWNLOAD_DIR or equivalent and move to target dir
# Move everything within the last n minutes (or 5 by default)
interval=${1:-5}
# Move to the specified directory (or $PWD by default)
dest=${2:-.}
[ -r ~/.config/user-dirs.dirs ] \
&& . ~/.config/user-dirs.dirs \
|| XDG_PICTURES_DIR=~/pictures
find $XDG_PICTURES_DIR -type f -mmin -$interval -exec mv -i {} $dest \;
}
# In case there local aliases I'd rather not publish to Github
if [ -f ~/.bash_functions.local ] ; then
. ~/.bash_functions.local
fi
# vim: filetype=bash:ts=2:sw=2:expandtab