Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Determine ip address/hostname of requesting host

by davidj (Priest)
on Sep 21, 2006 at 19:11 UTC ( #574237=perlquestion: print w/ replies, xml ) Need Help??
davidj has asked for the wisdom of the Perl Monks concerning the following question:

My fellow monks,

Her's my situation:
I have been asked by my employer to write a web-based application where employees evaluate departmental procedures. Due to privacy matters, logins are required to access the application and view the content. This part is easy enough. I want to implement an additional security measure where I am emailed of failed login attempts. This, too, is easy enough.

Here's my need:
Where I'm stuck is that I will need to determine the ip address of the machine from which the login attempt failed. Since we use dhcp I will also need to determine the hostname of said machine. Unfortunately, my knowledge of http requests is lacking in this level of the protocol. My google searches and SuperSearch attempts have been fruitless so far.

As always, any assistance you can provide, modules you can point me to, etc is greatly appreciated.
davidj

Comment on Determine ip address/hostname of requesting host
Re: Determine ip address/hostname of requesting host
by ikegami (Pope) on Sep 21, 2006 at 19:14 UTC

    Is this a CGI application? CGI servers set $ENV{REMOTE_ADDR} to the IP address, and sometimes (configurable in Apache) set $ENV{REMOTE_HOST} to the reverse lookup of the IP address.

Re: Determine ip address/hostname of requesting host
by liverpole (Monsignor) on Sep 21, 2006 at 19:24 UTC
    Hi davidj, As ikegami recommends, use CGI.

    Personally, I like the remote_host() command, (which you can get from reading the CGI documentation):

    use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); my $ipaddr = remote_host(); # Now log the address $ipaddr ...

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
          use CGI::Carp qw(fatalsToBrowser);

      One comment: Don't use fatalsToBrowser in a production environment. It could potentially provide an information leak. In production CGI::Carp is going to log to your web server's log files any spew that comes forth. The user just sees something went wrong without knowing what.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        blue_cowdawg,

            Don't use fatalsToBrowser in a production environment.

        Thanks; that is a good tip.

        To date, all of my own CGI experience has been limited to a local network, where the consumers of my work are mostly other developers, so it's seemed natural to provide as much detail as possible.  But I've had in the back of my mind that it probably isn't what you'd want to do in a larger (ie. visible-to-the-outside) context.

        I'm glad you gave that advice, which I'll remember in case I ever do happen to develop for a "production environment".


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Determine ip address/hostname of requesting host
by derby (Abbot) on Sep 21, 2006 at 19:33 UTC

    where employees evaluate departmental procedures ...logins are required ...

    <sarcasm>Nothing like honest feedback, is there?</sarcasm>

    -derby
Re: Determine ip address/hostname of requesting host
by blue_cowdawg (Monsignor) on Sep 21, 2006 at 21:09 UTC
        Where I'm stuck is that I will need to determine the ip address of the machine from which the login attempt failed.

    On a non-production machine that you don't care about try the following very evil code:

    #!/usr/bin/perl -w use CGI qw/ :standard /; my $cgi=new CGI; print header,start_html; print h1("Environment"); print table( Tr(th("Environment Variable"),th("Value")), map { Tr(td($_),td($ENV{$_})) } sort keys %ENV ); print hr; print h1('CGI Varialbes'); print table ( Tr(th("parameter"),th("parameter value")), map { Tr(td($_),td($cgi->param($_))) } sort $cgi->param ) ; print end_html;
    I call this evil because besides what you are looking for is some information that you really don't want to leak to the "public."

    Of particular interest in your case is the following key and value that are found in the %ENV hash:

    REMOTE_ADDR 68.37.227.52
    There's other information there as well but, this was what you specifically asked for.

    Update: (GAH!) ikegami got his post out there before I hit "submit" Oh well, the code I put up there is good stuff anyway if you want to "play."


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://574237]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-18 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (61 votes), past polls