SUMMARY: Getting peer IP address from shell level

From: Michael Ryan (
Date: Fri Jul 21 1995 - 08:20:35 CDT

Thanks folks.

Summary of question:
Can the IP address of the host a user telnets in from be obtained by
running a program from the shell?

Synopsis of answers:
Nearly all advised piping the output of "who am i" into "awk" and
getting the host/address bit.
I was aware that this would work in a BSD environment but,
unfortunately, it won't work on all platforms, especially SVR3
systems (where this information isn't stored in the utmp file). I know I
didn't throw in the SysV bit in the original question, but I wanted to
keep the question short.
It appears it's not possible to write a program to accomplish this.
One person suggested using "gethostbyname()" but that requires
knowing the host name. Again, through a fault of mine, I didn't
specify that *either* hostname or IP address is what I'm looking for
as, once I've got one, the other is easily obtained.
Anyway, sincere thanks to all who took the time and trouble to reply.
I appreciate it.

Bye for now,

Summary of answers:

From: (Al Venz)
use ksh and in my .profile I define my DISPLAY environment variable
in the
following manor:
export DISPLAY="`who am i | awk '{print $6}' | cut -d\( -f2 | cut -d\)
You could do the same thing in any shell to see where you are
coming from:
who am i | awk '{print $6}' | cut -d\( -f2 | cut -d\) -f1

From: Heas <>
`who am i` should give you the info.

From: (when will I go home on time ?)
lastlog & utmp share a man page in 4.1.x. These are useful
files to start on if you are going to code something.

From: Chris Lyon <>
echo `hostname`

From: (Seiki Tatesawa)
I would also like to know the IP adress from shell level.
Could you foward the answer mails to me ?

From: Jas (Matthew K) <matt@uts.EDU.AU>
who am i | nawk '{print $6}' | tr -d '()'

From: Henry Unger <>
who am i gives
hunger pts/0 Jul 19 17:11 (
You can use cut from there.

From: (S. D. Raffensberger 500622500 (RD))
DISPLAY=`who am i | awk '{ print $6 }' | sed -e 's/(//' -e 's/)//'`

One of the features of tcsh is an environment variable REMOTEHOST
gives you exactly what you are looking for.

From: (Russ Poffenberger)
Look at the system call "gethostbyname".

From: (Dave Fabian)
echo "Which individual's IP address do you want ?"
read person
who | grep $person | ping -s `awk -F" " '{print $6}' | cut -d"(" -c2- | cut
-d")" -f1` count 1 | awk -f" " '{print $5}' | cut -d"(" -c2- | cut -d")" -f1 |
grep -v acke

From: (John Hearns - System Manager)
HOST = `who am i | awk '{ print $6 }'
ypcat hosts | grep HOST
then use awk to filter off the IP address
Alternatively, use nslookup $HOST ?

This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:10:30 CDT