NineTail
NineTail

Reputation: 223

Regex multiple filters

Need help with a regex to select multiple lines. From what I have so far it works until I put the last regex in. Once I do that, it skips over the middle one. Passwords have been removed for security purposes.

My Goals

  1. Select all lines beginning at Current configuration ending at version, "or the ! right before it"
  2. Select all lines with "secret"
  3. Select all lines with "password"

My Regex

(?s)Current configuration (.*)NVRAM|secret 5 +(\S+)|password 7 +(\S+)

EDIT: took about spaces before and after the | and it seems to highlight what i want. It doesn't do the entire line though.

Test Data

Building configuration...

Current configuration : 45617 bytes
!
! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
!
version 15.0
no service pad
!
no logging console
enable secret 5 ***encrypted password***
!
username admin privilege 15 password 7 ***encrypted password***
username sadmin privilege 15 secret 5 ***encrypted password***
aaa new-model
!
ip ftp username ***encrypted password***
ip ftp password 7 ***encrypted password***
ip ssh version 2
!
line con 0
 password 7 ***encrypted password***
 login authentication maint
line vty 0 4
 password 7 ***encrypted password***
 length 0
 transport input ssh
line vty 5 15
 password 7 ***encrypted password***
 transport input ssh
!

Desired Result:

Building configuration...

!
version 15.0
no service pad
!
no logging console
enable 
!
username admin privilege 15 
username gisadmin privilege 15 
aaa new-model
!
ip ftp username cfgftp
ip ftp 
ip ssh version 2
!
line con 0

 login authentication maint
line vty 0 4

 length 0
 transport input ssh
line vty 5 15

 transport input ssh
!

Upvotes: 0

Views: 107

Answers (1)

Eduardo Lynch Araya
Eduardo Lynch Araya

Reputation: 824

This Should Work.

REGEXP:

((?:\bCurrent configuration)(?:.*\n?){6})$|((?:\bsecret)(?:.)+$)|((?:\bpassword\s)(?:.)+$)

INPUT:

Building configuration...

Current configuration : 45617 bytes
!
! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
!
version 15.0
no service pad
!
no logging console
enable secret 5 ***encrypted password***
!
username admin privilege 15 password 7 ***encrypted password***
username sadmin privilege 15 secret 5 ***encrypted password***
aaa new-model
!
ip ftp username ***encrypted password***
ip ftp password 7 ***encrypted password***
ip ssh version 2
!
line con 0
 password 7 ***encrypted password***
 login authentication maint
line vty 0 4
 password 7 ***encrypted password***
 length 0
 transport input ssh
line vty 5 15
 password 7 ***encrypted password***
 transport input ssh
!

OUTPUT:

Group: $1:

Current configuration : 45617 bytes
!
! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
!
version 15.0

GROUP $2:

enable secret 5 ***encrypted password***

GROUP $3:

password 7 ***encrypted password***
password 7 ***encrypted password***
password 7 ***encrypted password***
password 7 ***encrypted password***
password 7 ***encrypted password***

See: https://regex101.com/r/NOrndn/1

Test Python Code: http://ideone.com/UyjT38

Upvotes: 1

Related Questions