[comp.unix.questions] GETTING HOSTNAME FROM UTMP

michiel@baan.UUCP (Michiel Wories) (01/30/91)

Howdy folks,

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.

NOTE: who gives this output:
michiel    ttyp0        Jan 30 10:33  (bv8c55.baan.nl)
I'm interested in this entry ----------^

Many thanks,

--------------------------------------------------------
Michiel Wories @ Baan International, The Netherlands
..!hp4nl!baan!michiel

moss@cs.umass.edu (Eliot Moss) (01/31/91)

The versions of who with which I am familiar get the hostname from the ut_host
part of the utmp entry. They skip printing if the first character is null
(inidicating there is no hostname present in the utmp entry).
--

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206, 545-1249 (fax); Moss@cs.umass.edu

ag@cbmvax.commodore.com (Keith Gabryelski) (02/05/91)

[Followups to comp.unix.questions.]

In article <994@baan.UUCP> michiel@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