regex - Perl regular expressions to capture parts of command output -
edited question : have added third function. have come regex seems correct . (1st , 2nd function work expected. )
i have written couple of functions in library, , call functions test script. i'm having issues regular expressions. can me out regular expressions?
function 1:
sub ipsec_version { ($self) = @_; $cmd = 'sudo -s ipsec version '; $version = 0; #execute command $self->execute($cmd); foreach $line ( @{ $self->get_stdout() } ) { if ( $line =~ m/strongswan/msx ) { $version = $1; } } return $version; }
function call:
$self->{'ipsec_version'} = $self->{'ipsec_obj'}->ipsec_version(); info('[startup] ipsec version : ' . $self->{'ipsec_version'} );
actual output:
use of uninitialized value in concatenation (.) or string @ ... line 37. ipsec version :
expected output:
strongswan u5.1.2/k3.16.0-30-generic
command output:
i need script capture expected output string this
linux strongswan u5.1.2/k3.16.0-30-generic institute internet technologies , applications university of applied sciences rapperswil, switzerland see 'ipsec --copyright' copyright information.
function 2:
sub ipsec_status { ($self,$connection_name) = @_; $cmd = 'sudo -s ipsec status ' . $connection_name; $status = 0; #execute command $self->execute($cmd); foreach $line ( @{ $self->get_stdout() } ) { if ( $line =~ m/security\sassociations\d\()/ ) { $status = $1; } } return $status; }
function call:
$self->{'ipsec_status'} = $self->{'ipsec_obj'}->ipsec_status('connection'); ('[startup] ipsec status : ' . $self->{'ipsec_status'} );
actual output:
info [startup] ipsec status : 0
expected output:
security associations (1 up, 0 connecting)
command output:
i need script capture expected output string this
security associations (1 up, 0 connecting): connection[3]: established 3 seconds ago, 1.1.1.19[1.1.1.19]...10.81.1.50[10.81.1.50] connection{3}: installed, tunnel, esp in udp spis: cb343e86_i abf6d1f2_o
function 3 :
sub ipsec_restart { ($self) = @_; $cmd = 'sudo -s ipsec restart'; $restart = 0; $self->execute($cmd); foreach $line ( @{ $self->get_stdout() } ) { if ( $line =~ /(starting strongswan.*ipsec$)/ ) { $restart = $1; last; } } return $restart; }
function call :
$self->{'ipsec_restart'} = $self->{'ipsec_obj'}->ipsec_restart(); ('[startup] ipsec restart status : ' . $self->{'ipsec_restart'} );
expected output : see highlighted text below. checked in https://regex101.com/ . regex seems correct. /(starting strongswan.*ipsec$)/
starting strongswan 5.1.2 ipsec actual output : 0
the problem you're using $1
set if there captures in regular expression using. also, should add last
don't go on searching matching line once have found one. lastly, wrong habitually add /m
, /s
, /x
modifiers every regex match regardless of whether make difference, , leading m/
necessary if using other default slash delimiters.
the first function should contain
for $line ( @{ $self->get_stdout } ) { if ( $line =~ /(strongswan.*\s)/i ) { $version = $1; last; } }
and second function similar, although looks you're trying match security associations
followed digit, isn't in text. have small a
instead of capital, fine if use /i
modifier. captures security associations
, including last closing parenthesis on same line. need?
for $line ( @{ $self->get_stdout } ) { if ( $line =~ /(security\s+associations.*\))/i ) { $status = $1; last; } }