/
.functions
296 lines (236 loc) · 8.83 KB
/
.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
# Common
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# Base64 encode a string
function b64() {
echo -n "$*" | base64
}
function b64decode() {
echo -n "$*" | base64 --decode
}
# Usage: crypto <money> <currency>
function crypto() {
curl -s https://api.cryptonator.com/api/ticker/$1-$2 | jq -r '.ticker .price'
}
# Usage: cryptowatch <money> <currency> [<wallet amount> [<alert amount>]]
function cryptowatch() {
latest=0
while true; do
value=$(crypto $1 $2)
current=`printf "%.2f" $(( value ))`
date=$(date "+%H:%M:%S")
operator="${YELLOW}=${NC}"
walletvalue=0
# Find operator
if (( $(echo "$value > $latest" | bc -l) )); then
operator="${GREEN}⬆${NC}"
elif (( $(echo "$value < $latest" | bc -l) )); then
operator="${RED}⬇${NC}"
fi
latest=$value
display="$operator | $date | $1: ${YELLOW}$current${NC}"
if [ -n "$3" ]; then
walletvalue=`printf "%.2f" $(( $3 * $value ))`
display="$display | $2: ${YELLOW}$walletvalue${NC}"
fi
echo $display
# Amount reached? Notify
if [ -n "$4" ] && (( $(echo "$value >= $4" | bc -l) )); then
osascript -e "display notification "$value" with title \"Cryptowatch\" subtitle \"Current value:\""
fi
sleep 15
done
}
# Preview CSVs
function csvpreview() {
sed 's/,,/, ,/g;s/,,/, ,/g' "$@" | column -s, -t | less -#2 -N -S
}
# Usage: encrypt file <output> (default: file.gpg)
function encrypt() {
output="$1.gpg"
if [ -n "$2" ]; then
output="$2"
fi
gpg2 --encrypt --recipient vincent --output $output $1
}
# Usage: decrypt file <output>
function decrypt() {
command="gpg2 --decrypt $1"
if [ -n "$2" ]; then
command="$command > $2"
fi
eval $command
}
# Clones if a Git repository is given, elsewhere just cd
function cdc() {
name=`sed -e 's/.*\///g' <<< $1`
if [[ ! -d "$1" && $1 =~ ^(github|bitbucket|gitlab)\.[a-z]{2,}/.*$ ]]; then
ssh=`sed -e 's/^/git@/' -e 's/$/.git/' -e 's/\//:/' <<< $1`
git clone $ssh;
fi
cd $name;
}
# Determines size of a file or total size of a directory
function fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh;
else
local arg=-sh;
fi
if [[ -n "$@" ]]; then
du $arg -- "$@";
else
du $arg .[^.]* ./*;
fi;
}
# Creates a data URL from a file
function dataurl() {
local mimeType=$(file -b --mime-type "$1");
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8";
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
}
# Returns useful information using dig tool
function diga() {
dig +nocmd "$1" any +multiline +noall +answer;
}
# Returns information about usual subdomains
function dig_in_parallel() {
dig=$(dig +noall +answer $1);
if [ -n "$dig" ]; then
echo "✔ $dig";
fi
}
function digscan() {
set +m;
subdomains=( a ad ads alert alpha adm admin administration analytics api apis api-gateway app apps application assets auth authentication auto autodiscover b back backend beta b2b b2c black blog bo board brown build builds c cache cached cal calendar cdn ceph cli client clients ci cloud cms crm conf config configuration console consul contact contacts content contract contracts corp corporation cpanel cust customer customers d dark dashboard database db dev demo direct directory discover display dl dns doc documentation domain domains download downloads e elasticsearch elb elk endpoint exchange exchanges ext extern external externals extra extranet f factory free files front frontend ftp g gateway geo git gitlab github go golang grafana graph graphs grey green group h ha haproxy health healths help home host hosting hub hubs i info infos int integ integration intra intranet j jenkins jira k kibana k8s kube kubectl kubernetes l light live lb loadbalancer loader logs m mail manager market marketing minio mobile monitor monitoring mongo mongodb movie movies mysql movie movies n nas new news no ns1 ns2 ns3 ns4 ns5 o old open openshift opensource order orders owa p package packages packagist partner partners perso preprod priv private prive portal postgres postgresql pro pros purple q quality qualys r recette red redis reg relay registry remote rule rules root roots s s3 secret secrets secure sale sales serv server servers share shop smtp source sources ssh sso staging stat static statistics stats status storage store stream support svn t test tests time timeline u upload v vault video videos view views vol vols volume volumes vpn w web webdisk webmail white wiki work worker workers www x y yellow yes z zoo )
echo "Checking...\n";
for subdomain in "${subdomains[@]}"; do
dig_in_parallel "$subdomain.$1" &
done
wait
set -m;
}
# Transforms a video file to a gif
# Usage: gify <input file> <output file> <start> <duration> [<ratio> <fps>]
function gify {
ratio=${5:-480:270}
fpm=${6:-60}
ffmpeg -i $1 -vf scale=$ratio -r $fpm -ss $3 -t $4 $2
}
# Usage: lighthouse <url> <json | html>
function lighthouse {
result=`curl -s https://builder-dot-lighthouse-ci.appspot.com/stream\?url=$1\&format=$2 | grep done | cut -d' ' -f3`
if [[ $2 == "json" ]]; then
curl -s $result
else
echo "Report available: $result"
fi
}
# Asks the user to select a network interface and returns the result
function selectnetworkinterface() {
echo "Select the network interface:\n1) Belkin USB-C LAN\n2) Wi-Fi"
read n
case $n in
1) selected="Belkin USB-C LAN";;
2) selected="Wi-Fi";;
*) echo "Invalid option"; return;;
esac
return $selected
}
# Sets Cloudflare DNS servers on a network interface
function opendns() {
selectnetworkinterface
echo "\n-> Setting open DNS servers on network interface '$selected'"
networksetup -setdnsservers $selected 1.1.1.1 1.0.0.1
}
# Sets SSH proxy on a network interface
# Usage: sshproxy <user>@<ip>
# Usage: sshproxy stop
function sshproxy() {
selectnetworkinterface
if [[ $1 == "stop" ]]; then
networksetup -setsocksfirewallproxystate $selected off
ps x | grep "[s]sh -fN -D 5000" | awk '{print $1}' | xargs kill -9
echo "\n-> Done: proxy is stopped and SSH connection is closed"
return
fi
echo "\n-> Setting socks proxy on network interface '$selected'"
ssh -fN -D 5000 $1
networksetup -setsocksfirewallproxy $selected localhost 5000
echo "-> Done: proxy is ready"
}
# Automatically Starting tmux on SSH
function ssht() {
SSH_HOST="${@: -1}"
tabname "$SSH_HOST"
ssh "$@" -t 'tmux ls && exec tmux a || exec tmux new || exec $SHELL -l'
}
# Generates a password and returns hashed values
function genpasswd() {
passwd=`env LC_CTYPE=C tr -dc 'a-zA-Z0-9-_\$\?' < /dev/urandom | head -c 20`;
sha256=`echo $passwd | shasum -a 256`;
sha512=`echo $passwd | shasum -a 512`;
md5=`md5 -s $passwd | cut -d'=' -f2 | xargs`
echo "Raw: $passwd\n\nSHA256: $sha256\nSHA512: $sha512\n\nMD5: $md5";
}
# Gets iTunes current track information
function itunes_title_script() {
osascript -e 'tell application "iTunes" to get the '"$1"' of the current track'
}
function track() {
artist=$(itunes_title_script artist)
name=$(itunes_title_script name)
echo "$artist - $name"
}
# Find files and exec commands at them.
# $ find-exec .coffee cat | wc -l
# # => 9762
function find-exec() {
find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
}
# Count code lines in some directory.
# $ loc py js css
# # => Lines of code for .py: 3781
# # => Lines of code for .js: 3354
# # => Lines of code for .css: 2970
# # => Total lines of code: 10105
function loc() {
local total
local firstletter
local ext
local lines
total=0
for ext in $@; do
firstletter=$(echo $ext | cut -c1-1)
if [[ firstletter != "." ]]; then
ext=".$ext"
fi
lines=`find-exec "*$ext" cat | wc -l`
lines=${lines// /}
total=$(($total + $lines))
echo "Lines of code for ${fg[white]}$ext${reset_color}: ${fg[yellow]}$lines${reset_color}"
done
echo "${fg[white]}Total${reset_color} lines of code: ${fg[yellow]}$total${reset_color}"
}
# Uploads a file to transfer.sh and returns the public URL
# Usage: transfer <file>
transfer() {
if [ $# -eq 0 ]; then
echo -e "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md";
return 1;
fi
tmpfile=$( mktemp -t transferXXX );
if tty -s; then
basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g');
curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" >> $tmpfile;
else
curl --progress-bar --upload-file "-" "https://transfer.sh/$1" >> $tmpfile;
fi
cat $tmpfile;
echo "\n";
rm -f $tmpfile;
}