gagle / raspberrypi-motd

Message of the Day for the Raspberry Pi.
MIT License
194 stars 75 forks source link

Last Login....: None #8

Open WouterVdS opened 7 years ago

WouterVdS commented 7 years ago

The 'last login' line doesn't work for me. I'm on a Pi Zero with: PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)" NAME="Raspbian GNU/Linux" VERSION_ID="8" VERSION="8 (jessie)" ID=raspbian ID_LIKE=debian

I also got the 'last: invalid option -- '-'' problem (see other issue) but PenguinPaws's comment fixed that!

vidia commented 7 years ago

I fixed this here: https://gist.github.com/vidia/5d760a27cec2b9bd318dcc5c636c5a1d#file-motd-sh-L113

and line 123 as well.

WouterVdS commented 7 years ago

Still not working for me, replaced both lines 113 and 123

DoctorOctagonapus commented 7 years ago

I'm getting the same issue. I've replicated the highlighted line and it doesn't show a usage error, but it's still printing None under login. I'm guessing there's a problem either with the If statement or the value $loginDate.

NotMyFirstChoice commented 7 years ago

I was having the same issue. I applied the fixes from @vidia, but it still didn't work until I made one more change. On line 122 from @vidia's link above the IF statement is looking for a "T" in the $loginDate string and on my Raspberry Pi that doesn't exist, so I changed the if [[ $loginDate == T ]]; to if [[ $loginDate != T ]];. Now it works.

NotMyFirstChoice commented 7 years ago

Don't use my fix listed above, It has issues on some days of the week. There may be better ways to fix it, but for now I just changed it to $loginDate == instead of $loginDate == *T\. This is more of a hack, but I think it will be okay for now.

if [[ $loginDate == * ]]; then login="$(date -d "$loginDate" +"%A, %B %d %Y, %T") ($loginIP)" else # Not enough logins login="None" fi

UsernamesAreForChumps commented 7 years ago

signed up for github just to leave this fix! hope that it works for all... i dont know if i am supposed to modify the code with this slightly hackish fix or just leave it, but at least i'll leave a comment with how i fixed it...

the issue for this is that the "--time-format" switch is not supported on this version of the date command for whatever reason. the work around i cobbled together involves line 100:

original code: read loginFrom loginIP loginDate <<< $(last $me --time-format iso -2 | awk 'NR==2 { print $2,$3,$4 }')

modified (and added line) code: read loginFrom loginIP loginMonth loginDay loginTime loginYear <<< $(last $me -2 -F | awk 'NR==2 { print $2,$3,$5,$6,$7,$8 }') loginDate=$( date -d "${loginDay}-${loginMonth}-${loginYear} ${loginTime}" +'%FT%T' ) # format date to ISO

the key difference is that it changes the date to display the full date with the -F switch, and pulls all of the individual date elements followed up by the next command to join all of the different strings into the ISO formatted string that the later code (if [[ $loginDate == T ]]; then...), that particular if is looking for the "T" that is present in an iso formatted timestamp which was originally designed to come from the "--time-format iso" portion.

DoctorOctagonapus commented 7 years ago

I tried that fix but couldn't get it to work for some reason. With your modified code it displays the login IP but no data to the left of that.

UsernamesAreForChumps commented 7 years ago

what is the output of

# last -2

---EDIT--- disregard my question, i see the issue is that for some reason when i added the code it inserted an extra space between the $FT and %T, they should be all together. i have edited the fix in the original post.

DoctorOctagonapus commented 7 years ago

Yep that fixed it! Awesome thanks.