Skip to main content
Question

NQE example similar to using grep and awk

  • 6 June 2024
  • 7 replies
  • 78 views

when going through devices with interface descriptions, for example

description “to site x interface gi0/0”;
description “to site y interfaace gi0/1”:

block pattern is 
description {desc:string}

Since it is a description with multiple words and space delimited, is there a way to get the entire description no matter how many words there are?  As the above would only get the first word.

similar to the way I would use grep for example:
grep '^description' ciscoconfig.txt

or if I wanted specific columns or text within lines beginning with description, I would grep and use awk like below

grep '^description' ciscoconfig.txt | awk '{print $2}'

grep '^description' ciscoconfig.txt | awk '{print $2 $4}'

grep '^description' ciscoconfig.txt | awk '{print $4 $2}'

Etc.

can anybody provide an NQE Example, Thanks

7 replies

Userlevel 4
Badge +1

Hello @RonAvila 

You can capture the entire string into a list

interface {string}
description {desc: (string*)}

 

Userlevel 4
Badge +2

Hi Ron,

I’m not sure if this is exactly what you’re looking for: But this will give you the output of:
Hostname ---- Interface ----- Description

I narrowed it down to just Cisco/IOS-XE with the “wheres” but you can adjust.

pattern = ```
interface {IntName:string}
description {desc: ((string)*)}
```;

foreach device in network.devices
where device.platform.vendor == Vendor.CISCO
let outputs = device.outputs
foreach command in outputs.commands
where command.commandType == CommandType.CONFIG
let parsed = parseConfigBlocks(OS.IOS_XE , command.response)
foreach match in blockMatches(parsed, pattern)



select {Hostname:device.name , Intf:match.data.IntName, Desc:match.data.desc}

 

Userlevel 4
Badge +2

Hello @RonAvila 

You can capture the entire string into a list

interface {string}
description {desc: (string*)}

 

You beat me!

Thanks all!

Now is it possible to parse through the strings by character? For example, if I wanted to extract site_a, site_b from each of the descriptions? I want to extract just one piece of the description for each interface. Can we iterate through strings?

Userlevel 4
Badge +1

@RonAvila String manipulation requires a little special handling, We will be improving this in future releases. But here is one example.


 

test = """
interface Ethernet1/1
description “to site x interface gi0/0”;
!
interface Ethernet1/2
description “to site y interface gi0/1”:
!
""";

trimLast(s) = prefix(s, length(s)-1);
trimFirstLast(s) = max(foreach x in [0] let p = prefix(s, length(s)-2) select suffix(p, length(p)-1));

pattern = ```
interface {IntName:string}
description {s: (string*)}
```;

stringParse = `to site {site: string} interface {int: string}`;


foreach x in [0]
let blocks = parseConfigBlocks(OS.UNKNOWN, test)
foreach match in blockMatches(blocks, pattern)
let newString = trimFirstLast(join(" ", match.data.s))
let site = patternMatch(newString, stringParse)
select {FromInterface: match.data.IntName, ToSite: site.site, ToInterface: site.int, }

 

Thanks @GaryB ! We will try this example!

Reply