forked from compunet-jimmy/nts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall_nts.sh
More file actions
executable file
·206 lines (182 loc) · 5.94 KB
/
uninstall_nts.sh
File metadata and controls
executable file
·206 lines (182 loc) · 5.94 KB
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
#!/bin/bash
# uninstall_nts.sh
#
# This script is a tool used in testing to reset the server to a base level.
# The script can be run with either the 'full' or 'nts' parameters.
# Example: ./uninstall_nts.sh full # Uninstalls NTS and Docker
# Without parameters, script prompts user for uninstallation type, with
# default being NTS services.
# Pretty Colors
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Check if the script is being run as root or as sudo
if [ "$EUID" -ne 0 ]; then
echo
echo -e "${RED}This script must be run as root or with sudo. Exiting." >&2
echo
exit 1
fi
# Parameter Input
# Check if no parameters were entered
if [ $# -eq 0 ]; then
cleanup_option="default"
# Check the number of parameters, bail if more than 1
elif [ "$#" -gt 1 ]; then
echo "Error: More than one parameter provided."
exit 1
else
# Parameters entered, use first parameter
cleanup_option=$1
# Convert input to lowercase
cleanup_option=$(echo "$cleanup_option" | tr '[:upper:]' '[:lower:]')
fi
# Echo function for printing primary demarcation lines on screen
echo_primary () {
message=$1
echo
echo -e "${BLUE}---------------------------------------------------------------------${NC}"
echo
echo $message
echo
}
# Echo function for printing secondary demarcation lines on screen
echo_secondary () {
message=$1
echo
echo -e "${GREEN}----------------------------------------------------------${NC}"
echo
echo $message
echo
}
# NTS Cleanup
nts_cleanup () {
# Docker Cleanup
echo_secondary "Cleaning up Docker containers..."
docker ps -a --filter "name=netdisco" --format "{{.ID}}" | xargs -I {} sh -c 'docker stop {}; docker rm {}'
docker ps -a --filter "name=librespeed" --format "{{.ID}}" | xargs -I {} sh -c 'docker stop {}; docker rm {}'
docker ps -a --filter "name=librenms" --format "{{.ID}}" | xargs -I {} sh -c 'docker stop {}; docker rm {}'
docker ps -a --filter "name=oxidized" --format "{{.ID}}" | xargs -I {} sh -c 'docker stop {}; docker rm {}'
docker ps -a --filter "name=nginx" --format "{{.ID}}" | xargs -I {} sh -c 'docker stop {}; docker rm {}'
# Removing system variables
echo_secondary "Removing any system variables..."
# Clear NTS_PASSWORD
sed -i "/NTS_PASSWORD=/d" /etc/environment
source /etc/environment
# Server group cleanup
echo_secondary "Cleaning server groups..."
# List of groups to delete
nts_groups=('netdisco','wireshark')
# Loop through patterns and delete matching groups
for pattern in "${patterns[@]}"; do
groupdel $pattern
done
# Server service account cleanup
echo_secondary "Cleaning server service accounts..."
userdel netdisco
# Removing NTS directory
echo_secondary "Removing NTS directory..."
rm -rf /opt/nts
# Removing server apps
echo_secondary "Removing previously installed dependencies and CLI tools..."
apt autoremove --purge -y apache2-utils hping3 tshark iperf3 iftop nmap net-tools sqlite3
apt autoclean
# Removing and disabling firewall rules
echo_secondary "Disabling and removing NTS firewall rules..."
HOST_IP=$(hostname -I | awk '{print $1}')
ufw disable
ufw delete allow 80/tcp
ufw delete allow 443/tcp
ufw delete allow 514/tcp
ufw delete allow 514/udp
ufw delete allow 5000/tcp
ufw delete allow 8000/tcp
ufw delete allow 8080/tcp
ufw delete allow 8888/tcp
ufw delete allow from $HOST_IP to any port 8888 proto tcp
ufw delete allow in on lo to any port 8888 proto tcp
}
# Docker Uninstall
docker_uninstall () {
echo_secondary "Removing Docker and related files/directories..."
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images -q)
apt-get autoremove -y --purge docker-ce docker-ce-cli
apt-get autoclean
rm -rf /var/lib/docker /etc/docker
rm -rf /var/run/docker.sock
rm -rf /etc/systemd/system/docker.service.d
rm -rf /etc/systemd/system/docker.service
rm -rf /etc/systemd/system/docker.socket
rm -rf /usr/lib/systemd/system/docker.service
rm -rf /usr/lib/systemd/system/docker.socket
rm -rf docker-install
while true; do
echo -e "${YELLOW}"
read -p "Uninstaller needs to reboot. Reboot computer? (Y/n): " answer
echo -e "${NC}"
# If the user just presses Enter, default to 'y'
answer=${answer:-y}
# Convert input to lowercase
answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
# Check the user's response
if [[ "$answer" == "y" || "$answer" == "yes" ]]; then
reboot now
break
elif [[ "$answer" == "n" || "$answer" == "no" ]]; then
echo "Exiting script."
exit 0
fi
done
}
cleanup_options () {
# Print the cleanup message
echo
echo
echo -e "${BLUE}| "
echo -e "${BLUE}| "
echo -e "${BLUE}| ${YELLOW}NTS Cleanup"
echo -e "${BLUE}| "
echo -e "${BLUE}| "
echo -e "${BLUE}| ${YELLOW}Please select a cleanup option"
echo -e "${BLUE}| "
echo -e "${BLUE}| ${YELLOW}1. NTS Services and Dependecies Cleanup"
echo -e "${BLUE}| ${YELLOW}2. Full Service Cleanup (NTS and Docker)"
echo -e "${BLUE}| "
echo -e "${BLUE}| ${NC}"
echo
read -p "Cleanup Option [Default: 1]: " answer
echo
# If the user just presses Enter, default to 'y'
answer=${answer:-1}
# Check the user's response
if [[ "$answer" == 1 ]]; then
echo_primary "Cleaning up NTS services and depdencies only..."
nts_cleanup
elif [[ "$answer" == 2 ]]; then
echo_primary "Performing FULL Docker and NTS cleanup..."
nts_cleanup
docker_uninstall
else
echo "Invalid input. Exiting script."
exit 1
fi
}
# Check the user's response
if [[ "$cleanup_option" == "nts" ]]; then
echo_primary "Cleaning up NTS services and depdencies only..."
nts_cleanup
elif [[ "$cleanup_option" == "full" ]]; then
echo_primary "Performing FULL Docker and NTS cleanup..."
nts_cleanup
docker_uninstall
elif [[ "$cleanup_option" == "default" ]]; then
cleanup_options
else
echo "Invalid input. Exiting script."
exit 1
fi