Subject: Re: determining originating IP address of current rsh session
To: Laine Stump <>
From: Andrew Gillham <>
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
> and I give the command "rsh whatsmyip", it should
> display "". (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.

Anyway, this is what I use with some rsh scripts for dealing with
Cisco routers.  YMMV.

Andrew Gillham                            | This space left blank                     | 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 )


        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));
                (void)strncpy(remotehost, inet_ntoa(sin->sin_addr),
        remotehost[sizeof(remotehost) - 1] = '\0';

	printf("%s\n", remotehost);