Finding your remote host's name

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Fri Jun 8 10:43:27 AEST 1990


In article <1211 at tuewsd.win.tue.nl> wswietse at lso.win.tue.nl (Wietse Venema) writes:
: scm at dlcq15.datlog.co.uk (Steve Mawer) writes:
: 
: >If I `rlogin' from machine1 to machine2, is there a simple and (relatively)
: >portable way to find out on machine2 the name of machine1?  I'd like to do
: >this from a shell script if possible, but I'm willing to write C code if
: >necessary.
: 
: /* fromhost - print name of host we are logged in from */
: 
: [preliminaries deleted]
: 
:     if (getpeername(0, &sa, &length) >= 0) {
: 	if (sa.sa_family == AF_INET) {
: 	    if (hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
: 				   sizeof(sin->sin_addr.s_addr), AF_INET))
: 		(void) printf("%s\n", hp->h_name);
: 	    else
: 		(void) printf("%s\n", inet_ntoa(sin->sin_addr));
: 	    exit(0);
: 	}
:     }
:     exit(1);

Nice program.

But it won't solve the stated problem.  Your fd 0 is only going to be
attached to the socket if the user did an rsh, not if they did an rlogin.
Under rlogin it will be attached to a pseudo terminal.  The only solutions
are to dredge it out of the utmp file with who or some other dredger, or
get VERY fancy following pointers back through the kernel, or rewrite your
rlogind to salt it away somewhere such as your environment.

Oh, by the way, just for the fun of it, the Perl version of fromhost is:

#!/usr/bin/perl

($family, $port, $inetaddr) = unpack("S n a4", getpeername(STDIN));

die "Can't get peer name: $!\n" if $inetaddr eq '';
die "Not internet address\n" if $family != 2;

print +(gethostbyaddr($inetaddr,2))[0] || join('.',unpack('C4',$inetaddr)),"\n";

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.questions mailing list