Subject: Re: determining originating IP address of current rsh session
To: Laine Stump <lainestump@rcn.com>
From: Andrew Gillham <gillhaa@ghost.whirlpool.com>
List: netbsd-users
Date: 03/18/2000 14:17:41
Laine Stump writes:
> There must be a simple way to do this, and I'm just suffering from brain
> rot...
>
> I need for a shell script executed by rsh from another machine to tell
> me the IP address of the originating machine. For example, if I'm on
> 10.0.0.1 and I give the command "rsh 192.168.0.1 whatsmyip", it should
> display "10.0.0.1". (In practice I'll use rexec so that the person
> attempting to run the command (from some random IP address) is
> authenticated, but the principle should be the same.
I use the following 'getpeer.c' that I hacked up. I think I based it
on a NetBSD source file, but I apparently killed the Copyright.
Oops.
Anyway, this is what I use with some rsh scripts for dealing with
Cisco routers. YMMV.
-Andrew
--
-----------------------------------------------------------------
Andrew Gillham | This space left blank
gillham@whirlpool.com | inadvertently.
I speak for myself, not for my employer. | Contact the publisher.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
void printhost(struct sockaddr_in *);
void main() {
int addrlen;
struct sockaddr_in his_addr;
addrlen = sizeof(his_addr);
if (getpeername (0, (struct sockaddr *)&his_addr, &addrlen) <0 )
exit(1);
printhost(&his_addr);
}
void
printhost(sin)
struct sockaddr_in *sin;
{
char remotehost[128];
struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
sizeof(struct in_addr), AF_INET);
if (hp)
(void)strncpy(remotehost, hp->h_name, sizeof(remotehost));
else
(void)strncpy(remotehost, inet_ntoa(sin->sin_addr),
sizeof(remotehost));
remotehost[sizeof(remotehost) - 1] = '\0';
printf("%s\n", remotehost);
}