-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpanel.sh
279 lines (241 loc) · 9.65 KB
/
panel.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
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
#!/bin/bash
version="1.1"
date="2023-10-17"
title="SSH TUI Panel v$version"
linux_dist() {
if [ -x "$(command -v yum)" ]; then
echo "CentOS/RHEL"
elif [ -x "$(command -v apt-get)" ]; then
echo "Debian/Ubuntu"
else
echo "Unsupported"
exit 1
fi
}
get_users() {
awk -F: '$3 >= 1000 && $1 != "nobody" { print $1 }' /etc/passwd
}
is_suspended() {
local username="$1"
local account_status
account_status=$(sudo chage -l "$username" 2>/dev/null | grep "Password expires")
if [ -z "$(echo "$account_status" | grep "never")" ]; then
return 0
else
return 1
fi
}
manage_users() {
users=$(get_users)
if [ -n "$1" ]; then
filtered_users=""
for user in $users; do
if echo "$user" | grep -q "$1"; then
filtered_users="$filtered_users $user"
fi
done
users="$filtered_users"
fi
local i=1
local choices=""
local user_list=""
for user in $users; do
local text
if is_suspended "$user"; then
text="$user(suspended)"
else
text="$user"
fi
choices="$choices $i $text"
user_list="$user_list$user "
i=$(($i+1))
done
choice=$(dialog --clear --backtitle "$title" \
--title "Non-System Users" \
--menu "Select username:" 30 60 20 $choices 2>&1 >/dev/tty)
username="$(echo "$user_list" | cut -d " " -f "$choice")"
if [ -n "$username" ]; then
choice=$(dialog --clear --backtitle "$title" \
--title "Manage User: $username" \
--menu "\nChoose an action:" 20 60 5 \
"S" "Statistics" \
"R" "Reset Password" \
"U" "Suspend User" \
"D" "Delete" \
2>&1 >/dev/tty)
case "$choice" in
"S") # Statistics
user_stats "$username"
;;
"R") # Reset Password
clear
echo "Resetting password for \`$username\`"
sudo passwd "$username"
dialog --clear --backtitle "$title" --title "Success" --msgbox "\nPassword for \`$username\` updated successfully." 10 60
;;
"U") # Suspend User
confirmed_username=$(dialog --clear --backtitle "$title" --title "Suspend User" \
--inputbox "Enter \`$username\` to confirm:" 10 60 2>&1 >/dev/tty)
if [ "$username" = "$confirmed_username" ]; then
sudo passwd -e "$username"
dialog --clear --backtitle "$title" --title "Suspended" --msgbox "\nUser \`$username\` suspended successfully." 10 60
else
dialog --clear --backtitle "$title" --title "Error!" --msgbox "\nOperation canceled!" 10 60
fi
;;
"D") # Delete
confirmed_username=$(dialog --clear --backtitle "$title" --title "Delete User" \
--inputbox "Enter \`$username\` to confirm:" 10 60 2>&1 >/dev/tty)
if [ "$username" = "$confirmed_username" ]; then
sudo userdel -r "$username"
dialog --clear --backtitle "$title" --title "Deleted" --msgbox "\nUser `$username` deleted successfully." 10 60
else
dialog --clear --backtitle "$title" --title "Error!" --msgbox "\nOperation canceled!" 10 60
fi
;;
esac
fi
}
user_stats() {
./hogs -type=csv /var/log/ssh-panel/* > hogs.csv
local i=1
if [ -n "$1" ]; then
users="$1"
else
users=$(get_users)
fi
clear
printf " |-------|-------------------------------------|--------------|--------------|\n"
printf " | # | Username | Upload(MB) | Download(MB) |\n"
printf " |-------|-------------------------------------|--------------|--------------|\n"
for user in $users; do
user_upload=0
user_download=0
rm -f temp.csv
cat hogs.csv | grep ",$user," > temp.csv
while IFS=, read -r tmp upload download username path machine; do
# date=$(echo "$path" | awk -F/ '{print $NF}' | awk -F. '{print $1}' | cut -d "-" -f "1-3")
if [ -n "$upload" ]; then
user_upload=$(echo "$user_upload + ($upload / 1024)" | bc)
fi
if [ -n "$download" ]; then
user_download=$(echo "$user_download + ($download / 1024)" | bc)
fi
done < temp.csv
local text
if is_suspended "$user"; then
text="$user(suspended)"
else
text="$user"
fi
user_upload_formatted=$(echo $user_upload | numfmt --grouping)
user_download_formatted=$(echo $user_download | numfmt --grouping)
printf " | %4d | %-34s | %10s | %10s |\n" $i "$text" "$user_upload_formatted" "$user_download_formatted"
i=$((i + 1))
done
rm -f temp.csv hogs.csv
printf " |-------|-------------------------------------|--------------|--------------|\n\n"
echo "Press Enter to continue..."
dd bs=1 count=1 2>/dev/null
}
while true; do
choice=$(dialog --clear --backtitle "$title" \
--title "SSH User Management" \
--no-cancel \
--menu "\nChoose an operation:" 20 60 10 \
"S" "Statistics" \
"M" "Manage Users" \
"C" "Create User" \
"F" "Find User" \
"U" "Update" \
"A" "About" \
"Q" "Quit" \
2>&1 > /dev/tty)
case "$choice" in
"S") # Statistics
choice=$(dialog --clear --backtitle "$title" \
--title "Statistics" \
--menu "\nChoose an action:" 20 60 5 \
"S" "Statistics / User" \
"C" "Clear Statistics" \
2>&1 > /dev/tty)
case "$choice" in
"C") # Clear Statistics
prompt=$(dialog --clear --backtitle "$title" --title "Clear Statistics" \
--inputbox "Enter \`CLEAR\` to confirm:" 10 60 2>&1 >/dev/tty)
if [ "$prompt" = "CLEAR" ]; then
sudo rm -rf /var/log/ssh-panel/*
dialog --clear --backtitle "$title" --title "Success" --msgbox "\nStatistics cleared successfully." 10 60
else
dialog --clear --backtitle "$title" --title "Cancel" --msgbox "\nOperation canceled!" 10 60
fi
;;
*) # Statistics / User
user_stats
;;
# 2) # Daily
# ;;
esac
;;
"M") # Manage Users
manage_users
;;
"C") # Create User
username=$(dialog --clear --backtitle "$title" \
--title "Create User" \
--inputbox "Enter Username:" 10 40 2>&1 >/dev/tty)
if [ -n "$username" ]; then
if [ linux_dist = "CentOS/RHEL" ]; then
sudo adduser --shell /usr/sbin/nologin "$username"
else
sudo adduser --shell /usr/sbin/nologin --no-create-home --disabled-password --gecos "" "$username"
fi
dialog --clear --backtitle "$title" --title "Success" --msgbox "\nUser \`$username\` created successfully." 10 60
clear
sudo passwd "$username"
dialog --clear --backtitle "$title" --title "Success" --msgbox "\nPassword for \`$username\` updated successfully." 10 60
else
dialog --clear --backtitle "$title" --title "Error!" --msgbox "\nOperation canceled!" 10 60
fi
;;
"F") # Find User
username=$(dialog --clear --backtitle "$title" \
--title "Find User" \
--inputbox "Enter Username:" 10 40 2>&1 >/dev/tty)
manage_users "$username"
;;
"U") # Update Panel
current_sha=$(cat version.info)
latest_sha=$(curl -s "https://api.github.com/repos/vfarid/ssh-panel/commits/main" | jq -r .sha)
if [ "$current_sha" = "$latest_sha" ]; then
dialog --clear --backtitle "$title" --title "Up to Date" --msgbox "\nYou already have the latest version." 10 60
else
prompt=$(dialog --clear --backtitle "$title" --title "Update Panel" \
--inputbox "Enter \`UPDATE\` to confirm:" 10 60 2>&1 >/dev/tty)
if [ "$prompt" = "UPDATE" ]; then
cd ..
clear
wget -O ssh-panel-install.sh https://raw.githubusercontent.com/vfarid/ssh-panel/main/install.sh
sudo rm -rf ssh-panel/*
sudo sh ssh-panel-install.sh
sudo rm -f ssh-panel-install.sh
dialog --clear --backtitle "$title" --title "Success" --msgbox "\nPanel updated successfully." 10 60
clear
cd ..
exit
else
dialog --clear --backtitle "$title" --title "Cancel" --msgbox "\nOperation canceled!" 10 60
fi
fi
;;
"A") # About
dialog --clear --backtitle "$title" \
--title "About" \
--msgbox "\n$title \n\nLicenced under GPLv3\nby Vahid Farid\n\nRepo: github.com/vfarid/ssh-panel\nTwitter: @vahidfarid" 15 60
;;
"Q") # Quit
clear
exit 0
;;
esac
done