Tuesday, September 22, 2009

Determine if user is logged in remotely or locally

Since I do all the maintenance on the digital picture frame via ssh I don't want the same login scripts to run when i log in remotely as when I log in locally. Well to be more specific I don't really log in locally but the user I have created logs in automatically when the DPF is turned on and from there on a number of things start. Needless to say I don't need to run "startx" everytime I log in via ssh.

So I wrote a small script to help me determine whether or not I was logged in locally or remotely (via ssh). The script is in its bare form here, I use it in my .bash_profile but you might find another use for it.

Important note: This script doesn't really determine if a user is logged in locally or remotely but rather using a tty or pts although the latter usually means that the user is logged in remotely, but not always. ;)

# tty-vs-pts.sh
# tty vs pts checker by Markus Ulfberg 2009-09-22
# Inteded use is running different scripts wether
# the user logs in remotely or locally.

# Get the name of the tty the user is on.
# strip everything but tty or pts from the string that "tty" produces.

# Check if it is tty or pts.
if [ $USER_TTY == 'tty' ]
echo "You are logged in locally using: $FULL_USER_TTY"

if [ $USER_TTY == 'pts' ]
echo "You are logged in remotely using: $FULL_USER_TTY"
# Return failure if it is neither.
echo "Failed to establish type of tty"

Formatted for blogger using: formatmysourcecode.blogspot.com