Sometimes it's nice when something is much easier than you expected. I have a few cobbled together python scripts for speeding things up, one in particular I have on my home pc which I wanted to work out if am I running it locally or if I have SSH'd in... below is the surprising simple example!
#!/usr/bin/env python
import os
try:
os.environ["SSH_TTY"]
print("SSH Connection Detected")
except:
print("Running Locally")
You could also test the SSH_CLIENT
variable as well, but this was good enough for me.
Enjoy!