-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic-mole.sh
executable file
·214 lines (190 loc) · 6.23 KB
/
magic-mole.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
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
#!/bin/bash
input="example-tunnels.csv"
# Setup global vars
headerPrinted=false
foundTunnel=false
# Make sure the script exits cleanly in failure
set -e
# Prints out how to use the tool
function usage {
echo 'Usage: [command] [name]'
echo ' 1: The command to perform on the tunnel (eg. start/stop/restart/status)'
echo ' 2: (Optional) The name of the tunnel (eg. tunnel-name) or leave empty for all'
exit 0
}
# Parse options for help (-h)
while getopts ':h' opt; do
case ${opt} in
h)
usage
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
# Check the subcommand is valid
subcommand=$1
case "$subcommand" in
start)
command='start'
;;
up)
command='start'
;;
stop)
command='stop'
;;
down)
command='stop'
;;
restart)
command='restart'
;;
status)
command='status'
;;
ls)
command='status'
;;
*)
echo 'Command not found: $subcommand'
usage
exit 1
esac
# Checks if a tunnel is currently running
function isTunnelRunning {
local processCount="$(ps -ef | grep ${1}:${2}:${3} | grep -v grep | awk '{print $2}' | wc -l | awk '{print $1}')"
if [ "$processCount" -ge 1 ]; then
echo 'true'
else
echo 'false'
fi
}
# Prints the table header
function printHeader {
headerPrinted=true
echo -e " _____\n \'_ _'/ ・:*:・゚★,。・:*:・゚☆。・:*:・゚★,。・:*:・゚☆\n |(>)-(<)| ✧・゚: *✧・゚:* MAGIC MOLE *:・゚✧*:・゚✧\n ../ ' O ' \.. ・:*:・゚★,。・:*:・゚☆。・:*:・゚★,。・:*:・゚☆\n''(((:-.,_,.-:)))''\n"
printf "%-25s \e[95m|\e[39m %-30s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-25s \e[95m|\e[39m %-7s" 'Tunnel Name' 'Tags' 'IP' 'Local Port' 'Remote Port' 'Bastion' 'Status'
printf "\n\e[95m--------------------------+--------------------------------+-----------------+-----------------+-----------------+---------------------------+----------\e[39m\n"
}
# Pretty prints a row in the table
function prettyPrint {
# Print table header if it's not there
if [ "$headerPrinted" = 'false' ]; then
printHeader
fi
# Check the status of the tunnel and colour the status text accordingly
if [ $7 = "Up" ]; then
printf "%-25s \e[95m|\e[39m %-30s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-25s \e[95m|\e[39m \e[92m%-7s\e[39m" "$1" "$2" "$3" "$4" "$5" "$6" "$7"
elif [ $7 = "Down" ]; then
printf "%-25s \e[95m|\e[39m %-30s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-25s \e[95m|\e[39m \e[91m%-7s\e[39m" "$1" "$2" "$3" "$4" "$5" "$6" "$7"
elif [ $7 = "Starting" ] || [ $7 = "Restarting" ] || [ $7 = "Stopping" ]; then
printf "%-25s \e[95m|\e[39m %-30s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-25s \e[95m|\e[39m \e[93m%-7s\e[39m" "$1" "$2" "$3" "$4" "$5" "$6" "$7"
else
printf "%-25s \e[95m|\e[39m %-30s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-15s \e[95m|\e[39m %-25s \e[95m|\e[39m \e[93m%-7s\e[39m" "$1" "$2" "$3" "$4" "$5" "$6" "$7"
fi
printf "\n"
}
# Gets the status of a given tunnel
function getStatus {
# Check if any processes are running for this tunnel
local isRunning=$(isTunnelRunning "$4" "$3" "$5")
if [ $isRunning = "true" ]; then
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Up'
else
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Down'
fi
}
# Starts a tunnel if it's not currently running
function startTunnel {
# Find if any processes are running for this tunnel already
local isRunning=$(isTunnelRunning "$4" "$3" "$5")
if [ "$isRunning" = "true" ]; then
# Tunnel is already up, so don't start it
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Up'
else
# Tunnel is down so start it up
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Starting'
# Start the tunnel in the background
startTunnelProcess "$4" "$3" "$5" "$6"
fi
}
# Stops a tunnel if it's currently running
function stopTunnel {
# Find if any processes are running
local isRunning=$(isTunnelRunning "$4" "$3" "$5")
if [ "$isRunning" = "false" ]; then
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Down'
else
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Stopping'
# Kill current tunnel
killTunnelProcesses "$4" "$3" "$5"
fi
}
# Restarts a tunnel only if it's currently running
function restartTunnel {
# Find if any processes are running
local isRunning=$(isTunnelRunning "$4" "$3" "$5")
if [ $isRunning = "false" ]; then
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Down'
else
prettyPrint "$1" "$2" "$3" "$4" "$5" "$6" 'Restarting'
# Kill current tunnel
killTunnelProcesses "$4" "$3" "$5"
# Start the tunnel in the background
startTunnelProcess "$4" "$3" "$5" "$6"
fi
}
# Starts autossh process for a tunnel
function startTunnelProcess {
autossh -f -M 0 -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -NL ${1}:${2}:${3} "$4"
}
# Kills any processes currently running for a tunnel
function killTunnelProcesses {
kill $(ps -ef | grep ${1}:${2}:${3} | grep /bin/ssh | grep -v grep | awk '{print $2 " " $3}')
}
# Searches and extracts matching tunnels from spreadsheet
function findTunnelsFromSpreadsheet {
# Check if the user has specified all
if [ "$1" = '' ]; then
# If all then result is every record except the spreadsheet header
tail -n+2 $input
else
# Otherwise look for records that contain name or tag matching params
local tunnelCount=0
local awkString=''
for tag in "$@"
do
if [ "$tunnelCount" = 0 ]; then
awkString="/(${tag} | ${tag}|,${tag},|^${tag},)/"
else
awkString="${awkString} && /(${tag} | ${tag}|,${tag},|^${tag},)/"
fi
((tunnelCount++))
done
cat $input | awk "${awkString}"
fi
}
# Main script controller
tunnels=$(findTunnelsFromSpreadsheet ${@:2})
# If no tunnel was found then let the user know
if [ ${#tunnels} = 0 ]; then
echo "No tunnel found with tags: ${@:2}"
else
# Loop over each row in spreadsheet
echo -e "$tunnels" | while IFS=',' read -r f1 f2 f3 f4 f5 f6
do
if [ $command = "start" ]; then
startTunnel "$f1" "$f2" "$f3" "$f4" "$f5" "$f6"
elif [ $command = "stop" ]; then
stopTunnel "$f1" "$f2" "$f3" "$f4" "$f5" "$f6"
elif [ $command = "restart" ]; then
restartTunnel "$f1" "$f2" "$f3" "$f4" "$f5" "$f6"
elif [ $command = "status" ]; then
getStatus "$f1" "$f2" "$f3" "$f4" "$f5" "$f6"
fi
done
fi
exit 0