iphony has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a script which runs automatically when the user logs on. Sometimes the user may logon via the console (tty) sometimes via remotely via network (pts). E.g.
[root@localhost ~]# who root tty1 Nov 12 15:48 root pts/0 Nov 12 15:51 (10.10.20.13)

How can the perl script detects which terminal it's currently running on? Thanks.

Replies are listed 'Best First'.
Re: Detecting the current terminal
by Fletch (Bishop) on Nov 12, 2008 at 14:34 UTC

    If you're looking for OS interaction POSIX is (usually, most of the time :) your friend . . .

    $ perl -MPOSIX=ttyname -le 'print "I am on tty ", ttyname(STDIN);' I am on tty /dev/pts/34 $ tty /dev/pts/34

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Detecting the current terminal
by pjotrik (Friar) on Nov 12, 2008 at 09:08 UTC
    Just an idea:

    who -a gives PID of the login process in addition. From the PID of your script, you could traverse the parent-pid hierarchy (through ps -f --pid xxx or some handy module), until you find one listed in the who -a.

Re: Detecting the current terminal
by shmem (Chancellor) on Nov 12, 2008 at 14:42 UTC
    chop (my $tty = `tty`); if ($tty =~ /tty\d/) { # console ... } else { # network ... }
Re: Detecting the current terminal
by Anonymous Monk on Nov 12, 2008 at 09:38 UTC
    Why would the script care?
      It's because some functions would be disabled if the user is running the script remotely.
        web servers set some special %ENV values, don't remote terminal servers do the same?