Browse code

Add existing snippets.

Xavier G authored on20/07/2016 02:45:24
Showing5 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+# Fetch a remote TLS certificate:
2
+function tlscertget {
3
+	local domain="${1}"
4
+	shift
5
+	openssl s_client \
6
+		-connect "${domain}" \
7
+		-servername "${domain}" \
8
+		"$@" < /dev/null 2> /dev/null |\
9
+		sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
10
+}
11
+
12
+# Show (in text form) a certificate passed through stdin; display
13
+# full details (-text) if invoked without any argument. See man x509 for
14
+# possible options.
15
+function tlscertshow {
16
+	if [ $# -eq 0 ]; then
17
+		openssl x509 -noout -text
18
+	else
19
+		openssl x509 -noout "$@"
20
+	fi
21
+}
22
+
23
+# Fetch a remote TLS certificate and display its subject, issuer and dates.
24
+function tlscertcheck {
25
+	local domain="${1}"
26
+	shift
27
+	tlscertget "${domain}" | tlscertshow -subject -issuer -dates "$@"
28
+}
0 29
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+# Override the "cd" keyword with a copy/paste friendly function.
2
+function cd() {
3
+	if [ $# -eq 0 ]; then
4
+		builtin cd
5
+	else
6
+		if [ -f "$1" ]; then
7
+			builtin cd "$(dirname "$1")"
8
+		else
9
+			builtin cd "$1"
10
+		fi
11
+	fi
12
+}
0 13
new file mode 100644
... ...
@@ -0,0 +1,93 @@
1
+# Declare and fill an associative array providing ANSI colors enclosed between
2
+# \[ and \], as per the "PROMPTING" section of man bash:
3
+function declare_colors {
4
+	local ci cj cn
5
+	declare -g -A c
6
+	c['normal']='\[\e[0m\]'
7
+	for cn in {,bright_}{black,red,green,yellow,blue,magenta,cyan,white}; do
8
+		c["${cn}"]='\[\e['"${ci:=0};${cj:=30}"'m\]'
9
+		((++cj,(cj==38)?(cj=30,++ci):1))
10
+	done
11
+}
12
+
13
+# Generate the PS1 variable; doing that in a function allows to reflect almost
14
+# anything in the prompt.
15
+function generate_ps1 {
16
+	# Save the return code value (rcv)
17
+	local rcv=$?
18
+
19
+	# Color for brackets and braces
20
+	local bc="${c['red']}"
21
+
22
+	# Pick the return code color (rcc)
23
+	local rcc='bright_green'
24
+	[ $rcv -ne 0 ] && rcc='bright_red'
25
+
26
+	local rc dstamp tstamp user host cwdir es
27
+	printf -v rc     "%s[%s${rcv}%s]" "${bc}" "${c[${rcc}]}" "${bc}"
28
+	printf -v tstamp '[%s\\t%s]'      "${c['bright_yellow']}" "${bc}"
29
+	printf -v user   '[%s\\u%s]'      "${c['cyan']}" "${bc}"
30
+	printf -v host   '{%s\\h%s}'      "${c['cyan']}" "${bc}"
31
+	printf -v cwdir  '%s\w%s '        "${c['green']}" "${c['normal']}"
32
+	printf -v es     '%s\$%s '        "${c['red']}" "${c['normal']}"
33
+
34
+	if [ "${PS1_DISPLAY_DATE}" ]; then
35
+		printf -v dstamp '[%s\\D{%%F}%s]' "${c['bright_yellow']}" "${bc}"
36
+	fi
37
+
38
+	PS1="${rc}${dstamp}${tstamp}${user}${host}${cwdir}${es}"
39
+
40
+	# Also set PS2 and PS4:
41
+	PS2="${c['bright_green']}"
42
+	PS4="+ ${c['cyan']}[trace]${c['normal']} "
43
+
44
+	# It makes sense to export PS4 (so that e.g. "bash foobar.sh" benefit from
45
+	# it):
46
+	export PS4
47
+}
48
+
49
+# Provide a helper function to swiftly enable or disable the date in the
50
+# prompt:
51
+function ps1_toggle_date {
52
+	if [ "${PS1_DISPLAY_DATE}" ]; then
53
+		unset PS1_DISPLAY_DATE
54
+	else
55
+		PS1_DISPLAY_DATE='yes'
56
+	fi
57
+}
58
+
59
+# Provide a helper function to swiftly enable or disable colors in the
60
+# prompt:
61
+function ps1_toggle_colors {
62
+	if [ "${PS1_USE_COLORS}" ]; then
63
+		unset PS1_USE_COLORS c
64
+	else
65
+		declare_colors
66
+		PS1_USE_COLORS='yes'
67
+	fi
68
+}
69
+
70
+# Provide a helper function to swiftly reduce the prompt to '$ ' -- this is
71
+# useful e.g. for documentation purposes, when one wants to run a few commands
72
+# and copy the whole terminal output without the prompt (which usually reflects
73
+# undesired details such as user and hostname):
74
+function ps1_toggle_prompt {
75
+	local min_prompt='\$ '
76
+	if [ "${PS1}" == "${min_prompt}" ]; then
77
+		PROMPT_COMMAND='generate_ps1'
78
+		generate_ps1
79
+	else
80
+		unset PROMPT_COMMAND
81
+		PS1="${min_prompt}"
82
+	fi
83
+}
84
+
85
+# By default, use colors but do not display the date:
86
+ps1_toggle_colors
87
+#ps1_toggle_date
88
+
89
+# Instruct bash to regenerate PS1 before printing the prompt:
90
+PROMPT_COMMAND='generate_ps1'
91
+
92
+# Generate PS1 at least once:
93
+generate_ps1
0 94
new file mode 100644
... ...
@@ -0,0 +1,14 @@
1
+#!/bin/bash
2
+# Backup files before the resizing operation:
3
+tar czvf "resize-rrd.backup.$(date '+%s').tar.gz" "$@"
4
+
5
+for file in "$@"; do
6
+    # Munin always creates 12 RRAs: resize them all:
7
+    for num in $(seq 0 11); do
8
+        # Adding 9 times the amount of rows is the same as multiplying it by 10:
9
+        new_size=$(rrdtool info "${file}" | perl -nle "\$num=${num};" -e 'print $1*9 if /^rra\[$num\].rows = (.+)$/')
10
+        # Resizing itself:
11
+        rrdtool 'resize' "${file}" "${num}" 'GROW' "${new_size}" || continue
12
+        mv 'resize.rrd' "${file}"
13
+    done
14
+done
0 15
\ No newline at end of file
1 16
new file mode 100644
... ...
@@ -0,0 +1 @@