#!/usr/bin/perl use strict; # PGP Professional Services, Andreas Zengel, azengel@pgp.com, June 2010 # This is an example on how to grab PGP Command Line output and extract a UUID from the output # This example will make use of regex to trigger on UUIDs to make sure only real UUIDs are filtered out # Certain PGP Command Line operations together with PGP Key Managament Server will output a UUID that # you will probably need for some of the next operations. Exmaples are: # Example 1. Creating a MAK # pgp --usp-server keys.senderdomain.com --create-mak --name "TestMak1" # will return on STDERR # TestMak1:create MAK (0:Created as 6c36e781-d1aa-47e4-87e2-a20d7e7c1045) # Example 2. Creating a MEK Series # pgp --usp-server keys.senderdomain.com --create-mek-series --name "TestMekSeries1" --parent 6c36e781-d1aa-47e4-87e2-a20d7e7c1045 # will return on STDERR # TestMekSeries1:create MEK Series (0:Created as 87b0ca5f-c356-4ce2-829f-746aee74ff59) # Example 3. Searching for MAKs by name # pgp --usp-server keys.senderdomain.com --search-mak "RE(NAME,""senderdomain"")" # can output several UUIDS like: #UUID Mode Name #------------------------------------ ---- ---- #140f5bdd-f117-4137-a6d7-98ec68ff5824 GKM Alice Jones #75283b4d-de13-4e00-94b0-929857d13aac CKM Bob Knight #e1966bdf-48be-4b7a-a9ee-9ed9e5c37b36 SKM senderdomain.com # define out three example outputs that can result from PGP Command Line operations my $output1 = 'TestMak1:create MAK (0:Created as 6c36E781-d1aa-47e4-87e2-a20d7e7c1045)'; my $output2 = 'TestMekSeries1:create MEK Series (0:Created as 87b0ca5f-c356-4ce2-829f-746aee74ff59)'; my $output3 = 'UUID Mode Name ------------------------------------ ---- ---- 140f5bdd-f117-4137-a6d7-98ec68ff5824 GKM Alice Jones 75283b4d-de13-4e00-94b0-929857d13aac CKM Bob Knight e1966bdf-48be-4b7a-a9ee-9ed9e5c37b36 SKM senderdomain.com'; my $output4 = 'This is not a uuid here since to short c36e781-daa-4e4-8e2-a0d7e7c1045)'; my ($uuid,@uuids); # This is a regex that only matches on correctly formated UUIDs my $uuidRegex = '([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})'; print "Matching a UUID with a regex example starting:\n"; # Parse the uuid from example 1 and print it out print "Example 1:\nPGP Command Line output was '$output1'\n"; $uuid = getUUIDFromOutput($output1); if ($uuid){ print "Found uuid '$uuid' in this string\n"; } else { print "Couldn't find a UUID in this string\n"; } # Parse the uuid from example 2 and print it out print "\nExample 2:\nPGP Command Line output was '$output2'\n"; $uuid = getUUIDFromOutput($output2); if ($uuid){ print "Found uuid '$uuid' in this string\n"; } else { print "Couldn't find a UUID in this string\n"; } # Parse the uuids from example 3 and print them out print "\nExample 3:\nPGP Command Line output was '$output3'\n"; @uuids = getUUIDArrayFromOutput($output3); if (@uuids){ print "Found '". ($#uuids + 1) ."' uuids in this string\n"; foreach (@uuids){ print " Found uuid '$_' in this string\n"; } } else { print "Couldn't find a UUID in this string\n"; } # try to parse the uuid from example 4 and print out the result print "\nExample 4:\nPGP Command Line output was '$output4'\n"; $uuid = getUUIDFromOutput($output4); if ($uuid){ print "Found uuid '$uuid' in this string\n"; } else { print "Couldn't find a UUID in this string\n"; } print "Finished example\n"; # The function getUUIDFromOutput will take a string as input and will return the first UUID found in this string # if no UUID is found, then it will return 0. sub getUUIDFromOutput{ my $output = shift; # perform a regex match on the output string, in multiline mode, not case sensitive if ($output =~ /$uuidRegex/im){ # if a match was found, return the first match return $1; } else { # If no match was found, return 0 return 0; } } # The function getUUIDArrayFromOutput will take a string as argument and will return all UUIDs found in this string as # a perl array. if only one UUID is found, it will return an array with only one entry. If no UUID is found it will return 0. sub getUUIDArrayFromOutput{ my $output = shift; my @result; # perform a regex match on the output string, in multiline mode, not case sensitive, globally while ($output =~ /$uuidRegex/isg){ # if a match was found, push the result on top of the array push(@result,$1); } # now if the array is empty still, return 0 if ($#result < 0){ # If no match was found, return 0 return 0; } else { # Else return the array return @result; } }