I'm not sure why that command running on ssh is the right one :-) Might be worth explaining with the desired output of "last | head -n1"

Also, I guess there should be a sleep before running next.