I determine whether I'm remote using this C program I wrote. I've tested it only linux. It uses the fact that most well-behaved remote login daemons make sure that your tty's utmp entry contains the host from which you logged in if you are logged in remotely. This program prints "local" if you're local, "remote" if you remote, and a few other things if it can't tell for various reasons. I've found it to accurately detect remote logins with ssh, telnet, and (Kerberos) rlogin and to accurately detect local logins with or without X. If you remote login back to your local host (for some reason) it still considers you to be local.
Attachment:
utmp_checkremote.c
Description: Binary data