Finding the originating host on a network login

Blair P. Houghton bph at buengc.BU.EDU
Wed Oct 25 14:48:46 AEST 1989


In article <MONTNARO.89Oct23141238 at sprite.crd.ge.com> <montanaro at crdgw1.ge.com> (Skip Montanaro) writes:
>In article <3539 at amelia.nas.nasa.gov> samlb at pioneer.arc.nasa.gov (Sam Bassett RCD) writes:
>
>	   On my Ultrix 3.0 system here (4.3BSD derived), 'who' gives me
>   just that info.  I am not an expert on SysV 'who's, but I know that
>   they're different . . .
>
>I think it depends how good a job the vendor has done incoporating all the
>little odds'n'ends that go into "Berkeley UNIX compatibility". Stellar's
>'who' command prints the originating host, while HP-UX (at least as of 6.5)
>does not.

Screw 'who':

-----------------------------Clip 'n' compile-------------------------------
#include <stdio.h>
#include <utmp.h>

/* Where the current logins are stored. */
#define LOGFILE "/etc/utmp"

extern char *rindex();

main()
{
    char *tty, *ttyname(), *nptr;
    struct utmp U;
    FILE *fp, *fopen();

    fp = fopen( LOGFILE, "r");

    /* Get stdin's tty's name. */
    tty = ttyname(0);

    /* "/dev/ttyxx" ==> "ttyxx" */
    nptr = 1 + rindex(tty,'/');

    /* Loop through login log, printing remote	*
     * host for all that have this tty's name.	*/
    while( fread( (char *)&U, sizeof(struct utmp), 1, fp ) )
	if ( (*U.ut_name != '\0') && (strcmp(nptr, U.ut_line) == 0) ) 
	    printf( "%s\n", U.ut_host);
}
----------------------------------------------------------------------------

This is a rather thoroughly altered version of something
I got from someone else.  I forget who.  (No doubt
their lawyers will remind me :-).

				--Blair
				  "An oldie but a goodie."



More information about the Comp.unix.questions mailing list