-
Notifications
You must be signed in to change notification settings - Fork 1
/
patronus.sh
executable file
·93 lines (80 loc) · 2.1 KB
/
patronus.sh
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
#!/usr/bin/env bash
#
# patronus.sh
#
# Dispel ghost logins that suck the joy out of your active terminal session
#
# USAGE
#
# ./patronus.sh
#
# To run with debugging information enabled:
#
# DEBUG=true ./patronus.sh
#
# DISCLAIMER
#
# This is a parody of J.K. Rowling's Harry Potter novels.
# Lawyers: please don't sue this poor house-elf who is only trying to help.
#
# Many thanks (and aplologies) to J.K. Rowling, who has inspired us all.
#
# LEGAL
#
# Copyright (C) 2007 Richard Bullington-McGuire
# Copyright (C) 2019 by The Obscure Organization
# MIT licensed. See the LICENSE file for details.
#
# Release History:
#
# 1.0 (September 6, 2007)
# First public release
#
# 1.0.1 (May 19, 2019)
# Relicensed under the MIT license
#
# 1.0.2 (May 19, 2019)
# Lint fixes: now ShellCheck clean (see https://www.shellcheck.net)
#
# 1.0.3 (December 10, 2019)
# Improved documentation
# Set unofficial bash strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
DEBUG=${DEBUG:-false}
# Thanks https://stackoverflow.com/a/17805088
$DEBUG && export PS4='${LINENO}: ' && set -x
# Thanks http://redsymbol.net/articles/bash-exit-traps/
function finish {
rm -rf "$FLOO_NETWORK" "$MAGICAL_CREATURES"
}
trap finish EXIT
MAGICAL_CREATURES=$(mktemp -t patronus.XXXXXXXXXX)
FLOO_NETWORK=$(mktemp -t patronus.XXXXXXXXXX)
DEMENTORS=""
printf "Lumos!\n"
ps x > "$MAGICAL_CREATURES"
FLOO_EXITS=$(grep -E " *$$" "$MAGICAL_CREATURES" | \
awk '{print $2}')
if awk '!/ PID/{print $2}' < "$MAGICAL_CREATURES" | \
sort -u |
grep -E -v "^\?|$FLOO_EXITS" > "$FLOO_NETWORK"; then
DEMENTORS=$(cat "$FLOO_NETWORK")
fi
echo "$DEMENTORS"
if [ -z "$DEMENTORS" ] ; then
echo "No dementors spotted nearby."
else
DEMENTOR_KISSES=$(grep -f "$FLOO_NETWORK" < "$MAGICAL_CREATURES" | \
awk '{print $1}')
DEMENTOR_DESCRIPTION=$(ps u "$DEMENTOR_KISSES")
cat << EOT
Traversing floo network at $FLOO_EXITS, dementors spotted at:
$DEMENTORS
Specialis Revelio!
Dementors found:
$DEMENTOR_DESCRIPTION
EXPECTO PATRONUM!
EOT
kill -HUP "$DEMENTOR_KISSES" 2>/dev/null
fi