GETTING HOSTNAME FROM UTMP

Keith Gabryelski ag at cbmvax.commodore.com
Tue Feb 5 06:36:04 AEST 1991


[Followups to comp.unix.questions.]

In article <994 at baan.UUCP> michiel at baan.UUCP (Michiel Wories) writes:
>Does anybody know a standard way to retrieve the hostname from
>/etc/utmp or getting this hostname any other standard way?  It should
>work for UNIX V.2 through UNIX V.4 and BSD (...).  I am aware of the
>fact /etc/utmp hasn't always a ut_host member in its structure.

V.2 and V.3 didn't have a mechanism for figuring out where a remote
login came from.  Some V.2 and V.3 developers hacked in some support
for this but could not modify utmp's format because of binary
compatibility.  You may be able to use finger(1) on such systems but
finger(1) will not display the current user so you may have to
do something similar to:

	#!/bin/sh
	finger >/tmp/Crock$$
	grep `tty | sed -n 's:^/dev/::p'` /tmp/Crock$$
	rm -f /tmp/Crock$$

SVR4 has this new crock called /etc/utmpx which is a shadow of the
/etc/utmp file.  utmpx keeps track of the bsd ut_host field.  If these
files get out of sync--You're f*cked; login will not work.  If you do
not have a way to boot to single user or from floppy, prepare to
re-install.  finger is the only command under SVR4 that displays
ut_host.  See program above.

BSD has ``who am i'' which is useful (WARNING: untested code):

	$ who am i | sed -n 's/^.*(\([^)]*\)).*$/\1/p'

or

	$ set `who am i`
	$ echo $6

Pax, Keith



More information about the Comp.unix.questions mailing list