Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Can my perl scripts get the IP address that called the script?

by bartender1382 (Beadle)
on Apr 08, 2022 at 19:12 UTC ( [id://11142855]=note: print w/replies, xml ) Need Help??


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

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?

  • Comment on Re^2: Can my perl scripts get the IP address that called the script?
  • Download Code

Replies are listed 'Best First'.
Re^3: Can my perl scripts get the IP address that called the script?
by Discipulus (Canon) on Apr 08, 2022 at 19:58 UTC
    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.
Re^3: Can my perl scripts get the IP address that called the script?
by kcott (Archbishop) on Apr 09, 2022 at 01:32 UTC
    "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

Re^3: Can my perl scripts get the IP address that called the script?
by davebaker (Pilgrim) on Apr 09, 2022 at 17:12 UTC

    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');"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2026-04-19 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.