/
.bash_aliases
295 lines (249 loc) · 9.1 KB
/
.bash_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
alias dot-local='cd ~/.dotfiles'
alias dot-codespace='cd /workspaces/.codespaces/.persistedshare/dotfiles'
if [ -d .git ]; then
git config pull.rebase false
fi
transfer() {
local file
declare -a file_array
file_array=("${@}")
if [[ "${file_array[@]}" == "" || "${1}" == "--help" || "${1}" == "-h" ]]
then
echo "${0} - Upload arbitrary files to \"transfer.sh\"."
echo ""
echo "Usage: ${0} [options] [<file>]..."
echo ""
echo "OPTIONS:"
echo " -h, --help"
echo " show this message"
echo ""
echo "EXAMPLES:"
echo " Upload a single file from the current working directory:"
echo " ${0} \"image.img\""
echo ""
echo " Upload multiple files from the current working directory:"
echo " ${0} \"image.img\" \"image2.img\""
echo ""
echo " Upload a file from a different directory:"
echo " ${0} \"/tmp/some_file\""
echo ""
echo " Upload all files from the current working directory. Be aware of the webserver's rate limiting!:"
echo " ${0} *"
echo ""
echo " Upload a single file from the current working directory and filter out the delete token and download link:"
echo " ${0} \"image.img\" | awk --field-separator=\": \" '/Delete token:/ { print \$2 } /Download link:/ { print \$2 }'"
echo ""
echo " Show help text from \"transfer.sh\":"
echo " curl --request GET \"https://transfer.sh\""
return 0
else
for file in "${file_array[@]}"
do
if [[ ! -f "${file}" ]]
then
echo -e "\e[01;31m'${file}' could not be found or is not a file.\e[0m" >&2
return 1
fi
done
unset file
fi
local upload_files
local curl_output
local awk_output
du --total --block-size="K" --dereference "${file_array[@]}" >&2
# be compatible with "bash"
if [[ "${ZSH_NAME}" == "zsh" ]]
then
read $'upload_files?\e[01;31mDo you really want to upload the above files ('"${#file_array[@]}"$') to "transfer.sh"? (Y/n): \e[0m'
elif [[ "${BASH}" == *"bash"* ]]
then
read -p $'\e[01;31mDo you really want to upload the above files ('"${#file_array[@]}"$') to "transfer.sh"? (Y/n): \e[0m' upload_files
fi
case "${upload_files:-y}" in
"y"|"Y")
# for the sake of the progress bar, execute "curl" for each file.
# the parameters "--include" and "--form" will suppress the progress bar.
for file in "${file_array[@]}"
do
# show delete link and filter out the delete token from the response header after upload.
# it is important to save "curl's" "stdout" via a subshell to a variable or redirect it to another command,
# which just redirects to "stdout" in order to have a sane output afterwards.
# the progress bar is redirected to "stderr" and is only displayed,
# if "stdout" is redirected to something; e.g. ">/dev/null", "tee /dev/null" or "| <some_command>".
# the response header is redirected to "stdout", so redirecting "stdout" to "/dev/null" does not make any sense.
# redirecting "curl's" "stderr" to "stdout" ("2>&1") will suppress the progress bar.
curl_output=$(curl --request PUT --progress-bar --dump-header - --upload-file "${file}" "https://transfer.sh/")
awk_output=$(awk \
'gsub("\r", "", $0) && tolower($1) ~ /x-url-delete/ \
{
delete_link=$2;
print "Delete command: curl --request DELETE " "\""delete_link"\"";
gsub(".*/", "", delete_link);
delete_token=delete_link;
print "Delete token: " delete_token;
}
END{
print "Download link: " $0;
}' <<< "${curl_output}")
# return the results via "stdout", "awk" does not do this for some reason.
echo -e "${awk_output}\n"
# avoid rate limiting as much as possible; nginx: too many requests.
if (( ${#file_array[@]} > 4 ))
then
sleep 5
fi
done
;;
"n"|"N")
return 1
;;
*)
echo -e "\e[01;31mWrong input: '${upload_files}'.\e[0m" >&2
return 1
esac
}
transfer-enc() {
cat $1 | gpg -ac -o- | curl -X PUT --upload-file "-" https://transfer.sh/$(basename $1)
}
addcs() {
git add "*.cs"
}
addExt() {
git add "*.$1"
}
ipaddr() {
ip=$(curl --silent ifconfig.io)
echo $ip
echo $ip | pbcopy
}
yankcommit() {
git rebase -p --onto $1^ $1
}
pbcat() {
cat $1 | pbcopy
}
# Removes ^M windows carriage return character from a given file
removeCR() {
sed -i -e 's/\r$//' $1
}
# Scans for bluetooth low energy from the given MAC address:
gattMAC() {
ssh ubuntu01 gatttool -b $1 --characteristics
}
# Analyze traffic on network
mitm_router() {
ssh router tcpdump -i eth0 -U -s0 -w - 'not port 22' | wireshark -k -i -
}
mitm_proxy_enable() {
networksetup -setwebproxy wi-fi localhost 8080
networksetup -setwebproxystate wi-fi on
networksetup -setsecurewebproxy wi-fi localhost 8080
networksetup -setsecurewebproxystate wi-fi on
mitmproxy
}
mitm_proxy_disable() {
networksetup -setwebproxystate wi-fi off
networksetup -setsecurewebproxystate wi-fi off
}
function socat-sendfile() {
# $1 = file_path
# $2 = remote_server_with_port
socat -u FILE:$1 TCP:$2
}
function socat-recvfile() {
# $1 = port_to_listen_on
# $2 = output_file_name
socat -u TCP-LISTEN:$1,reuseaddr OPEN:$2,creat
}
function socat-listen() {
if [ "$#" -ne 1 ]; then
echo "[-] Usage: ./socat-listen.sh <10000:10050>"
return 1
fi
socat file:`tty`,raw,echo=0 tcp-listen:$1
}
function socat-connect() {
if [ "$#" -ne 2 ]; then
echo "[-] Usage: ./socat-connect.sh <IP> <PORT>"
return 1
fi
socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:$IP:$PORT
}
function dockershellhere() {
dirname=${PWD##*/}
docker run --rm -it --entrypoint=/bin/bash -v `pwd`:/${dirname} -w /${dirname} "$@"
}
function mcr() {
dockershellhere mcr.microsoft.com/devcontainers/base
}
function dockershellshhere() {
dirname=${PWD##*/}
docker run --rm -it --entrypoint=/bin/sh -v `pwd`:/${dirname} -w /${dirname} "$@"
}
# Search git history for the removal of a given string ($1)
function pickaxe() {
git log -S $1
}
function brew-installed() {
brew leaves | xargs -n1 brew desc
}
json_escape () {
printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
}
json_unescape() {
echo "$1" | jq '. | fromjson'
}
# Given a URL, follow redirects and print what the final URL is (don't actually fetch)
follow_redirects() {
curl $1 -s -L -I -o /dev/null -w '%{url_effective}'
}
alias utc="date -u"
alias cls="clear"
alias gs="git status"
alias gd="git diff"
alias difffiles="git diff master HEAD --compact-summary"
alias gpb='git push origin $(git branch --show-current)'
alias githash="git rev-parse --short HEAD"
alias plz='sudo $(fc -ln -1)'
alias pull='git pull origin $(git branch --show-current)'
alias gitscrub='git clean -xdf'
alias git-repair-gitignore='git rm --cached `git ls-files -i --exclude-from=.gitignore`'
alias diff-open="git diff --name-only | xargs $EDITOR"
alias changed="git log --name-only"
alias ducks='du -cks * | sort -rn | head -n 10'
alias epoch='date +%s%3N'
alias clear-scrollback="clear && printf '\e[3J'"
# Codespaces
function new-cs () {
NAME=$(gh api -X POST repos/$1/codespaces -F 'location=WestUs2' -F 'vscs_target=ppe' | jq -r .name)
echo $NAME
gh cs ssh -c $NAME
}
function new-cs-cxc () {
NAME=$(gh api -X POST repos/github/codespaces-service/codespaces -F 'location=WestUs2' -F 'vscs_target=ppe' -F 'machine=largePremiumLinux' | jq -r .name)
echo $NAME
gh cs ssh -c $NAME
}
function new-cs-dev-test () {
NAME=$(gh api -X POST repos/joshspicer/almost-empty/codespaces -F 'location=WestUs2' -F 'vscs_target=development' | jq -r .name)
echo $NAME
gh cs ssh -c $NAME
}
# Dev Containers
function inspect-dev-container-metadata-from-image() {
docker image inspect "$1" | jq '.[0].Config.Labels."devcontainer.metadata" | fromjson'
}
# Docker
alias dockershell="docker run --rm -i -t --entrypoint=/bin/bash"
alias dockershellsh="docker run --rm -i -t --entrypoint=/bin/sh"
alias rm-imgs='docker rmi -f $(docker images -a -q)'
alias rm-containers-force='docker ps -q | xargs docker rm -f'
alias rm-docker-nuclear="rm-containers-force || : && rm-imgs || : && docker system prune || : && docker builder prune || :"
alias cdnewest='cd $(ls -1tr | tail -n 1)'
alias n='cdnewest'
alias yt-dl='docker run \
--rm -i \
-e PGID=$(id -g) \
-e PUID=$(id -u) \
-v "$(pwd)":/workdir:rw \
mikenye/youtube-dl'