SUMMARY: Getting peer IP address from shell level

From: Michael Ryan (mike@networx.ie)
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,
Mike
<mike@networx.ie>

-------------------------------------------------------------
Summary of answers:

From: Al.Venz@seag.fingerhut.com (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\)
-f1`:0"
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 <heas@nexen.com>
`who am i` should give you the info.

--------------------------------------------------
From: pallan@clare.risley.aeat.co.uk (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 <chrisl@tpc.bt.co.uk>
echo `hostname`

--------------------------------------------------
From: tate@cig.nml.mot.com (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 <hunger@hitech.com>
who am i gives
hunger pts/0 Jul 19 17:11 (foobar.hitech.com)
You can use cut from there.

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

--------------------------------------------------
From: white@uvm-gen.emba.uvm.edu
One of the features of tcsh is an environment variable REMOTEHOST
which
gives you exactly what you are looking for.

--------------------------------------------------
From: poffen@San-Jose.ate.slb.com (Russ Poffenberger)
Look at the system call "gethostbyname".

--------------------------------------------------
From: dfabian@efit.elcm.eds.com (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: johnh@gerbil.umds.ac.uk (John Hearns - System Manager)
First
HOST = `who am i | awk '{ print $6 }'
then
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