forked from IntellexApps/ssh_notify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh_notify.sh
executable file
·223 lines (160 loc) · 4.86 KB
/
ssh_notify.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
215
216
217
218
219
220
221
222
223
#!/bin/bash
#
# title : ssh_notify
# description : Notifies about a login via ssh.
# author : Intellex
# date : 20152205
# version : 0.1.0
# usage : place in /etc/profile.d/ssh.sh file
#===============================================================================
# Config {
# Define the list of users to ignore, user space or , to separate users
IGNORED_USERS="git"
# Email {
# Send email
EMAIL_ENABLED=1
# Email addresses to send a mail to, use , to separete multiple entries
EMAIL_RECEPIENTS="[email protected]"
# No email for logging from this IPs, use , to separate multiple entries
EMAIL_IGNORED_IPS="8.8.8.8"
# }
# Welcome message {
# Show message
MESSAGE_ENABLED=1
# List of services to check, use space to separate multiple entries
MESSAGE_SERVICES="nginx mysqld"
# }
#~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ }
# Definitions {
# Colors
RED=$(tput setaf 1)
GREEN=$(tput setaf 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"; then
# Get the remote address
IP=`echo $SSH_CLIENT | awk '{ ip = $1 } END { print ip }'`
PTR=`dig +short -x ${IP} | sed s/\.$//`
# Get the number of previous logins {
# Prepare
HISTORY_IP=()
HISTORY_COUNT=()
HISTORY_COUNTER=0
# Make sure the file exists
touch $FILE
# Get the current state
if [ -f $FILE ]; then
LIST=`cat "${FILE}"`
for VALUE in $LIST; do
# Get the pointer
I=`expr $HISTORY_COUNTER / 2`
# IP
if [ $((HISTORY_COUNTER%2)) -eq 0 ]; then
# Validate ip
if isValidIp $VALUE; then
HISTORY_IP[$I]=$VALUE
fi
# Count
else
# Validate count
if isNumber $VALUE; then
HISTORY_COUNT[$I]=$VALUE;
fi
fi;
# Increment counter
((HISTORY_COUNTER++))
done
fi
# Write back
echo '' > $FILE
for I in "${!HISTORY_IP[@]}"; do
C=${HISTORY_COUNT[$I]}
#if [ -n "${HISTORY_IP[$I]}" ] && [ -n "$C" ]; then
# Increment
if [ ${HISTORY_IP[$I]} == ${IP} ]; then
((C++))
FOUND=$C
fi
# Write to file
echo "${HISTORY_IP[$I]} ${C}" >> $FILE
#fi;
done
# If IP not found
if [ -z $FOUND ]; then
echo "${IP} 1" >> $FILE
FOUND=1
fi;
# }
# Send email {
if ! [[ $EMAIL_ENABLED -eq 0 ]] && ! inArray ${IP} "${EMAIL_IGNORED_IPS}"; then
# Email body
echo "This is an automated message reporting that a user has logged into the system.
Number of logins from this IP: ${FOUND}
User : `whoami`
Remote IP : ${IP}
Remote port : `echo ${SSH_CLIENT} | awk '{ port = $2 } END { print port }'`
Reverse DNS : ${PTR}
Server name : `uname -a | awk '{ name = $2 } END { print name }'`
Server IP : `dig +short myip.opendns.com @resolver1.opendns.com`
Server port : `echo ${SSH_CLIENT} | awk '{ port = $3 } END { print port }'`
Server time : `date +'%Y-%m-%d %T'`
--
Sent from `uname -a | awk '{ name = $2 } END { print name }'` [`dig +short myip.opendns.com @resolver1.opendns.com`]
" | mail -s "`whoami` has just logged on `uname -a | awk '{ name = $2 } END { print name }'`" "${EMAIL_RECEPIENTS}"
fi # }
# Print system info {
if ! [[ $MESSAGE_ENABLED -eq 0 ]]; then
# Get the services
[[ -z $(command -v service) ]] && SVC_CMD="/etc/init.d/" || SVC_CMD="service "
for SERVICE in ${MESSAGE_SERVICES}; do
STATUS=`${SVC_CMD}${SERVICE} status 2>&1`
SERVICES="${SERVICES} ${STATUS}\n"
done;
echo " Uptime :`uptime | grep -oP '^.*?(?=,)'`
Load average : `uptime | grep -oP '(?<=average: ).*'`
Space available : `df -h / | awk '{ a = $4; } END { print a }'`"
if ! [[ -z $SERVICES ]]; then
echo -e "\n${SERVICES}"
else
echo
fi
echo " ${YELLOW}CAUTION: Admin has been notified of this login${CLEAR}
Your IP address is ${YELLOW}${IP}${CLEAR}
Total IP logins: ${YELLOW}${FOUND}${CLEAR}
"
fi
fi;