Whois.pl

Iš Žinynas.
13:01, 25 vasario 2025 versija, sukurta \dev\null (Aptarimas | indėlis) (Naujas puslapis: Daro whois surašytiems į '''ips.txt''' ip adresams ir subnetams, rezultatus surašo į '''whois_results.csv''' <syntaxhighlight lang="perl"> #!/usr/bin/perl # sudo apt install...)
(skirt) ← Ankstesnė versija | Dabartinė versija (skirt) | Vėlesnė versija → (skirt)
Jump to navigation Jump to search

Daro whois surašytiems į ips.txt ip adresams ir subnetams, rezultatus surašo į whois_results.csv

#!/usr/bin/perl
# sudo apt install libnet-whois-ip-perl libtext-csv-perl

use strict;
use warnings;
use Net::Whois::IP qw(whoisip_query);
use Text::CSV;
use Data::Dumper;

# Input and output files
my $input_file  = "ips.txt";
my $output_file = "whois_results.csv";

# Open input file
open(my $fh, '<', $input_file) or die "Cannot open $input_file: $!";

# Open output CSV file
my $csv = Text::CSV->new({ binary => 1, eol => "\n" });
open(my $out_fh, '>', $output_file) or die "Cannot open $output_file: $!";
$csv->print($out_fh, ["IP", "Country", "Org-Name"]);

# Process each line
while (my $line = <$fh>) {
    chomp $line;

    # Extract IP address from CIDR notation if present
    if ($line =~ m{^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)}) {
        my $ip = $1;
        print "Processing: $ip\n";

        # Perform whois lookup
        my $whois = whoisip_query($ip);
        print Dumper($whois);
        my $country = $whois->{Country} // "Unknown";
        my $org_name = $whois->{Organization} // $whois->{org} // "Unknown";
        my $person = $whois->{Person};

        # Write to CSV
        $csv->print($out_fh, [$line, $country, $org_name, $person]);
    }
}

# Close files
close $fh;
close $out_fh;

print "Whois lookup completed. Results saved to $output_file\n";