-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_users.sh
29 lines (26 loc) · 1.29 KB
/
check_users.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
#!/bin/bash
set -x # Enable tracing of each command
# Read all users from users.csv
users_list=$(cut -d, -f1 /var/log/users.csv)
# For each user in users_list
while IFS= read -r user; do
# Check if the user tried to connect in auth.log
if grep -q "Invalid user $user" /var/log/auth.log; then
# If the user exists in users.csv but not in the system
if ! id "$user" &>/dev/null; then
# Extract user details from users.csv
user_details=$(grep "^$user," /var/log/users.csv)
username=$(echo $user_details | tr -d '\r' | cut -d',' -f1)
password=$(echo $user_details | tr -d '\r' | cut -d',' -f2)
expires=$(echo $user_details | tr -d '\r' | cut -d',' -f5)
traffic_limit=$(echo $user_details | tr -d '\r' | cut -d',' -f6)
# Create the user
sudo useradd -s /usr/sbin/nologin $username
echo "$username:$password" | sudo chpasswd
expires_date=$(date -d "+$expires days" +%Y-%m-%d)
sudo chage -E "$expires_date" $username
traffic_limit_bytes=$((traffic_limit * 1000000000)) # Convert GB to Bytes
sudo iptables -A OUTPUT -p tcp -m owner --uid-owner $username -m quota --quota $traffic_limit_bytes -j ACCEPT
fi
fi
done <<< "$users_list"