Article 6972 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:6972
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.ecn.bgu.edu!usenet.ins.cwru.edu!howland.reston.ans.net!paladin.american.edu!europa.eng.gtefsd.com!uunet!math.fu-berlin.de!informatik.tu-muenchen.de!lrz-muenchen.de!informatik.uni-muenchen.de!user@cis.uni-muenchen.de!max
From: [email protected] (Max Hadersbeck)
Subject: Develop DISPLAY Variable for X at login time.
Message-ID: <[email protected]>
Sender: max@[email protected] (Max Hadersbeck)
Date: Tue, 19 Oct 1993 10:27:56 GMT
Reply-To: [email protected]
Organization: CIS, University of Munich
X-Newsreader: mxrn 6.18-4
Keywords: DISPLAY Variable X- Windows
Lines: 81


Hi
Here is a program, which I find very useful in a login procedure.
If you log into a machine you must set your display variable to your
host and screen. The output of who truncates your machinename. So
use this script.
MAX
---------------------------------
cut here :

#!/bin/perl
# title : getXdisplayvar.pl
# author :
#----------------------------------------------------------------------
#Maximilian Hadersbeck     | Wagmuellerstr. 23  | Phone: +49 89 2110672
#CIS                       | D-80538 M"unchen   | Fax:   +49 89 2110674
#University of Munich      | Germany            | [email protected]
#----------------------------------------------------------------------
# Version : 1.0
# This program gets the right host and display-name for your DISPLAY
# variable. This is very usefull in a login script,
# if you connect to a remote machine
#

# get user name and terminal number :
$iam=`whoami`;
chop $iam;
$termid=`tty`;
$termid=~ s/\/dev\///;
chop $termid;

# get hostname extract
open(WHOFILE,"who |");
while (<WHOFILE>)

{
($name,$term,$mon,$day,$time,$host_split) = split(' ',$_);
 if ( length $host_split && $name eq $iam && $term eq $termid)
       {
       $host = $host_split;
       last;
       };
};
close (WHOFILE);


if ( $host =~ /\./ )
{
if   ( ! $host =~ /0/)
  {
       ($hostx,$rest) = split(/\./,$host,2);
        chop $hostx;
        $hostx =~ s/\(//;
  };
if ($host =~ /0/)
  {
       $hostx = `hostname`;
       chop $hostx;
   };
}
else
{
 $hostx = $host;
 $hostx =~ s/^\((.*)\)$/$1/;
};

print "You come from the host  : $hostx \n";

$host_from = &name_to_address($hostx);

print "Your IP-number and display is  $host_from:0.0\n";


sub name_to_address {
  local ($name) = shift (@_);
  local (@octets);
  local ($nam, $aliases, $addrtype, $length, $address) = gethostbyname ($name);
  if (! length ($address)) { die "$prog: no address found for $name\n"; }
  @octets = unpack ("CCCC", $address);
  join ('.', @octets[0..3]);
}