in reply to Can my perl scripts get the IP address that called the script?

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.

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

    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.