When connecting to another computer via ssh the computer will have some environment variables that are not set otherwise. You can examine the environment variables with SSH in the name with:
> printenv | grep SSH
And in both linux and macos you'll see an environment variable:
SSH_CONNECTION=192.168.50.3 65102 192.168.50.12 22
The first ip address and corresponding port represents the ip address if one exists and the second is the machine you are currently ssh'd into.