-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsshmonitor
More file actions
179 lines (157 loc) · 4.96 KB
/
Copy pathsshmonitor
File metadata and controls
179 lines (157 loc) · 4.96 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
#!/bin/bash
CONFIG="/opt/ssh-monitor/config.json"
SERVICE="ssh-monitor"
case "$1" in
start)
echo "Activation..."
systemctl start "$SERVICE"
;;
stop)
echo "Désactivation..."
systemctl stop "$SERVICE"
;;
restart)
echo "Redémarrage..."
systemctl restart "$SERVICE"
;;
status)
get_version() {
local f="$1"
if [ ! -f "$f" ]; then
echo ""
return
fi
# read file, strip CRLF
local content
content=$(tr -d '\r' < "$f")
if echo "$content" | grep -q "="; then
echo "$content" | sed -n 's/^[[:space:]]*[Vv]ersion[[:space:]]*=\s*//p' | head -n1
else
echo "$content" | head -n1
fi
}
ver=$(get_version /opt/ssh-monitor/version.txt)
if [ -n "$ver" ]; then
echo "Version: $ver"
else
echo "Version: inconnue"
fi
systemctl status "$SERVICE"
;;
user)
echo "Utilisateurs connectés :"
python3 - <<'PY'
import subprocess, time, datetime, re
out = subprocess.check_output(['who','-u']).decode(errors='ignore')
now = time.time()
for line in out.splitlines():
parts = line.split()
if not parts: continue
user = parts[0]
# try to find host in parentheses
host = ''
for p in parts:
if p.startswith('(') and p.endswith(')'):
host = p[1:-1]
break
# find time token HH:MM and optional date YYYY-MM-DD
time_token = None
date_token = None
for p in parts[1:]:
if re.match(r'^\d{2}:\d{2}$', p) and time_token is None:
time_token = p
if re.match(r'^\d{4}-\d{2}-\d{2}$', p) and date_token is None:
date_token = p
if not time_token:
# fallback: try second token
if len(parts) > 2:
time_token = parts[2]
if not time_token:
continue
if not date_token:
date_token = datetime.date.today().isoformat()
dt = datetime.datetime.fromisoformat(date_token + ' ' + time_token)
ts = dt.timestamp()
if ts > now + 12*3600:
dt -= datetime.timedelta(days=1)
ts = dt.timestamp()
else:
dt = datetime.datetime.fromisoformat(date_token + ' ' + time_token)
ts = dt.timestamp()
dur = int(now - ts)
h = dur // 3600
m = (dur % 3600) // 60
target = host if host else (parts[1] if len(parts) > 1 else '')
print(f"{user}\t{target}\t{date_token} {time_token}\t{h:02d}h{m:02d}m")
PY
;;
version)
get_version() {
local f="$1"
if [ ! -f "$f" ]; then
echo ""
return
fi
local content
content=$(tr -d '\r' < "$f")
if echo "$content" | grep -q "="; then
echo "$content" | sed -n 's/^[[:space:]]*[Vv]ersion[[:space:]]*=\s*//p' | head -n1
else
echo "$content" | head -n1
fi
}
ver=$(get_version /opt/ssh-monitor/version.txt)
if [ -z "$ver" ] && [ -f ./version.txt ]; then
ver=$(get_version ./version.txt)
fi
if [ -n "$ver" ]; then
echo "Version: $ver"
else
echo "Version introuvable."
fi
;;
add)
if [ -z "$2" ]; then
echo "Usage: sshmonitor add <ip>"
exit 1
fi
if ! [[ "$2" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "Adresse IP invalide."
exit 1
fi
jq --arg ip "$2" '
if .whitelist | index($ip)
then .
else .whitelist += [$ip]
end
' "$CONFIG" > /tmp/config.json && mv /tmp/config.json "$CONFIG"
echo "IP $2 ajoutée à la whitelist."
;;
remove)
if [ -z "$2" ]; then
echo "Usage: sshmonitor remove <ip>"
exit 1
fi
jq --arg ip "$2" '
.whitelist |= map(select(. != $ip))
' "$CONFIG" > /tmp/config.json && mv /tmp/config.json "$CONFIG"
echo "IP $2 supprimée de la whitelist."
;;
list)
echo "Whitelist :"
jq -r '.whitelist[]' "$CONFIG"
;;
*)
echo "Usage:"
echo " sshmonitor start # start the service"
echo " sshmonitor stop # stop the service"
echo " sshmonitor restart # restart the service"
echo " sshmonitor status # show service status and installed version"
echo " sshmonitor user # list connected users with login time and duration"
echo " sshmonitor version # show installed version"
echo " sshmonitor add <ip> # add IP to whitelist"
echo " sshmonitor remove <ip> # remove IP from whitelist"
echo " sshmonitor list # display whitelist"
exit 1
;;
esac