Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.unix.questions Subject: Re: Finding your remote host's name Message-ID: <8328@jpl-devvax.JPL.NASA.GOV> Date: 8 Jun 90 00:43:27 GMT References: <1990Jun6.091842.11335@dlcq15.datlog.co.uk> <1211@tuewsd.win.tue.nl> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 46 In article <1211@tuewsd.win.tue.nl> wswietse@lso.win.tue.nl (Wietse Venema) writes: : scm@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@jpl-devvax.jpl.nasa.gov