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?
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;
}
Comment