Home Forums Programming/Scripting Perl Display website information using Perl

This topic contains 0 replies, has 1 voice, and was last updated by  Sanjeev Jaiswal 2 years, 5 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2752

    Sanjeev Jaiswal
    Keymaster

    We have explained the logic and sample code snippets at http://www.aliencoders.org/content/how-use-perl-fetch-website-details/
    This script will gather all the information like:

    • DNS Servers
    • IP Address
    • Mail Servers
    • Google Page Rank
    • Alexa Rank
    • Creation Date
    • Expiration Date

    As of now we display only this information. This script may not work 100% for all domain names and it will not work for sub domains.
    This script is platform independent provided you have Perl installed and have all the necessary modules installed :)

    Later we may edit the script to get more information like :

    • Site Owner’s Address
    • Site Owner’s Email Id
    • Site Owner’s Contant Number
    • Registrar Info

    Here is the Perl Code :
    [shcode=perl]
    #!/usr/bin/perl
    ##################################################################################
    # Author: Sanjeev Kumar Jaiswal
    # Date: Feb, 2013, Updated on Feb, 2015
    # Script: siteinfo.pl
    # Description: This script will print relevant information
    # for the website passed while running the script through command line.
    # Like: Mail Servers, Name Servers, IP Address, Google Page Rank, Alexa Rank,
    # Creation Date, Expiration Date, Domain Exists or not
    # One can tweak the script to get more information
    # like registrar info, contact details, email id of the site owner etc
    #################################################################################
    use strict;
    use warnings;
    use Net::DNS;
    use Net::Whois::Raw qw( whois );
    use WWW::Google::PageRank;
    use LWP::Simple;

    my $res = Net::DNS::Resolver->new;

    # Perform a lookup, using the searchlist if appropriate.
    my $domain_name = $ARGV[0] or die “Please pass domain name at command linen”;

    my $site = whois(“$domain_name”);

    #Check if domain name exists or not before processing further
    die “$domain_name doesn’t exists as of now, try some other domainn” unless(domain_exists());

    print “*


    Site Info for $domain_name


    *n”;
    print “n*


    Google Page Rank and Alexa Rank


    *n”;
    get_google_pagerank($domain_name);
    get_alexa_rank($domain_name);
    print “*


    *n”;

    print “n*


    Mail Servers


    *n”;
    get_mail_server($domain_name);
    print “*


    *n”;

    print “n*


    IP and Name Servers


    n”;
    get_ip($domain_name);
    get_name_server($domain_name);
    print “*


    *n”;

    print “n*


    Site’s Other details


    *n”;
    get_site_details();
    print “*


    *n”;

    ################ Subroutines #########################
    #Checking Domain existense
    sub domain_exists{
    return 0 if($site =~ m/No match for/igs);
    return 1;
    }

    # Prints Alexa Rank
    sub get_alexa_rank{
    my $domain = shift;
    my $url = “http://www.alexa.com/siteinfo/$domain”;
    my $content = get($url);

    #Regular Expression to Find Global Rank
    my ($alexa_rank) = $content =~ m#globe-sm.jpg(?:.*?)”>(.*?)?#gis;
    print “Alexa Rank: $alexa_rankn”;
    }

    # Prints IP Address(es)
    sub get_ip{
    my $domain = shift;
    my $reply = $res->search($domain);

    if ($reply) {
    foreach my $rr ($reply->answer) {
    next unless $rr->type eq “A”;
    print $rr->address, “n”;
    }
    } else {
    warn “query failed: “, $res->errorstring, “n”;
    }
    }

    # Prints Google Page Rank
    sub get_google_pagerank{
    my $domain_name = shift;
    my $page_rank = WWW::Google::PageRank->new();
    my $rank = $page_rank->get(“http://www.$domain_name”);
    print “Google Page Rank: $rank n”;
    }

    # Prints Name Server for $domain_name
    sub get_name_server{
    my $domain = shift;
    my $query = $res->query(“$domain”, “NS”);
    if ($query) {
    foreach my $rr (grep { $_->type eq ‘NS’ } $query->answer) {
    print $rr->nsdname, “n”;
    }
    }
    else {
    warn “query failed: “, $res->errorstring, “n”;
    }
    }

    # prints mail server(s) list if any
    sub get_mail_server{
    my $domain = shift;
    my @mx = mx($res, “$domain”);
    if(scalar @mx){
    foreach my $rr (@mx) {
    print $rr->preference, ” “, $rr->exchange, “n”;
    }
    }
    else{
    print “No Mail servers found for $domainn”;
    }
    }

    sub get_site_details{
    # Created on: 06-Jan-11 or Created on……: or Registration Date:
    my ($created_type) = $site =~ m/((?:Creation|Created|Registration)s+.*?s*:.*)/im;
    print “$created_typen”;

    # Expires on: 06-Jan-16 or Expiration Date:
    my ($expire_type) = $site =~ m/((?:Expiry|Expires|Expiration)s+.*?s*:.*)/im;
    print “$expire_typen”;

    # Last Updated on: 16-Jan-13
    my ($update_type) = $site =~ m/((?:Updated|Update|modified)s+.*?s*:.*)/im;
    print “$update_typen”;

    # Registrant Name:foo foo
    my ($r_name) = $site =~ m/(Registrants+Names*:.*)/im;
    print “$r_namen”;

    # Registrant Phone:+91.1111111111
    my ($r_ph) = $site =~ m/(Registrants+Phones*:.*)/im;
    print “$r_phn”;

    # Registrant Email: foo@abc.com
    my ($r_email) = $site =~ m/(Registrants+Emails*:.*)/im;
    print “$r_emailn”;
    }
    [/shcode]

    If you have any idea or wish to suggest code editing/addition please comment here.

    [size=large]Updates:[/size]
    Updated Code to print:
    1. IP Address
    2. Registrant Name
    3. Registrant Phone
    4. Registrant Email
    5. Previous code was not working for Alexa Rank properly as Alexa site has changed its layout after Amazon bought it ;)
    6. Mail servers code were failing, fixed that too
    7. Edited code to get whois information more efficiently with simpler Regular Expression and easily readable format.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.