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. ;)
#!/bin/sh

#
# 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.
FULL_USER_TTY=`tty`
# strip everything but tty or pts from the string that "tty" produces.
USER_TTY=${FULL_USER_TTY:5:3}

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

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


Formatted for blogger using: formatmysourcecode.blogspot.com