Last updated on November 22nd, 2024 at 11:03 am

A script designed to determine your IP address has been created. Two methods for locating the IP address are provided. This Perl script utilizes CGI to obtain and present the client’s IP address.

1] Using remote_addr() function

2] Using the environmental variable $ENV{REMOTE_ADDR} in perl.

Let us take a look at how the script looks. The remote_addr() uses CGI module.

#!/usr/bin/env perl
use strict;
use warnings;
use CGI;

# Create a new CGI object
my $q = CGI->new();

# Start the HTML output
print $q->header('text/html');
print $q->start_html('Client IP Address');

# Retrieve the IP address using CGI module and environment variable
my $ip_via_cgi = $q->remote_addr() // 'Unknown';
my $ip_via_env = $ENV{REMOTE_ADDR} // 'Unknown';

# Display the results
print $q->h3('IP Address using CGI method:');
print $q->p($ip_via_cgi);

print $q->h3('IP Address using ENV method:');
print $q->p($ip_via_env);

# End the HTML output
print $q->end_html;

Since the second method shown above is just grabbing ip address from perl environmental variable there is no need of calling CGI module. This is much easier method without any complications.

You can use #!/usr/bin/env perl for better portability. The script reveals the client’s IP address. Be cautious about displaying or logging this information, as it can have privacy implications.

Usage:

  1. Save the script as ip_address.pl.
  2. Place it in your web server’s CGI-bin directory.
  3. Access the script via a web browser, e.g., http://yourserver/cgi-bin/ip_address.pl.

HTML Output

<html>
  <head><title>Client IP Address</title></head>
  <body>
    <h3>IP Address using CGI method:</h3>
    <p>192.168.1.100</p>
    <h3>IP Address using ENV method:</h3>
    <p>192.168.1.100</p>
  </body>
</html>