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

I am new to Perl and I am trying to make a script that will pull my external IP address, then write the IP to a file with the date after it. I seem to be missing something here as it always gives me a result of could not find the IP. Did I not initialize or use my variables properly, or am I not parsing the site properly? Any help or pointers would be appreciated. Thanks in advance
#!/usr/bin/perl -w use strict; use warnings; use File::Slurp; use LWP::Simple; use Time::localtime; # Location of file to write to my $addresses = '/home/blimpton/Pro/logger/addresses.txt'; # Find out what the external IP is my $ip = get('https://automation.whatismyip.com/n09230945.asp'); $ip = 'Could find ip address!' if !$ip; # Current Date my $month = localtime->mon + 1; my $day = localtime->mday; my $year = localtime->year + 1900; my $current_date = "$month-$day-$year"; # Write to the file if ($ip = write_file($addresses, {append => 1}, $ip . ',' . $current_d +ate . "\n"));

Replies are listed 'Best First'.
Re: Getting external IP
by Kenosis (Priest) on Jun 28, 2012 at 22:37 UTC

    Try the following (site) to obtain your external IP address:

    chomp (my $ip = get('http://icanhazip.com'));
      Thanks that gave me my desired results :) Though I am not sure, why that works and mine did not. Time to read about chomp and re-read about get again. Then just need to figure out the last thing to not record a duplicate IP in the file.

        Glad it worked! I'm guessing that the issue was mainly the site you were using, as http://icanhazip.com/ returns just a plain external IP address.

Re: Getting external IP
by Corion (Patriarch) on Jun 28, 2012 at 22:04 UTC

    Most likely, you are missing the module LWP::Protocol::https for accessing https modules and thus the request to your URL fails and returns an empty string.

      I tried adding that, installed on my system and still no dice. I also tried removing the "s" so it was http and removed the added LWP::Protocol::https and still gives the same result.
Re: Getting external IP
by Anonymous Monk on Jun 29, 2012 at 11:32 UTC
    Also note that what works okay from home might not be useful in a big corporate network, with lots of buffers and bounces between where you are and the big world outside.