in reply to Re: Getting IP from URL
in thread Getting IP from URL

Sorry I didn't explain it very well. If I have a URL such as http://www.somedomain.com/this_page.html I don't want to do anything. On the other hand, if I have a URL such as http://10.1.1.1/this_dir/this_page.html I wish to extract the IP address which in this case would be 10.1.1.1.

Looks like your first solution just might do the trick!

Thanks much!

Replies are listed 'Best First'.
Re: Re: Re: Getting IP from URL
by rob_au (Abbot) on Oct 04, 2002 at 13:18 UTC
    Hrmmm, in light of this clarification, I think something like the following may be best ...

    use Socket; use URI; my $url = 'http://www.mydomain.com'; my $uri = URI->new( $url ); my $ip_addr = gethostbyname( $uri->host ); $ip_addr = inet_ntoa( $ip_addr ); if ( $uri->host eq $ip_addr ) { #... Creatures evolve, code does stuff }

    The advantage that this method of employing the URI module over a simple regular expression is the correct handling of more complex URLs (which may incorporate username and password authentication details in the form of scheme://username:password@host:port/) and validation of IP addresses.

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001001")),"\n"'

      There really isn't any need for that socket stuff.
      use URI; my $url = 'http://foo.bar.com'; my $uri = new URI($url); if( $uri->host =~ /[a-zA-Z\-]/ ) { # or /[^\d\.]/ # it's an IP dag-nabbit }

      ____________________________________________________
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        Agreed, this approach does simplify things somewhat - The incorporation of the Socket stuff does however have the advantage of ensuring that the IP address (hostname) extracted is valid.

         

        perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001010")),"\n"'