Extracting a Substring Based on Regular Expression Matching
A string can be extracted from a larger string using the following syntax:
env.getRegexpMatch(stringToMatch, someRegularExpression, occurrence)
This example gets the third occurrence of a sequence of numbers followed by a space,
followed by a sequence of letters within string outputLine:
<# value := env.getRegexpMatch(outputLine, "[0-9]+ [a-zA-Z]+", 3) #>
This example gets the interface value from an interface string:
<# interface := env.getRegexpMatch(interface, "[0-9]+/[0-9]+", 1) #>
Adding Global Variables for Availability to the onError Macro
Global variables can be set in any macro and retrieved without being explicitly passed
in another macro. The global variable is set with the following syntax:
env.setVar(name, value)
The name is a quoted string and the value can be a character string or number.
The global variable is retrieved with the following syntax:
value := env.getVar(name).
The name is a quoted string and the value is the value stored by an earlier env.setVar.
A macro can contain one onError macro. The onError macro is like any other macro file
template. There can only be one macro with the name onError in the macro file.
In this example, the macro sets a global variable before using it in a configuration
command:
<# badInt #>
<# interface := "9/0" #>
<# env.setVar("interface",interface) #>
config t
interface fast <# interface; '\n' #>
ip add 7.6.5.4 255.255.255.0
<#endtmpl#>
<# onError #>
<# c := env.getVar("interface") #>
<# setoutput console #>
<# "begin output\n" #>
<# "The interface value: ";c; "\n" #>
<# endsetoutput #>
end
<#endtmpl#>
When the macro runs, the global variable interface is set and the interface command
contains an invalid interface value. The CLI reports a syntax error and the macro onError
is called. Within the onError macro, the global variable interface is retrieved.
463
Copyright © 2010, Juniper Networks, Inc.
Chapter 8: Writing CLI Macros
Summary of Contents for JUNOSE 11.3
Page 6: ...Copyright 2010 Juniper Networks Inc vi...
Page 8: ...Copyright 2010 Juniper Networks Inc viii JunosE 11 3 x System Basics Configuration Guide...
Page 24: ...Copyright 2010 Juniper Networks Inc xxiv JunosE 11 3 x System Basics Configuration Guide...
Page 32: ...Copyright 2010 Juniper Networks Inc 2 JunosE 11 3 x System Basics Configuration Guide...
Page 146: ...Copyright 2010 Juniper Networks Inc 116 JunosE 11 3 x System Basics Configuration Guide...
Page 166: ...Copyright 2010 Juniper Networks Inc 136 JunosE 11 3 x System Basics Configuration Guide...
Page 432: ...Copyright 2010 Juniper Networks Inc 402 JunosE 11 3 x System Basics Configuration Guide...
Page 488: ...Copyright 2010 Juniper Networks Inc 458 JunosE 11 3 x System Basics Configuration Guide...
Page 524: ...Copyright 2010 Juniper Networks Inc 494 JunosE 11 3 x System Basics Configuration Guide...
Page 554: ...Copyright 2010 Juniper Networks Inc 524 JunosE 11 3 x System Basics Configuration Guide...
Page 566: ...Copyright 2010 Juniper Networks Inc 536 JunosE 11 3 x System Basics Configuration Guide...
Page 588: ...Copyright 2010 Juniper Networks Inc 558 JunosE 11 3 x System Basics Configuration Guide...
Page 613: ...PART 3 Index Index on page 585 583 Copyright 2010 Juniper Networks Inc...
Page 614: ...Copyright 2010 Juniper Networks Inc 584 JunosE 11 3 x System Basics Configuration Guide...
Page 632: ...Copyright 2010 Juniper Networks Inc 602 JunosE 11 3 x System Basics Configuration Guide...