cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2619
Views
7
Helpful
9
Replies

EEM Scripting using regexp to return all matches

kmd
Level 1
Level 1

I am working on a script in EEM that uses regexp to return results regarding 4g signal strength and GPS coordinates on a IR829 router.

This is the current script. 

event manager applet test7
event none
action 010 cli command "enable"
action 020 cli command "show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long"
action 030 regexp "([\_0-9\\-]+)" "$_cli_result" match result
action 040 file open test7.TXT flash:test7.TXT a+
action 050 file puts test7.TXT nonewline "$result"
action 060 file close test7.TXT

I am having issues with regexp as it is only returning the first match. For example the current output of the script only shows the current RSSI. I would like the script to return all values listed in the show cellular 0 command. See below:

Current Result: 
-59

Wanted Result:

-59 -94 -16 2.4 38 23 20.5188 96 51 19.6884

Is it possible to get regexp to return all the matches or is there a better method to retrieve the results.

Any help would be greatly appreciated, thanks.

 

 

9 Replies 9

M02@rt37
VIP
VIP

Hello @kmd,

event manager applet test7
event none
action 010 cli command "enable"
action 020 cli command "show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long"
action 030 set counter 0
action 040 foreach line "$_cli_result" {
    action 050 regexp "([\_0-9\\-]+)" "$line" match result
    action 060 file open test7.TXT flash:test7.TXT a+
    action 070 file puts test7.TXT nonewline "$result "
    action 080 file close test7.TXT
    action 090 incr counter
}

A counter is introduced to keep track of the number of matches found. The "foreach" loop iterates over each line in the output of  the  "show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long" command. Inside the loop, the "regexp" action extracts the desired values from each line and appends them to the file "test7.TXT", separated by a space.

 

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

Thanks for the response, M02@rt37 

I have tried to put the script on the router but it doesn't like the "}" at the end of the script and comes back with "% Invalid input detected at '^' marker."

You don't specify the curly brackets at all - simply "action end" at the end of the loop block.
So:

action 100 end

 Command reference: https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/eem/command/eem-cr-book/eem-cr-a1.html#wp3468947737

Hi @Rich R , I used the action 100 end and the script now runs. The problem is that it still only returns the first match. Is there something else not right in the script that is making it stop on the first match?

Output of script:

TEST-ROUTER#more test7.TXT
-47

See current script below:

event manager applet test7
event none
action 010 cli command "enable"
action 020 cli command "show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long"
action 030 set counter "0"
action 040 foreach line "$_cli_result" "{"
action 050 regexp "([\_0-9\\-]+)" "$line" match result
action 060 file open test7.TXT flash:test7.TXT a+
action 070 file puts test7.TXT "$result "
action 080 file close test7.TXT
action 090 increment counter
action 100 end

 

 

I think your problem there is using "{" as your line delimiter - there aren't even any of those in the result you're looking at so there will never be a match!
Looking at example at https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/eem/configuration/15-mt/eem-15-mt-book/eem-policy-cli.html.xml

action 100 foreach int $poll_interfaces
action 101  cli command "en"
action 102  cli command "show int $int summ | beg ------"
action 103  foreach line $_cli_result "\n"
action 105  regexp ".*[0-9]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s+.*" $line junk rxps 
action 106   if $_regexp_result eq 1
action 107    if $rxps gt $max_rx_rate
action 108     syslog msg "Warning rx rate for $int is > than threshold. Current value is $rxps
 (threshold is $max_rx_rate)"
action 109    end
action 110   end
action 111  end
action 112 end

You should probably also be using the new-line delimiter for your end of line match?
I also don't think the counter variable is serving any purpose so can be removed altogether as you set to zero then increment on each loop but it never gets used anywhere.

Thanks for that @Rich R , I used your advice and have now revised the script again. It's working well but I am now running into an issue where I need multiple matches on a single line from the cli result.

Below is the result from the show command in the script.  And the result from the current script. In the latitude and longitude lines, there are three results in each. The current script is only returning the first result from each line (Shown in bold). I need the script to return the other values from longitude and latitude (underlined) I'm unsure on what to change in the script to also allow it to return multiple matches from a single line.

Additional help on this would be awesome, thanks.

show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long

Current RSSI = -56 dBm
Current RSRP = -87 dBm
Current RSRQ = -12 dB
Current SNR = 3.0 dB
Latitude: 38 Deg 23 Min 20.5188 Sec North
Longitude: 96 Deg 51 Min 19.6884 Sec North

Output from current script:

-56 -87 -12 3 38 96

Output that I am trying to achieve:

-56 -87 -12 3 38 23 20.5188 96 51 19.6884

Here is a copy of the current script.

event manager applet test7
event none
action 1 cli command "enable"
action 2 cli command "show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long"
action 3 foreach line "$_cli_result" "\n"
action 3.1 regexp "([\_0-9\\-]+)" "$line" match result
action 3.2 file open test7.TXT flash:test7.TXT a+
action 3.3 file puts test7.TXT nonewline "$result "
action 3.4 file close test7.TXT
action 3.5 end

 

balaji.bandi
Hall of Fame
Hall of Fame

Can you post the output of below command :

show cellular 0 all

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

The output from show cellular 0 all is super long and it has a lot of information I don't need.

This is the output from: show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long
Current RSSI = -56 dBm
Current RSRP = -87 dBm
Current RSRQ = -12 dB
Current SNR = 3.0 dB
Latitude: 0 Deg 0 Min 0 Sec North
Longitude: 0 Deg 0 Min 0 Sec North

KhanAkhtar
Level 1
Level 1

To retrieve multiple matches using regular expressions (regexp) in EEM scripting, you can modify your existing script to use a loop. Here's an updated version of the script that will capture all the desired values:

event manager applet test7
event none
action 010 cli command "enable"
action 020 cli command "show cellular 0 all | i RSRP|RSRQ|Current SNR|RSSI|Lat|Long"
action 030 regexp "(\\S+)" "$_cli_result" matchall result
action 040 file open test7.TXT flash:test7.TXT a+
action 050 foreach item in result
action 060 file puts test7.TXT nonewline "$item "
action 070 end
action 080 file puts test7.TXT ""
action 090 file close test7.TXT

In this updated script, the "regexp" command is modified to use (\\S+), which matches any non-whitespace character sequence. The "matchall" keyword is used to capture all the matches into the "result" variable.

The script then opens the file, enters a loop using "foreach" to iterate over each item in the "result" list, and writes each item to the file with a space delimiter. Finally, it adds a newline to separate each set of matches and closes the file.

With this modification, the script will capture and write all the desired values to the output file, separated by spaces.

Note: Make sure you have sufficient file system space available on your router to accommodate the output file. Adjust the file path and name if necessary.

Review Cisco Networking for a $25 gift card