Lock keyboard from CLI
One possibility could be to use ControllerMate.

You would have to create a Driver Configuration and map all of the keys to None.  Then there are multiple ways to enable/disable the custom Driver Configuration, but unfortunately CLI is currently not an option.

But CM is very customizable and I'm sure you could come up with something.  For example, using the logic blocks, you could have it disable when you have held down Z for 2 seconds, then hold down P and click the mouse.  Using a delay timer block to wait for 30 seconds but then only re-enable if holding the left shift key down and scrolling the mouse up/down.

Another similar utility that might work is Karabiner-Elements.  It is a complete rewrite of his wonderful Karabiner app which broke hard with macOS Sierra.  It is still in the early development stages but it might have the functionality you need.