Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2002-04-23
Medium Priority
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 200 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 ...
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

722 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