How to unlock my mac remotely?
Set the time to require a password after sleep/screensaver in System Preferences > Security &amp; Privacy > General to "immediately".

To lock the screen enter in the ssh session:

open -a ScreenSaverEngine


To stop the screensaver (=show the login screen) and unlock your user enter

osascript -e 'tell application "system events" to keystroke return'
osascript -e 'tell application "system events" to keystroke "password"'
osascript -e 'tell application "system events" to keystroke return'


Under certain circumstances you may have to enter the password one by one or first one keystroke and then the rest:

osascript -e 'tell application "system events" to keystroke "p"'
osascript -e 'tell application "system events" to keystroke "a"'
...
osascript -e 'tell application "system events" to keystroke return'


or

osascript -e 'tell application "system events" to keystroke "p"'
osascript -e 'tell application "system events" to keystroke "assword"'
osascript -e 'tell application "system events" to keystroke return'