getting an exporting an ipaddress - is awk the man for the job?

Posted on 2002-04-23
Last Modified: 2013-12-06
to run an xwindows session from our box i type this:
DISPLAY=999.99.99.99;export DISPLAY.

then i noticed using who -T that the system already knows
the ip i am logged into and wrote this to capture it:
who -T | awk '/myname/ {print $9}'

i got to wondering how i can capture this automatically so that when i needed the gui i could just type an alias and sit back and relax.

can i just capture it automatically at login and store it during the session? if that was undesirable, is there a smarter way to extract it than using my clumsy awk?

thanks for your insight.
Question by:banba
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 40

Expert Comment

ID: 6964374
You could capture the IP and export the DISPLAY var at login by including the shell commands in your shell's init script.

If your system supports it, you could use ssh to connect and it'll automatically forward X to your local system.
LVL 38

Accepted Solution

yuzh earned 50 total points
ID: 6964397
if you want to detect your IP for setting DISPLAY, who is not a porper command to do it.

   #who | grep myusername | awk '{print $6}'

   will give the IPs, what happen if you login from more than one machine? you'll not be able to get the correct for setting the display.

   Check if your have tcsh installed on your system, type in:
   #which tcsh

   if you have tcsh installed then your can use the following tcsh script to do the job:

#!/bin/tcsh -f

      if ( $?REMOTEHOST ) then
#          setenv MACHINE `echo ${REMOTEHOST} | cut -d. -f1`
           setenv MACHINE `echo ${REMOTEHOST}`
           echo "${MACHINE}:0.0" > ~/.dsplay
           setenv MACHINE `hostname`
           echo "${MACHINE}:0.0" > ~/.dsplay


put it somewhere, eg yourhome/bin nameed it ckremote

add the followings .profile

# Auto set the DISPLAY for X server
    DISPLAY=`cat ~/.dsplay`
    export DISPLAY

LVL 51

Expert Comment

ID: 6964939
hmm, sounds simple, but is complicated in real life.
Is it always the same UNIX you're connecting to? then a simple awk might do it. Please tell us which UNIX you come from, and which one is remote.

If you have various flaviours of local and remote UNIXs, then check following:

     echo $REMOTEHOST
     who am i
     who am i -R
     netstat -an|egrep ':22|:23|:513'

probably you need to combine information from several commands.
There're lot of dragons to beat ...
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6964963
knowing the environment is important.

When using lots of 'rlogin' life is complicated.
When dialing in onto a NT/WIn2000 box and then  connecting  via WRQ, Exceed, ... to unix things get really complicated. In this case the DISPLAY will end on ":0" any more, but some other numbers. In this case you need then just the ip address.

What i used to do, is provide user with some menu (dynamically build using method explained in above 'comments', and have the user choose. The latest choosen option can be the 'default' value.
LVL 51

Expert Comment

ID: 6965035
oops, forgot to mention nested logins (nearly impossible to handle), thanks elfie
LVL 38

Expert Comment

ID: 6965047
I agree with ahoffmann, nested logins (nearly impossible to handle),.

and remember REMOTEHOST available in tcsh
LVL 51

Expert Comment

ID: 6965067
yuzh@idg, $REMOTEHOST is IRIX only, not tcsh
LVL 38

Expert Comment

ID: 6970210
Try to use tcsh (for Solaris or HP_UX), $REMOTEHOST is there.

I am using it for both Solaris and HP_UX for my users.
LVL 51

Expert Comment

ID: 6970541
nice info, yuzh, hich version of tcsh do you use?
Are you shure that REMOTEHOST is not set by any of the shell's start up scripts (/etc/csh.{login,cshrc}, etc.)

banba, sorry for being off-topic
LVL 38

Expert Comment

ID: 6976158
Hi ahoffmann,

   I have a few different version of tcsh installed on my network.


   for Solaris (Sparc), use an old version:
   version tcsh 6.08.00 (Astron) 1998-10-02 (sparc-sun-solaris)

   For Solaris (intel), tcsh 6.10.xx
   For HP-UX (10.20) use tcsh 6.10.xx.

   For Linux, use
   version tcsh 6.11.00 (Astron) 2001-09-02 (i386-intel-linux)

   Nothing set by the startup script. I wrote all the setup script for my users.

   They all works.

   Just try it out yourself.

LVL 51

Expert Comment

ID: 6976488
hmm, my
tcsh 6.08.05 (Astron) 1999-05-11 (i386-intel-linux)
doesnt use REMOTEHOST, even strings does not give REMOTEHOST in tcsh executable, so I believe it doesn't know it. Strange ...
Probably HP-UX sets the variable for login|rlogin.

Author Comment

ID: 6977755
thanks, that worked great. also thanks to you other folks, i value the discussion as it teaches me HOW to approach some problems.


Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (, discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question