Last command alternative?

I want to know to display the recently logged in user, like the last command in linux. However, i am unable to find the last command. Can anyone suggest me the alternative for the last command in openwrt ??

Help is very much appreciated

OpenWrt uses only runtime logs to save flash resource:

So, that feature is unreliable and therefore is stripped off:

And it requires to recompile the related packages to enable the feature, otherwise you can get only:

logread -e dropbear

You can get an output, almost similar to the linux command β€œlast” using a script.

cat << "EOF" > /root/last
#!/bin/sh
logread -e dropbear | grep 'Password auth succeeded for' | sed 's/^.* dropbear\[//' | sed 's/\]:.*//' | sort -u > /tmp/procids

successfullogins="/tmp/procids"

while IFS= read -r procid
do
user=$(logread -e "$procid" | grep 'Password auth succeeded for' | sed 's/^.*succeeded for //' | sed 's/ from.*//')
ip=$(logread -e "$procid" | grep 'Password auth succeeded for' | sed 's/^.*from //' | sed 's/\:.*//')
starttime=$(logread -e "$procid" | grep 'Password auth succeeded for' | sed 's/authpriv.*//' | sed 's/ from.*//')
endtime=$(logread -e "$procid" | grep 'Exited normally' | sed 's/authpriv.*//' | sed 's/ from.*//')
if [ -z "$endtime" ]; then
endtime="still logged in"
fi
echo -e "$user\t$ip\t$starttime"- "$endtime"
done < "$successfullogins"
exit 0
EOF
chmod 755 /root/last
root@OpenWrt:~# /root/last
'root'  192.168.2.135   Mon Jun  7 14:44:58 2021 - Mon Jun  7 15:21:42 2021
'root'  192.168.2.135   Mon Jun  7 15:21:13 2021 - Mon Jun  7 15:21:16 2021
'root'  192.168.2.135   Mon Jun  7 15:22:02 2021 - Mon Jun  7 17:05:10 2021
'root'  192.168.2.135   Mon Jun  7 16:31:55 2021 - Mon Jun  7 17:04:41 2021
'root'  192.168.2.135   Mon Jun  7 17:08:24 2021 - Mon Jun  7 17:33:35 2021
'root'  192.168.2.135   Mon Jun  7 17:15:01 2021 - Mon Jun  7 17:33:25 2021
'root'  192.168.2.135   Mon Jun  7 17:33:50 2021 - still logged in

Tested with 19.07.7

4 Likes