scm@dlcq15.datlog.co.uk (Steve Mawer) (06/06/90)
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. I need this to run on ULTRIX V3 (DECsystem 3100), AIX V2 and V3 (RT PC and RISC system/600), Unisys 5000 UTS and SCO Xenix on Compaqs using SCO TCP/IP (and probably Excelan, but I'm not keen on this one :-(). -- Steve C. Mawer <scm@datlog.co.uk> or < {backbone}!ukc!datlog!scm > Voice: +44 1 863 0383 (x2153)
wswietse@lso.win.tue.nl (Wietse Venema) (06/07/90)
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 */ #include <stdio.h> #include <sys/types.h> #include <sys/param.h> #include <sys/socket.h> #include <syslog.h> #include <netinet/in.h> #include <netdb.h> #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN BUFSIZ /* BSD 4.2 ?? */ #endif main(argc, argv) int argc; char **argv; { int length; struct sockaddr sa; struct sockaddr_in *sin = (struct sockaddr_in *) (&sa); char host_name[MAXHOSTNAMELEN]; struct hostent *hp; char *inet_ntoa(); void exit(); void syslog(); length = sizeof(sa); 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); }
subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (06/08/90)
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 */ < Extensive C code deleted > YOW! I don't see at all why it has to be that complicated. We can just do this: set from = `who am i | cut -d'(' -f2 | cut -d')' -f1` and do with $from as we wish. -- subbarao@{phoenix,bogey or gauguin}.Princeton.EDU -|Internet kartik@silvertone.Princeton.EDU (NeXT mail) -| subbarao@pucc.Princeton.EDU - Bitnet
Anselmo-Ed@cs.yale.edu (Ed Anselmo) (06/08/90)
>>>>> On 7 Jun 90 18:21:12 GMT, subbarao@phoenix.Princeton.EDU (Kartik Subbarao) said:
Kartik> YOW! I don't see at all why it has to be that complicated. We can just
Kartik> do this:
Kartik> set from = `who am i | cut -d'(' -f2 | cut -d')' -f1`
Kartik> and do with $from as we wish.
This certainly doesn't work with all versions of "who" (I just tried
it on a Sun, a Xenix machine, an ATT unix/386 machine, something
running mach, and an apollo). Some versions of "who am i" show the
machine name, some don't. Even on the ones that do show the machine
name, the name is typically trucated to 16 or so characters, e.g
"bigbird.cf.cs.ya" instead of "bigbird.cf.cs.yale.edu".
--
Ed Anselmo anselmo-ed@cs.yale.edu {harvard,decvax}!yale!anselmo-ed
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (06/08/90)
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
del@thrush.mlb.semi.harris.com (Don Lewis) (06/08/90)
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 */ > [ code deleted ] > > if (getpeername(0, &sa, &length) >= 0) { ^^^^^^^^^^^^^ this won't work. [ code deleted ] Hmn. Fd 0 (or 1 or 2) is not connected to a socket when you rlogin. It is connected to a pseudo tty. Rlogind, or its equivalent sits between the other side of the pty and the socket. Therefore, getpeername() won't work, unless you make a habit of using rsh for remote logins ;-). -- Don "Truck" Lewis Harris Semiconductor Internet: del@mlb.semi.harris.com PO Box 883 MS 62A-028 Phone: (407) 729-5205 Melbourne, FL 32901
wswietse@lso.win.tue.nl (Wietse Venema) (06/08/90)
In article <1211@tuewsd.win.tue.nl> I write: > if (getpeername(0, &sa, &length) >= 0) { to obtain the name of the host one is logged in from. Obviously, this does not work when stdin is connected to a pty. Oops. Good thing this ain't comp.unix.wizards.