39 posts categorized "perl"

10/16/2014

OpenSSL Version Matrix Updated - October 2014

I updated my OpenSSL Version Matrix again to reflect new versions of OpenSSL released since June 5 2014, including the three new versions of OpenSSL that were released yesterday, (October 15, 2014) to address four security issues.

05/27/2014

iControl Perl get_member_v2 Example

This first fairly useful iControl example using Perl and SOAP::Lite solves a fairly time-consuming problem if you want to print all the pools and pool members from one of your BigIP LTM's and you're not really sure how to do it with the tmsh commands (or tmsh scripting). If you have a large number of pools and are using a web browser, you could spend hours clicking on a pool, then clicking the Members tab, then clicking on Pools, then selecting the next pool, then the Members tab for *that* pool, etc. Very time consuming.

This example uses the get_member_v2() method. It first grabs all the pools on that ltm using the get_list() method, then loops through each one and prints the pool followed by the pool members using get_member_v2().

There are other examples of this script out there but this one is the first version of the one I wrote for my own job. Some values have been sanitized and replaced with placeholders, ($host, $uid, and $pwd in my script). If you were to copy and paste this script verbatim into your new Perl script, it won't work until you replace those values.

#!/usr/bin/env perl

use Modern::Perl;
use Mozilla::CA;
use SOAP::Lite;
#use SOAP::Lite +trace => [ qw (all -transport) ];

# Pick one or the other. If the former doesn't work
# try the latter
use iControlTypeCast; # Same directory as your script
# --OR--
#use lib '[/path/to/your/icontrol/modules/directory/]';

$ENV{HTTPS_CA_FILE} = Mozilla::CA::SSL_ca_file();

# IMPORTANT - Values in ALL_CAPS are placeholders
# Swap out the values with your own values

my $host = "IP_ADDRESS_OR_HOSTNAME_OF_YOUR_BIGIP";
my $port = "443"; # Usually...

# Replace with your userid and password
# An account with Operator rights is all that is required here
my $uid = "YOUR_ICONTROL_USERID";
my $pwd = "YOUR_ICONTROL_PASSWORD";

sub SOAP::Transport::HTTP::Client::get_basic_credentials {
    return "$uid" => "$pwd";
    }

# This is the base request
my $req = SOAP::Lite
    -> uri('urn:iControl:LocalLB/Pool')
    -> proxy("https://$host:$port/iControl/iControlPortal.cgi");

# Add authorization header; Otherwise the
# initial request will fail
    eval { $req->transport->http_request->header (
        'Authorization' => 'Basic ' . MIME::Base64::encode("$uid:$pwd", '')); };

# Grab a list of all pools on this BigIP
my $pools = $req->get_list();
my @list = @{$pools->result};
say "BigIP LTM: $host";

# Iterate through the list of pools and get the pool members.
foreach my $pool (@list) {
    say "    $pool";
    my $poolmember = $req->get_member_v2(
        SOAP::Data->name(pool_names => [$pool])
    );

    my @memberListAofA = @{$poolmember->result};
    my @memberListA = @{$memberListAofA[0]};

    foreach my $member_def (@memberListA) {
        my $address = $member_def->{"address"};
        my $port = $member_def->{"port"};
        say "        $address:$port";
    }
}

04/19/2014

Convert Imperva Performance Report CSV from Epoch Date to Human Readable

Imperva SecureSphere lets you download Performance Reports, which are dumped into a CSV file. Timestamps are in 13-digit unix epoch format, which aren't much fun to read. Also, every value in the CSV file is quoted, so if you want to do anything interesting with the data in Perl, you first need to get rid of all the quotes.

The script below uses just two CPAN modules, Modern::Perl and Text::CSV, (note that it will not run without some additional re-work using Text::CSV_XS). Modern::Perl probably isn't strictly needed since I am not really using any newer Perl features but I always use it by default now.

It's pretty quick. It converts an 8500+ test CSV of mine in about 1.7 seconds. It takes one argument, which is the name of the CSV file you want to convert. You can then pipe the output to stdout or redirect it to a new file.

#!/usr/bin/env perl

use Modern::Perl;
use Text::CSV;

my $csv = Text::CSV->new({
	binary => 1,
	auto_diag => 1,
	allow_loose_quotes => 1,
	sep_char => ',',
    });

my $file = $ARGV[0] or die "Need to supply CSV file as argument to $0\n";

my $time;
open (my $data, '<', $file) or die "Could not open file '$file' $!\n";
while (my $line = <$data>) {
	chomp $line;
	# Needed this next line because the Text::CSV->new options
	# above were not removing all the quotes from the CSV
	$line =~ s/"//gm;

	if ($csv->parse($line)) {
		my @fields = $csv->fields();
		$time = $fields[0];
        my @ts = localtime($time/1000);
        $ts[4] += 1;
        $ts[5] += 1900;
        say sprintf('%d-%02d-%02d %02d:%02d:%02d', @ts[5,4,3,2,1,0]) . "," . $fields[1] . "," . $fields[2] . "," . $fields[3] . "," . $fields[4] . "," . $fields[5] . "," . $fields[6];
	} else {
        warn "Line could not be parsed: $line\n";
	}
}

As an aside, chromatic has updated his Modern Perl book, which is available in paperback format on Amazon. Kindle format should be released soon. The 2014 edition has been updated for Perl 5.18.