bartender1382 has asked for the wisdom of the Perl Monks concerning the following question:

Working on simple Perl scripts, that get called via a web browser.

Have a few "make a log" functions, mostly for debugging. But since users can be guest, would also like to log the calling IP.

Is it possible to get that in the scripts?

  • Comment on Can my perl scripts get the IP address that called the script?

Replies are listed 'Best First'.
Re: Can my perl scripts get the IP address that called the script?
by Fletch (Bishop) on Apr 08, 2022 at 18:09 UTC

    Possibly, but because you've given no details about how this putative script is actually being run no one's going to be able to help you precisely. For example CGI has a remote_host method, Mojo::Transaction::HTTP has remote_address, yadda yadda yadda.

    Be more specific in your questions (preferably with an Short, Self-Contained, Correct Example) and you'll get better answers.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Can my perl scripts get the IP address that called the script?
by hippo (Archbishop) on Apr 08, 2022 at 18:40 UTC

    Assuming CGI, say $ENV{REMOTE_HOST}


    🦛

      You're right I should have stated CGI.
      So I added:

      my $q = new CGI; my $userIP = $q->remote_addr();

      And it works. Is there a way to mark a question closed?

        Hello bartender1382 and welcome to the monastery and to the wonderful world of perl!

        > .. I should have stated CGI

        ..and many of us could have answered: we wait for you in XXI century :)

        No seriously and without any offending intent, you can glance CGI to understand some basic concepts but then (or from the beginning) look at something contemporary like Dancer2 or Mojolicious

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        "Is there a way to mark a question closed?"

        There's no need to do that. In fact, doing so may result in other, relevant information not being posted. Just leave the thread as is: you'll get a notification is someone else replies to you.

        — Ken

        This might be fun for you to add to those two lines:

        use Net::Whois::IP ('whoisip_query'); # This module is not installed +automatically with a # Perl installation, so you'll + need to install it # from CPAN in order to be abl +e to use its # whois_ip_query function. use Fcntl (':flock'); if ($userIP) { my $location_of_some_log_file = '/var/logs/my_log_file.log'; open ( my $log_fh, '>> :encoding(UTF-8)', $location_of_some_log_fi +le ) or die "FATAL: Trouble opening '$location_of_some_log_file' for +appending: $!"; flock ( $log_fh, LOCK_EX ); my $ip_whois_response = whoisip_query($userIP); if ($ip_whois_response) { print {$log_fh} "Running whois on IP address '", $userIP, "':\ +n\n"; foreach ( sort keys( %{$ip_whois_response} ) ) { print {$log_fh} "$_ $ip_whois_response->{$_} \n"; } } else { print {$log_fh} qq|Cannot run whois to do an IP lookup, becaus +e no response received when invoking whoisip_query method of Perl mod +ule Net::Whois::IP on IP '$userIP'\n\n|; } close $log_fh or warn "WARNING: Some trouble closing file handle f +or '$location_of_some_log_file': $!"; }
        EDITED 4/12/2022 -- corrected "use Net::Whois::IP ('whois_ip_query');" to read "use Net::Whois::IP ('whoisip_query');"
Re: Can my perl scripts get the IP address that called the script?
by misterperl (Friar) on Apr 13, 2022 at 14:06 UTC
    The replies here are over-simplified. First,  $ENV{REMOTE_ADDR}

    isn't always defined, depending on VPN, proxies, etc.

    Second, you were seeking an IP- $ENV{REMOTE_HOST} is a name, which can presumably resolve to an IP, but the same network restrictions may apply for both.

    Last,

    my $q = new CGI; my $userIP = $q->remote_addr();
    probably uses %ENV as well, so the IP may or may not be defined.

    in the simple-case, you MAY be able to get the IP. In a secure corporate case like ours, it dubious. I suspect the replies that stated use  $ENV{REMOTE_ADDR} only use this on simple networks. In my world and perhaps yours, things just aren't that easy;I wish they we're since this *simple* IP is in-fact a major PAIN for us.

      No, if you're running in a CGI environment then per the spec then REMOTE_ADDR must be set to the address of the client sending the request. Now due to proxies and other network vagaries that might not be the IP address the client machine uses on its local network (which is another question / issue; if you think it should be the "real" IP the client knows itself as you're mistaken about what CGI provides), but it will be what the serving machine sees the connection as having come from.

      And yes the environment variables are what CGI will use to set its members (because that's how CGI works). Again, it's correct per the spec and presuming a functioning webserver implementing the CGI interface they'll be there.

      Edit: Of course if you're not running as a CGI script then that's a different horse of another color. But the limitation remains; if you're running under (say) mod_perl then that's going to be populating from the address stashed in the request structure (same that mod_cgi would use to fill out those environment variables) but (again) that address is going to be what the server sees the client connection as originating from. There's no magic that's going to make that show the "real" local network address of the client if there's proxies or NAT or avian carriers or what have you in the mix.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.