There is a command line tool for this: "caffeinate". You can add a script that runs it while there are SSH sessions open.
The exceptions are really narrow. E.g. if you want your SSH sessions to stay alive, you also need to ensure that you never leave the WiFi coverage.