blob: 4e609893bb942e428f17b7310f597b1262897732 (
plain) (
blame)
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
|
# E.g. after `.mkshrc` is called, perhaps in `~/.mkshrc.local`
# export GOPATH=$HOME/code/gopath
# export GOROOT=$HOME/go/1.13.5
# set_go
set_go() {
export GOPATH="${GOPATH:-$HOME/go}"
export PATH="$GOPATH/bin:$PATH"
GOROOT="${GOROOT:-/usr/local/go}"
_gobin="${GOROOT}/bin"
if [ -e "$_gobin" ] ; then
export PATH="$_gobin:$PATH"
fi
}
_set_editor() {
export EDITOR="$1"
export VISUAL="$1"
alias vi="$EDITOR"
alias vim="$EDITOR"
alias edit="$EDITOR"
alias e="$EDITOR"
}
set_editor() {
for editor in vim vi ed ; do
unalias "$editor" >/dev/null 2>&1
e="$(command -v "$editor")"
if [ -n "$e" ] && [ -x "$e" ] ; then
_set_editor "$e"
fi
break
done
}
set_pager() {
#shellcheck disable=SC2155
export PAGER="$(command -v cat more less 2>/dev/null | tail -n1)"
}
set_lang() {
export CHARSET="$1";
export LANG="$1";
}
# Usage debugstarttls $ipaddr:$port
debugstarttls() {
openssl s_client -starttls smtp -crlf -connect "$1" "$2"
}
randmacgen() {
if [ "$(uname)" = "FreeBSD" ] ; then
LC_COLLATE=c
fi
tr -c -d '0123456789abcdef' < /dev/urandom \
| head -c 12 \
| sed 's!^M$!!;s!\-!!g;s!\.!!g;s!\(..\)!\1:!g;s!:$!!'
echo ""
}
# First argument is the interface on which to scan
ipv6_local_hosts() {
if [ -n "$1" ] ; then
interface="$1"
ping6 -c 3 "ff02::2%$interface" \
| grep 'bytes from' \
| awk '{print $4}' \
| sort \
| uniq \
| sed -e's/:$//'
else
echo "You need to specify an interface"
fi
}
set_nocaps() {
setxkbmap -layout "us,de"
setxkbmap -option ctrl:nocaps
setxkbmap -option shift:breaks_caps
setxkbmap -option numpad:microsoft
setxkbmap -option grp:alt_space_toggle
}
# Usage: dir_oct:file_oct path
setperms() {
_perm="$1"; shift
dir_perms=$(echo "$_perm" | awk -F: '{print $1}')
file_perms=$(echo "$_perm" | awk -F: '{print $2}')
find "$@" -type f -print0 | xargs -0 chmod "$file_perms"
find "$@" -type d -print0 | xargs -0 chmod "$dir_perms"
}
set_title() {
printf $'\033]0;'"%s"$'\007' "$1"
}
set_my_title() {
_load="Load Avg: $(_sys_load)"
_free_mem="M Free: $(_sys_memory)"
_uptime="Uptime: $(_sys_uptime)"
set_title "$(id -un)[$(hostname)] -- $_load $_free_mem $_uptime"
}
_sys_memory() {
! [ -e "$(command -v vmstat)" ] && return
if [ "$(uname)" = "Linux" ] ; then
vmstat | tail -n1 | awk '{print $4/1024}' | sed -e's/\..*$//g'
elif [ "$(uname)" = "FreeBSD" ] ; then
vmstat | tail -n1 | awk '{print $5/1024}' | sed -e's/\..*$//g'
elif [ "$(uname)" = "OpenBSD" ] ; then
vmstat | sed -n '$p' | awk '{print $4}' | sed 's/M$//'
fi
}
_sys_load() {
# Works on BSD too, neat.
uptime | sed -E 's/^.*load averages?: ([0-9]+\.[0-9]+).*$/\1/g'
}
_sys_uptime() {
# Took awhile to figure out, but this works on BSD as well
uptime | sed -E -e's/^.*up[ ]+//g' -e's/^([^,]*),.*/\1/g' -e's/^([^ ]+)[ ]*(.).*/\1\2/g'
}
timestamp() {
date +%m.%d.%y_%H.%M.%S
}
checkSSHAgent() {
ssh_agent_conf="$HOME/.ssh/agent"
if [ -e "$ssh_agent_conf" ] ; then
#shellcheck disable=SC1090
. "$ssh_agent_conf"
fi
#shellcheck disable=SC2009
if ! ps -eo pid | grep -q "$SSH_AGENT_PID" \
|| ! [ -e "$ssh_agent_conf" ] \
|| [ -z "$SSH_AGENT_PID" ] ; \
then
ssh-agent -s | grep -v echo > "$ssh_agent_conf"
#shellcheck disable=SC1090
. "$ssh_agent_conf"
fi
}
passenv() {
_f="$(mktemp)"
pass "$@" > "$_f"
#shellcheck disable=SC1090
. "$_f"
rm -f "$_f"
}
######################################################################
# Tmux utilities
_tmux_session() {
session=""
window_name=""
working_directory="$(pwd)"
while [ $# -gt 0 ] ; do case $1 in
--) shift ; break;;
-s) session="$2" ; shift ; shift ;;
-n) window_name="$2" ; shift ; shift ;;
-w) working_directory="$2" ; shift ; shift ;;
*)echo "Invalid option $1" ; return ;;
esac ; done
cd "$working_directory" || { echo "Cannot change to: $working_directory" ; return; }
tmux new-session -s "$session" -n "$window_name" -c "$working_directory" \;\
"$@"
}
_tmux_dev() {
session=""
working_directory="$(pwd)"
window_name="editor"
while [ $# -gt 0 ] ; do case $1 in
--) shift ; break;;
-s) session="$2" ; shift ; shift ;;
-n) window_name="$2" ; shift ; shift ;;
-w) working_directory="$2" ; shift ; shift ;;
*)echo "Invalid option $1" ; return ;;
esac ; done
cd "$working_directory" || { echo "Cannot change to: $working_directory" ; return; }
_tmux_session -s "$session" -w "$working_directory" -n "$window_name" \
-- \
send-keys -t 0 "$EDITOR" \; send-keys -t 0 Enter \;\
new-window -n "shell" \;\
new-window -n "git" \;\
send-keys -t 0 "git status" \; send-keys -t 0 Enter \;\
"$@"
}
_tmux_servers_split_commands() {
n=0
server_cmds=""
while [ $# -gt 0 ] ; do
if [ $n -eq 0 ] ; then
server_cmds='send-keys "ssh \"'"$1"'\"" \; send-keys Enter \; '
else
server_cmds="${server_cmds} split-window \; "'send-keys "ssh \"'"$1"'\"" \; send-keys Enter \; '
fi
n="$(echo "$n+1" | bc)"
shift
done
echo "$server_cmds"
}
_tmux_servers() {
session=""
working_directory="$HOME"
servers=""
layout="even-vertical"
while [ $# -gt 0 ] ; do case $1 in
--) shift ; break;;
-s) session="$2" ; shift ; shift ;;
-w) working_directory="$2" ; shift ; shift ;;
-servers) servers="$2"; shift ; shift ;;
-l) layout="$2"; shift ; shift ;;
*)echo "Invalid option $1" ; return ;;
esac ; done
cd "$working_directory" || { echo "Cannot change to: $working_directory" ; return; }
layout="select-layout $layout ';' set-window-option synchronize-panes on ';'"
eval _tmux_session -s "\$session" -w "\$working_directory" -n "main" \
-- \
$(_tmux_servers_split_commands $servers) \
"$layout" \
"\$@" \
}
gitdate() {
date=""
while [ $# -gt 0 ] ; do case $1 in
-d) date="$2" ; shift ; shift ;;
*) printf "Bad option: $1... usage:\n$0 -d \"\$date_string\"\n" ; return ;;
esac ; done
date="$(date --date "$date")"
export GIT_AUTHOR_DATE="$date"
export GIT_COMMITTER_DATE="$date"
echo export GIT_AUTHOR_DATE="$date"
echo export GIT_COMMITTER_DATE="$date"
}
gitdateUnset() {
unset GIT_AUTHOR_DATE
unset GIT_COMMITTER_DATE
}
gpgTmpDir() {
set -x
if echo "$GNUPGHOME" | grep -q "/dev/shm/"; then
rm -rvf "$GNUPGHOME"
fi
#shellcheck disable=SC2155
export GNUPGHOME="$(mktemp -d /dev/shm/GPG.XXXXXX)"
set +x
}
|