Reading IP address in sql database using perl language.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prudhvinath
    New Member
    • Apr 2010
    • 1

    Reading IP address in sql database using perl language.

    Could you solve this problem. Write a simple utility program in the perl language which will read in a list of IPv4 addresses, one per line, from STDIN and then either insert or update records in an SQL database. The database records will record the IPv4 address, the location of the IP address in the world meaning the country, region, longitude and latitude, and the autonomous system number (ASN) which is most recently associated with the IPv4 address. Additionally records should include timestamps for when the record is created and separately when/if it is updated.

    I wrote a code for this. Is it correct?
    Code:
    #!/usr/bin/perl
    @userinput = <STDIN>
    foreach (@userinput)
    @timeData = localtime(time);
    entry_time = join(' ', @timeData);  
    {
    $dbh = DBI->connect("DBI:mysql:host=[hostname];
    database=ip_registry","[user]",
    "[password]",{PrintError=>0,RaiseError=>1});
    $sth = $dbh->do("DELETE from ip_map");
    print "\n\nData from ip_map table dropped\n\n";
    $sth = $dbh->prepare("INSERT into ip_map values (?,?,?,?)");
    $count = 0;
    open (PROCESS, "<$file");
    while ($line = <PROCESS>) 
    {
    chomp ($line);
    if (($line =~ m/\|ipv4\|/) and ($line !=~ m/\*/)) {
    ($registrar,$country_code,$latitude,$longitude,$ASN,$num_ip,$entry_time,
     ) = split(/\|/, $line);
    @octets_start = split(/\./, $start_ip);
    $long_start = 0;
    foreach $octet_start (@octets_start) {
    $long_start <<= 8;
    $long_start |= $octet_start;
    }
    $long_end = $long_start + ($num_ip-1);
    $count += $sth->execute($country_code,$registrar,$long_start,
     $long_end);
    }
    }
    close PROCESS;
    }
    Last edited by numberwhun; Apr 28 '10, 05:24 PM. Reason: Please use CODE TAGS!
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    You have a lot of syntactical errors in your code. You need to start by making the 2nd and 3rd lines of the file be:

    Code:
    use strict;
    use warnings;
    Once you do that, run your code and correct any issues that are reported.

    Also, I see you are using DBI, but you don't have a DBI module 'use' statement anywhere.

    Your code could also use a fair amount of cleaning up to be a lot more readable. From the perspective of someone who would support this, I wouldn't, personally, but that is my opinion.

    Do the above, get past the syntactical errors and such and then please come back with a question other than "is this correct". The best way to determine that is to run your code. If you had, you would have had a fair amount of errors, especially using the above pragmas.

    Regards,

    Jeff

    Comment

    Working...