Operating System‎ > ‎Linux‎ > ‎Linux Shell Script‎ > ‎

Lab 3-4 Shell Script to track user's login pattern


Lab 3-4 Shell Script Requirement

Lab3-4 Script Requirement


Lab 3-4 Shell Script Solution

#!/bin/bash
# Lab3-4

# Module 3 - Variables and Expressions
#  Write a script that allows the user to track down someone's log in pattern in the system. The script will allow the user to view all user IDs who.ve logged in to the system on a particular day, and then allow the user to choose one particular user id to view all his/her log in time within a range of days.

# Module 4: Selection and Looping (added after done with Module 3)
# The last output line of the last command shows the earliest log in date that is kept track in the history file, wtmp. Use this earliest log in day as the limit to how far the user can go back in time to search for log in information. Don't hard code the earliest day in your script. 
# Assumption: Your script should get this information from last utility, but for now you can assume that the script only has to work between the months of April & May.


# Module 4
earliestLogMonth=$(last | tail -3 | head -1 | tr -s ' ' '$' | cut -f5 -d'$')
earliestLogDay=$(last | tail -3 | head -1 | tr -s ' ' '$' | cut -f6 -d'$')
earliestLogDate=$(date -d "$earliestLogDay-$earliestLogMonth-$(date +%Y)") # Assumption for current year
(( diffDayAllowed = (($(date +%s) - $(date -d "$earliestLogDate" +%s))/(24*60*60))+ 1 ))

isInputDaysOK=1 # false
while [ isInputDaysOK==1 ]
do
echo -n "Enter number of days ago or hit enter to use today: "
read numOfDaysAgo
numOfDaysAgo=${numOfDaysAgo:=0}

#Calculate date entered days ago from current date
prevDate=$(date +%s) # Convert current date to seconds
(( prevDate = prevDate - (numOfDaysAgo*24*60*60) )) # Subtract days
earliestDate=$(date -d "$earliestLogDate" +%s)
if (( ($diffDayAllowed >= $numOfDaysAgo) && ($numOfDaysAgo >= 0) ))
then
isInputDaysOK=0 # true
break
else
echo between 0 and $diffDayAllowed
fi
done
# Module 4 - End

echo "Users logged in $numOfDaysAgo day(s) ago on $(date -d "@$prevDate" +"%b %d")"

#c. Print  in column format the user id, log in start time, and log in duration for all users on the start day. The command last will give you the history of all successful log in (within a range of days, as determined by sys admin); you should use the last command and then print out the required information
prevMonth=$(date -d "@$prevDate" +"%b")
prevDay=$(date -d "@$prevDate" +"%d")
last | awk '($5F ~ '$prevMonth' && $6F ~ '$prevDay') { printf "%-20s%-8s%-8s\n", $1, $7, $(NF)}'

#d. Next ask the user how far back to go from the start day by prompting for the number of days before the start day. This is your end day.
echo -n "Enter number of days back from $(date -d "@$prevDate" +"%b %d"): "
read numDaysFromPrevDate

#e. Prompt for a user id
echo -n "user id: "
read userID

# Calculate date entered days ago from previous date
(( dateFrmPrevDate=prevDate - (numDaysFromPrevDate*24*60*60) ))
echo "User $userID log in record between $(date -d "@$prevDate" +"%b %d") and $(date -d "@$dateFrmPrevDate" +"%b %d"): "

#f.Print all log in day and time of the user id between the start day and the end day
prevMonthFrom=$(date -d "@$dateFrmPrevDate" +"%b")
prevDayFrom=$(date -d "@$dateFrmPrevDate" +"%d")
last $userID | \
awk '(($5F ~ '$prevMonth' || $5F ~ '$prevMonthFrom') && ($6F <= '$prevDay' && $6F >= '$prevDayFrom')) \
{ printf "%-20s%-8s%-8s%-8s%-8s\n", $1, $5, $6, $7, $(NF)}'


Comments