Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 26 Mar 2013 09:27:59 +1100
From:      "Daniel O'Callaghan" <danny@clari.net.au>
To:        freebsd-questions@freebsd.org
Subject:   Re: Remote IP to script over inetd
Message-ID:  <5150CF6F.7000208@clari.net.au>
In-Reply-To: <5150305F.6000205@starnet.cz>
References:  <5150305F.6000205@starnet.cz>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi Radek,

On 25/03/2013 10:09 PM, Radek Krejc(a wrote:
> #!/bin/sh
> echo $REMOTEHOST

getpeername() info is not available in Bourne shell directly.
You need to use perl or C or ....

So you can do something like this:

----------------------

    #!/usr/bin/perl

    # Example code from http://perldoc.perl.org/functions/getpeername.html
    use Socket;
         $hersockaddr    = getpeername(SOCK);
         ($port, $iaddr) = sockaddr_in($hersockaddr);
         $herhostname    = gethostbyaddr($iaddr, AF_INET);
         $herstraddr     = inet_ntoa($iaddr);   # $herstraddr is of form
    "12.34.56.78"

    # Now you know the remote IP address, do what we want to do
    # eg run a shell script and put IP into $1
    $cmd = "/root/redirects.sh $herstraddr";
    exec($cmd);

----------------------

PS, if you are just sending an HTTP redirect or similar, you don't need 
to run the script as root.

Cheers,

Danny



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5150CF6F.7000208>