By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Reading IP address in sql database using perl language.

P: 1
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?
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. @userinput = <STDIN>
  3. foreach (@userinput)
  4. @timeData = localtime(time);
  5. entry_time = join(' ', @timeData);  
  6. {
  7. $dbh = DBI->connect("DBI:mysql:host=[hostname];
  8. database=ip_registry","[user]",
  9. "[password]",{PrintError=>0,RaiseError=>1});
  10. $sth = $dbh->do("DELETE from ip_map");
  11. print "\n\nData from ip_map table dropped\n\n";
  12. $sth = $dbh->prepare("INSERT into ip_map values (?,?,?,?)");
  13. $count = 0;
  14. open (PROCESS, "<$file");
  15. while ($line = <PROCESS>) 
  16. {
  17. chomp ($line);
  18. if (($line =~ m/\|ipv4\|/) and ($line !=~ m/\*/)) {
  19. ($registrar,$country_code,$latitude,$longitude,$ASN,$num_ip,$entry_time,
  20.  ) = split(/\|/, $line);
  21. @octets_start = split(/\./, $start_ip);
  22. $long_start = 0;
  23. foreach $octet_start (@octets_start) {
  24. $long_start <<= 8;
  25. $long_start |= $octet_start;
  26. }
  27. $long_end = $long_start + ($num_ip-1);
  28. $count += $sth->execute($country_code,$registrar,$long_start,
  29.  $long_end);
  30. }
  31. }
  32. close PROCESS;
  33. }
Apr 28 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 3,503
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:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. 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.


Apr 28 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.