This in in PERL Unix systems keep track of which users log in, when they do it a
ID: 3756464 • Letter: T
Question
This in in PERL
Unix systems keep track of which users log in, when they do it and where they do it from. To see an example of this, try out the last command on your Unix system: [Lokil last This will give you a listing of all of the people who have logged in to the system for the current month, with the most recent logins reported at the beginning of the list. To page through the results using the less command in Unix, use this command instead: [Loki]:-:> last less This pipes the output through the less command, which allows you to page through the output and use vim -like control to scroll forward and backward through the output. To process the previous month's logins, try this [Loki] last -f /var/log/wtmp.1 less A typical line of output looks something like one of the following two lines rfulkers pts/46 rfulkers pts/56 137.48.186.243 Thu Sep 17 11:52 13:58 (02:06) pc-185-13.ist.un Thu Sep 17 11:11 16:37 (05:25) As you can see, there are 7 logical "ields" contained within the output of the last command, and there are 10 actual pieces of information. The logical fields are the TRUNCATED_ACCOUNT, TERMINAL, HOSTNAME, DATE, LOGIN, LOGOUT, and DURATION. The DATE field is made up of 3 separate pieces of information (DAYOFWEEK, MONTH, DATE), and there is a dash between the LOGIN and LOGOUT fields. The TERMINALExplanation / Answer
#!/bin/bash
if [[ $1 == '' ]]
then
echo "usage: ./lastsummary <username>"
exit
fi
out=$(last | grep $1)
c=1
echo "Here is a listing of the logins for $1:"
echo ""
oneline=""
hours=0
minutes=0
while read -r line
do
echo " $c. $line"
c=$((c+1))
#oneline=$line
read -ra el <<< "$line"
f=${el[-1]}
s=${el[-1]}
if [[ ${f:2:1} == '+' ]]
then
fh=${f:3:2}
fm=${f:6:2}
else
fh=${f:1:2}
fm=${f:4:2}
fi
hours=$((hours+fh))
minutes=$((minutes+fm))
done < <(printf '%s ' "$out")
if [[ $minutes -ge 60 ]]
then
h=$((minutes/60))
hours=$((hours+h))
minutes=$((minutes%60))
fi
echo ""
echo "Here is the summary of the time spent on the system for $1:"
echo ""
echo $1
echo $c
echo "$hours::$minutes"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.