-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKPing.sh
More file actions
283 lines (267 loc) · 10.1 KB
/
Copy pathKPing.sh
File metadata and controls
283 lines (267 loc) · 10.1 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
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
#!/bin/bash
YEL=$'\e[1;33m' # Yellow
RED=$'\033[0;31m' # Red
NC=$'\033[0m' # No Color
PRPL=$'\033[1;35m' # Purple
GRN=$'\e[1;32m' # Green
BLUE=$'\e[3;49;34m' # Blue
#script logo with copyrights
printf "${BLUE}\n"
echo "██╗ ██╗██████╗ ██╗███╗ ██╗ ██████╗ "
echo "██║ ██╔╝██╔══██╗██║████╗ ██║██╔════╝"
echo "█████╔╝ ██████╔╝██║██╔██╗ ██║██║ ███╗"
echo "██╔═██╗ ██╔═══╝ ██║██║╚██╗██║██║ ██║"
echo "██║ ██╗██║ ██║██║ ╚████║╚██████╔╝"
echo "╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ "
printf "\nPowered by KeepSec Technologies Inc.™\n"
printf "${NC}\n"
#check if root or not
if [ $(id -u) -ne 0 ]; then
printf "${RED}\nThis script can only be executed as root\n\n${NC}"
sleep 0.5
exit
fi
sleep 1
printf "\n"
#make directories and variables for it
mkdir /etc/KPing &>/dev/null
mkdir /etc/KPing/logs &>/dev/null
mkdir /etc/KPing/configs &>/dev/null
dirConf="/etc/KPing/configs"
dirLogs="/etc/KPing/logs"
#variables for number of questions
num=1
#variables of quotes to output secondary scripts well
sqt="'"
dqt='"'
#where configs are localted
config=($dirConf/kping-*)
#function to ask if you want to delete previous configs
function delConf {
read -n 1 -p "${YEL}Delete${NC} previous KPing configurations? (Y/N) " ynDEL
printf "\n\nThose configurations will be ${RED}deleted${NC}:\n\n"
ls -A1 $dirConf | awk -F 'kping-|-job' {'print $2'}
case $ynDEL in
[yY])
printf "\n"
read -n 1 -p "${RED}Are you ${RED}sure?${NC} (Y/N) " ynDEL2
printf "\n"
;;
[nN])
printf "\n"
;;
*)
printf "${RED}\n\nAnswer 'Y' or 'N', try again\n\n${NC}"
sleep 0.5
delConf
;;
esac
case $ynDEL2 in
[yY])
printf "\n"
;;
[nN])
printf "\n"
delConf
;;
*)
printf "${RED}\nAnswer 'Y' or 'N', try again\n\n${NC}"
sleep 0.5
delConf
;;
esac
}
#function to ask if you want to curl the hosts or not
function askCurl {
read -n 1 -p "Do you want the script to also ${YEL}curl${NC} the hosts? (Y/N) " ynCURL
case $ynCURL in
[yY])
printf "\n\n"
;;
[nN])
printf "\n\n"
;;
*)
printf "${RED}\nAnswer 'Y' or 'N', try again\n\n${NC}"
sleep 0.5
askCurl
;;
esac
}
#function for curl confirmation
function CURLconfirmation {
if [[ $ynCURL == Y || $ynCURL == y ]]; then
read -n 1 -p "Curl alone? (Y/N) " CURLconfirm
case $CURLconfirm in
[yY])
printf "\n\n"
;;
[nN])
printf "\n\n"
;;
*)
printf "${RED}\nAnswer 'Y' or 'N', try again\n\n${NC}"
sleep 0.5
CURLconfirmation
;;
esac
fi
}
#function to ask frequence of checking the hosts (min 1 minute, max 60 minutes)
function askFreq {
read -n 2 -p "How often in ${YEL}minutes${NC} do you want the cron job to run (1-60) : " cron
case $cron in
*)
if [[ $cron -gt 0 && $cron -lt 61 ]]; then
printf "\n"
else
printf "${RED}\nOnly numbers between 1 and 60, try again\n\n${NC}"
sleep 0.5
askFreq
fi
;;
esac
}
#function to ask for another monitor or not
function askMonit {
read -n 1 -p "Do you want another ping (${YEL}${num}${NC})? (Y/N) " yn
case $yn in
[yY])
printf "\n\n"
;;
[nN])
printf "\n\n"
return 1
;;
*)
printf "${RED}\nAnswer 'Y' or 'N', try again\n\n${NC}"
sleep 0.5
askMonit
;;
esac
}
#check to see if configuration already present and run delConf function if it is
if [ -f "${config[0]}" ]; then
delConf
if [[ $ynDEL2 == Y || $ynDEL2 == y ]]; then
rm -f /etc/cron.d/kping-*
rm -f $dirConf/kping-*
fi
fi
#start of series of questions
askCurl
CURLconfirmation
askFreq
#email address to receive the notifs
read -p "What is the ${YEL}email${NC} address that you want to receive your notifications : " to
echo""
#internet domain pointing to your server to send emails
read -p "What is the ${YEL}domain${NC} that will be used to send emails : " domain
echo""
printf "${GRN}Assure yourself that the domain is pointing to the IP of your server${NC}\n"
echo""
sleep 1
#loop for infinite questions to add as many hosts as you want
while :; do
read -p "What is the (${YEL}${num}${NC}) IP or website you want to get notifications for : " pingedloop
echo""
#PING-ONLY--------------------------------------
if [[ $ynCURL == N || $ynCURL == n ]]; then
echo -e "#!/bin/bash
subject=${dqt}HOST DOWN: $pingedloop${dqt}
status=${dqt}\$(ping -c 4 $pingedloop && curl $pingedloop 2>&1)${dqt}
status_text=\$(echo ${dqt}\${status}${dqt} | grep -o ${sqt}100% packet loss${sqt})
if [[ ${dqt}\${status_text}${dqt} == ${dqt}100% packet loss${dqt} ]]; then
printf ${dqt}The host ${sqt}$pingedloop${sqt} is currently down!\n\n Please check it out as soon as possible.${dqt} | mail -r ${dqt}notification${dqt} -s ${dqt}\$subject${dqt} ${dqt}$to${dqt}
fi" >$dirConf/kping-$pingedloop-job.sh
croncmd="root /usr/bin/bash $dirConf/kping-$pingedloop-job.sh >> $dirLogs/kping-$pingedloop.log"
cronjob="*/$cron * * * * $croncmd"
printf "$cronjob\n" >"/etc/cron.d/kping-$pingedloop-job"
#CURL-WITH-PING--------------------------------------------------------
elif [[ $CURLconfirm == N || $CURLconfirm == n ]]; then
echo -e "#!/bin/bash
subject=${dqt}HOST DOWN: $pingedloop${dqt}
status=${dqt}\$(ping -c 4 $pingedloop && curl $pingedloop 2>&1)${dqt}
status_text=\$(echo ${dqt}\${status}${dqt} | grep -o ${sqt}100% packet loss${sqt})
status_textCURL=\$(echo ${dqt}\${status}${dqt} | grep -o ${sqt}100% packet loss\|Connection refused${sqt})
if [[ ${dqt}\${status_text}${dqt} == ${dqt}100% packet loss${dqt} ]] || [[ ${dqt}\${status_textCURL}${dqt} == ${dqt}Connection refused${dqt} ]]; then
printf ${dqt}The host ${sqt}$pingedloop${sqt} is currently down!\n\n Please check it out as soon as possible.${dqt} | mail -r ${dqt}notification${dqt} -s ${dqt}\$subject${dqt} ${dqt}$to${dqt}
fi" >$dirConf/kping-$pingedloop-job.sh
croncmd="root /usr/bin/bash $dirConf/kping-$pingedloop-job.sh >> $dirLogs/kping-$pingedloop.log"
cronjob="*/$cron * * * * $croncmd"
printf "$cronjob\n" >"/etc/cron.d/kping-$pingedloop-job"
#CURL-ONLY-------------------------------------------------------
elif [[ $CURLconfirm == Y || $CURLconfirm == y ]]; then
echo -e "#!/bin/bash
subject=${dqt}HOST DOWN: $pingedloop${dqt}
status=${dqt}\$(curl $pingedloop 2>&1)${dqt}
status_textCURL=\$(echo ${dqt}\${status}${dqt} | grep -o ${sqt}100% packet loss\|Connection refused${sqt})
if [[ ${dqt}\${status_textCURL}${dqt} == ${dqt}Connection refused${dqt} ]]; then
printf ${dqt}The website ${sqt}$pingedloop${sqt} is currently down!\n\n Please check it out as soon as possible.${dqt} | mail -r ${dqt}notification${dqt} -s ${dqt}\$subject${dqt} ${dqt}$to${dqt}
fi" >$dirConf/kping-$pingedloop-job.sh
croncmd="root /usr/bin/bash $dirConf/kping-$pingedloop-job.sh >> $dirLogs/kping-$pingedloop.log"
cronjob="*/$cron * * * * $croncmd"
printf "$cronjob\n" >"/etc/cron.d/kping-$pingedloop-job"
fi
num=$((num + 1))
askMonit || break
done
#function for the installing wheel
function installing {
tput civis
spinner="⣾⣽⣻⢿⡿⣟⣯⣷"
while :; do
for i in $(seq 0 7); do
printf "${PRPL}${spinner:$i:1}"
printf "\010${NC}"
sleep 0.2
done
done
}
#executes function above
installing &
SPIN_PID=$!
disown
printf "${PRPL}\nInstalling utilities ➜ ${NC}"
#add notification user
sudo adduser --disabled-password --gecos "" notification &>/dev/null
#checks package manager and then install postfix with your right package manager + puts the right configuration in the config file for you if you don't already have postfix installed
if [ -n "$(command -v apt-get)" ] && [ -z "$(command -v postfix)" ]; then
sudo apt-get -y install postfix >/dev/null
sudo echo "postfix postfix/mailname string $domain" | debconf-set-selections
sudo echo "postfix postfix/protocols select all" | debconf-set-selections
sudo echo "postfix postfix/mynetworks string 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" | debconf-set-selections
sudo echo "postfix postfix/mailbox_limit string 0" | debconf-set-selections
sudo echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
sudo echo "postfix postfix/compat_conversion_warning boolean true" | debconf-set-selections
sudo echo "postfix postfix/protocols select all" | debconf-set-selections
sudo echo "postfix postfix/procmail boolean false" | debconf-set-selections
sudo echo "postfix postfix/relayhost string" | debconf-set-selections
sudo echo "postfix postfix/chattr boolean false" | debconf-set-selections
sudo echo "postfix postfix/destinations string $domain" | debconf-set-selections
elif [ -n "$(command -v yum)" ] && [ -z "$(command -v postfix)" ]; then
sudo yum install -y postfix >/dev/null
sudo sed -i -e "s/inet_interfaces = localhost/inet_interfaces = all/g" /etc/postfix/main.cf &>/dev/null
sudo sed -i -e "s/#mydomain =.*/mydomain = $domain/g" /etc/postfix/main.cf &>/dev/null
sudo sed -i -e "s/#mynetworks =.*/mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128/g" /etc/postfix/main.cf &>/dev/null
sudo sed -i -e "s/mydestination =.*/mydestination = mail."'$mydomain'", "'$mydomain'"/g" /etc/postfix/main.cf &>/dev/null
sudo sed -i -e "117d" /etc/postfix/main.cf &>/dev/null
fi
#install mailx a CLI tool to send emails
if [ -n "$(command -v apt-get)" ]; then
sudo apt-get -y install bsd-mailx >/dev/null
elif [ -n "$(command -v yum)" ]; then
sudo yum install -y mailx >/dev/null
fi
#enable postfix mail services
sudo systemctl enable postfix &>/dev/null
sudo systemctl start postfix &>/dev/null
sudo systemctl reload postfix &>/dev/null
#kills spinning wheel
kill -9 $SPIN_PID &>/dev/null
tput cnorm
echo ""
#bye bye message :)
printf "${GRN}\n\nWe're done!${NC}"
echo ""
exit 1