Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Split on a new line

by Luken8r (Novice)
on Jul 03, 2007 at 15:10 UTC ( [id://624705]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to parse some information from a SSL cert into some variables and would like each line in its own element. Here is my cert data:

Certificate: Data: Version: 3 (0x2) Serial Number: 3235 (0xca3) Signature Algorithm: md5WithRSAEncryption Issuer: C=US, O=GTE Corporation, CN=GTE CyberTrust Root Validity Not Before: Oct 22 17:02:00 2002 GMT Not After : Oct 22 23:59:00 2003 GMT Subject: C=US, ST=XXXX, L=XXXXX, O=XXXX Inc., OU=Security, CN= +XXXXXXX.com/emailAddress=XXXX@XXXX.com Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public Key: (1024 bit) Modulus (1024 bit): ...KEY... Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE X509v3 Key Usage: Key Encipherment X509v3 Certificate Policies: Policy: 1.2.840.113763.1.2.1.4 CPS: http://www.XXXXXX.com/XXX/XXX.html Signature Algorithm: md5WithRSAEncryption ...KEY... subject= /C=US/ST=XXXX/L=XXXXX/O=XXXXX Inc./OU=Security/CN=XXXXX issuer= /C=US/O=GTE Corporation/CN=GTE CyberTrust Root

So anyway, here is what I have:

my $SSL = "/usr/local/bin/sudo /usr/bin/openssl"; local (*FH); open (FH, "$SSL x509 -inform PEM -text -noout -subject -issuer -i +n /tmp/expiredcert.crt 2>/dev/null |") or return; my ($var1, $var2) = 0; while (<FH>) { my @args = split(/[\n\r\l]*/); $var1 = $args[1]; $var2 = $args[2]; } close FH; print " $var1 \n $var2 \n";
=====

Eventually, I want to get the X509v3 Data out of this cert into $varX to put elsewhere.

This was just my starting point to see if this would work. How do I get the split() to split each element on the end of line? This isnt working the way I anticipated, My output is

s s

Replies are listed 'Best First'.
Re: Split on a new line
by jasonk (Parson) on Jul 03, 2007 at 16:38 UTC

    Unless you've done something to $/ which you haven't mentioned, then $_ won't contain newlines inside the while, since the while is already splitting the data up by line.

    If you want each line in it's own element in the array, then you are going about it the hard way...

    my $SSL = "/usr/local/bin/sudo /usr/bin/openssl"; chomp( my @array = `$SSL x509 -inform PEM -text -noout -subject -issue +r -in /tmp/expiredcert.crt 2>/dev/null` );

    Or, if you still want to do it with open...

    my $SSL = "/usr/local/bin/sudo /usr/bin/openssl"; local (*FH); open (FH, "$SSL x509 -inform PEM -text -noout -subject -issuer -in /tm +p/expiredcert.crt 2>/dev/null |") or return; my @args = <FH>; close FH;

    We're not surrounded, we're in a target-rich environment!
      This works pretty well. I added a join to get almost what I want.
      my $SSL = "/usr/local/bin/sudo /usr/bin/openssl"; my ($var1, $var2, $var3, $var4, $var5); my $joinvar; chomp( my @array = `$SSL x509 -inform PEM -text -noout -subject -issue +r -in /tmp/expiredcert.crt 2>/dev/null` ); $joinvar = join("\n", @array[25..30]),
Re: Split on a new line
by jwkrahn (Abbot) on Jul 03, 2007 at 15:25 UTC
    Your problem is in using a modifier that will match a zero-length string. Change split(/[\n\r\l]*/) to split(/[\n\r\l]+/). Also, why do you have the letter 'l' in there? ([\n\rl] is the same as [\n\r\l].)
      Replacing the * with + helped, however args[0] is the very last line and args1 is empty. Perhaps they arent new lines?
Re: Split on a new line
by odrm (Novice) on Jul 03, 2007 at 15:29 UTC
    Idiomatic perl uses split() when you know what you want to throw away and a regular expression match when you know what you want.

    Putting a "zero or more characters from this class" regular expression into split (as in split /\n\r\l*/) effectively splits a string into individual characters - the regex is true between every single character. Your code would get further if you tried split /\n\r\l+/ ....

    Using regular expression matches, you could do something like:

    #!/usr/bin/perl use strict; use warnings; undef $/; # read whole file at once our $cert = <>; our ($info) = $cert =~ m/X509v3 Basic Constraints:\s+([^\n]+)/; print "Constraints = $info\n";
Re: Split on a new line
by moritz (Cardinal) on Jul 03, 2007 at 15:22 UTC
    Without having looked further: don't use an asterisk * in a regex where you mean a plus +. You don't want to split at the empty substring.
Re: Split on a new line
by arpad.szasz (Pilgrim) on Jul 04, 2007 at 10:48 UTC

    Maybe using a X.509 certificate parser from CPAN, like Crypt::X509 would be easier to extract what data You want from the certificate.

    :)))))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://624705]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found