How to configure a command to run on login, but only if through telnet

2

I want to run a command every time I log in to my Ubuntu box, but only if I'm connecting through telnet, not if I'm logging in at the console.

moswald

Posted 2010-02-09T15:43:43.433

Reputation: 238

Answers

2

Something like this in your .bashrc (assuming bash) should work fairly well:

if ( tty | egrep -q '\/pts\/' )
then
   echo "is a pts - remote login"
else
   echo "is not a pts - local login"
fi

You would just replace the echo's with whatever it was that you wanted to run.

jch

Posted 2010-02-09T15:43:43.433

Reputation: 166

A pty will also be allocated for a X terminal or 'screen' window, so this is not very reliable. – user1686 – 2010-02-09T18:40:26.820

I do open a few terminal windows open when I login at the console... – moswald – 2010-02-09T19:11:22.840

Yeah, this is not going to be good enough. That causes all X terminal windows to print 'is a pts - remote login'. – moswald – 2010-02-11T16:41:58.027

0

telnet? I don't recommend its use! One of the following approaches should work for most methods of remote access. (Remote access via a X client, or via VNC or similar being excusable exceptions.)

  • some telnetd set the environment variable REMOTEHOST or similar which you can check
  • if you are running xinetd you can alter the telnetd environment via the env attribute
  • if you really mean ssh (OpenSSH), then you can check either of the variables SSH_TTY and SSH_CONNECTION.

The Linux console virtual terminals (before you start X) will have tty names like /dev/tty1 rather than pty (pseudo-ttys) in /dev/pts/ (at least on x86). Within X, terminals will be allocated ptys.

Checking environment variables can often be subverted by users, if that's a concern.

A more robust general case solution is to walk up the process tree from $$, until you find what you're looking for (or not), use this in a bash script:

function checkparents() 
{
    local _proc=$1 _pid _ppid=$$ _tty="" _comm _rc=1

    while [ "$_ppid" != "1" ]; do
        read _pid _ppid _tty _comm < <(ps --no-headers -p $_ppid -o "pid ppid tty comm")
        #echo "$_pid $_ppid $_tty $_comm"
        [ "$_comm" = "$_proc" ] && { _rc=0; break; }
        [ "$_pid"  = "$_proc" ] && { _rc=0; break; }
    done
    return $_rc
}

When run via an ssh login for example:

$ if checkparents sshd; then echo ssh; fi
ssh
$ if ! checkparents in.telnetd; then echo not telnet; fi
not telnet

Un-comment the echo line to see it working.

mr.spuratic

Posted 2010-02-09T15:43:43.433

Reputation: 2 163

0

I personally munge the output of "who am i" for other reasons (setting DISPLAY). The last field seems to be the "source" of the login

    typeset -a LOGINARRAY
    # who am I format: USER TTY MON DAY TIME LOGINHOST, 
    # use array to get last entry
    LOGINARRAY=( $(/usr/bin/who -sum) )
    LASTINDEX=$(( ${#LOGINARRAY[*]} - 1))
    LOGINHOST=${LOGINARRAY[$LASTINDEX]}
    LOGINHOST=${LOGINHOST##*\(}
    LOGINHOST=${LOGINHOST%%)*}
    DISPLAY=$LOGINHOST:0
    export DISPLAY

    unset LOGINARRAY LASTINDEX

it should be easy enough to check the format of LOGINHOST to see if it's a "remote" login. telnet is disabled here (as it should be) so I can't explicitly check to see how it's set for telnet

Rich Homolka

Posted 2010-02-09T15:43:43.433

Reputation: 27 121