spstansbury has asked for the wisdom of the Perl Monks concerning the following question:

I am generating some demo files for a vulnerability reporting tool.

I have generated a list of IP addresses, and a list of NVD/CVE identifiers.

What I want to do is randomly assign a CVE vulnerability to each IP address.

In my mind I want the script to do this:

#!/usr/bin/perl $ip_file = 'ip_addrs'; $cve_file = 'cvd_ids'; srand; open(IPADDRS, $ip_file); open(CVEIDS, $cve_file); my $cve =[]; while (<IPADDRS>) { push(@addrs, $_) } foreach $addr (@addrs) { for $cve (<CVEIDS>) { rand($.) < 1 && ($cve = $_); print "$addr, $cve\n"; } }
But, of course, this doesn't work...

Any help would be greatly appreciated!

Best regards,

Scott...

Replies are listed 'Best First'.
Re: Need help assembling a list
by JavaFan (Canon) on Jan 27, 2009 at 15:08 UTC
    Assuming you have at least as many CVE's as IP addresses, I would read in the CVE's, shuffle them, and then assign the first IP address to the first CVE, the second IP address to the second CVE, etc.

    Unless the list of CVE's is so large you cannot slurp them into memory, this should work reasonably fast.

    Quick, dirty and untested code:

    use 5.010; use strict; use warnings; use Scalar::Util qw [shuffle]; my @ips = `cat ip_addresses`; my @cves = shuffle `cat cves`; chomp @ips; chomp @cves; while (@ips) { say "IP address $_ gets CVE ", shift @cves; }

      JavaFan I haven't used say or shuffle, but shouldn't it be the following?

      use 5.10; use List::Util qw[shuffle];
        but shouldn't it be the following?
        use 5.10;
        No:
        $ perl -M5.10 -e1 Perl v5.100.0 required (did you mean v5.10.0?)--this is only v5.10.0, +stopped. BEGIN failed--compilation aborted.
        use List::Util qw[shuffle];
        You are correct. shuffle is found in List::Util, not Scalar::Util.
      Thanks for your help! Scott...
Re: Need help assembling a list
by jethro (Monsignor) on Jan 27, 2009 at 15:39 UTC
    ...and if the number of CVEs is less or unknown compared to the IPs, change Javafan's program slightly
    $anz= @cves; while (@ips) { say "IP address $_ gets CVE ", $cves[int(rand($anz))]; }

    The shuffle isn't necessary in that case