0
# Command and RPC Execution
1
2
Command execution capabilities for running CLI commands and RPC operations on Junos devices, including support for multiple output formats, structured and unstructured data retrieval, and XML processing.
3
4
## Capabilities
5
6
### CLI Command Execution
7
8
Execute CLI commands on Junos devices with support for multiple output formats including text, XML, and JSON, along with timeout and normalization options.
9
10
```python { .api }
11
def cli(self, command, format='text', warning=True):
12
"""
13
Execute CLI command on the device.
14
15
Parameters:
16
- command (str): CLI command to execute
17
- format (str): Output format ('text', 'xml', 'json')
18
- warning (bool): Display warning messages
19
20
Returns:
21
- str: Command output in requested format
22
23
Raises:
24
- RpcTimeoutError: Command execution timeout
25
- RpcError: Command execution error
26
"""
27
```
28
29
### RPC Execution
30
31
Execute Remote Procedure Call (RPC) operations directly using XML structures, providing low-level access to all Junos device operations.
32
33
```python { .api }
34
def execute(self, rpc_cmd, **kwargs):
35
"""
36
Execute RPC command on the device.
37
38
Parameters:
39
- rpc_cmd (Element): XML RPC command element
40
- **kwargs: Additional RPC parameters
41
- dev_timeout (int): RPC timeout in seconds
42
- filter_xml (str): XML filter for response
43
- ignore_warning (bool): Ignore warning messages
44
- normalize (bool): Normalize XML response
45
46
Returns:
47
- Element: XML response element
48
49
Raises:
50
- RpcTimeoutError: RPC execution timeout
51
- RpcError: RPC execution error
52
"""
53
```
54
55
### Dynamic RPC Access
56
57
Access to dynamic RPC methods through the rpc property, allowing method-style access to all Junos RPC operations with automatic parameter handling.
58
59
```python { .api }
60
@property
61
def rpc(self):
62
"""
63
RPC meta-object providing dynamic access to all Junos RPC operations.
64
65
Usage:
66
- dev.rpc.get_interface_information()
67
- dev.rpc.get_route_information(destination='0.0.0.0/0')
68
- dev.rpc.load_configuration(configuration=config_xml)
69
70
Returns:
71
- _RpcMetaExec: RPC meta-execution object
72
"""
73
```
74
75
### XML and RPC Utilities
76
77
Utility functions for working with XML RPC commands, converting CLI commands to RPC format, and displaying RPC equivalents of CLI commands.
78
79
```python { .api }
80
def display_xml_rpc(self, command, format='xml'):
81
"""
82
Display the XML RPC equivalent of a CLI command.
83
84
Parameters:
85
- command (str): CLI command
86
- format (str): Output format for the command
87
88
Returns:
89
- str: XML RPC representation of the CLI command
90
"""
91
92
def cli_to_rpc_string(self, command, format='text', dev_timeout=None):
93
"""
94
Convert CLI command to RPC string format.
95
96
Parameters:
97
- command (str): CLI command to convert
98
- format (str): Output format
99
- dev_timeout (int): Timeout for the operation
100
101
Returns:
102
- str: RPC string representation
103
"""
104
```
105
106
### Template Processing
107
108
Jinja2 template processing for configuration generation and complex command construction with variable substitution and logic.
109
110
```python { .api }
111
def Template(self, filename):
112
"""
113
Load and return a Jinja2 template for configuration generation.
114
115
Parameters:
116
- filename (str): Template filename (searches current directory and module templates)
117
118
Returns:
119
- Template: Jinja2 template object
120
121
Usage:
122
- template = dev.Template('interface-config.j2')
123
- config = template.render(interface='ge-0/0/0', description='WAN Link')
124
"""
125
```
126
127
## Usage Examples
128
129
### Basic CLI Commands
130
131
```python
132
from jnpr.junos import Device
133
134
dev = Device(host='router1.example.com', user='admin', passwd='secret')
135
dev.open()
136
137
# Execute CLI command with text output
138
version_info = dev.cli('show version')
139
print(version_info)
140
141
# Execute CLI command with XML output
142
interfaces_xml = dev.cli('show interfaces', format='xml')
143
144
# Execute CLI command with JSON output
145
interfaces_json = dev.cli('show interfaces', format='json')
146
147
dev.close()
148
```
149
150
### RPC Operations
151
152
```python
153
from jnpr.junos import Device
154
155
dev = Device(host='router1.example.com', user='admin', passwd='secret')
156
dev.open()
157
158
# Execute RPC using dynamic method access
159
interfaces = dev.rpc.get_interface_information()
160
161
# Execute RPC with parameters
162
routes = dev.rpc.get_route_information(destination='0.0.0.0/0')
163
164
# Execute RPC with detailed parameters
165
bgp_neighbors = dev.rpc.get_bgp_neighbor_information(
166
neighbor_address='192.168.1.1',
167
detail=True
168
)
169
170
dev.close()
171
```
172
173
### Command Timeout Handling
174
175
```python
176
from jnpr.junos import Device
177
from jnpr.junos.exception import RpcTimeoutError
178
179
dev = Device(host='router1.example.com', user='admin', passwd='secret')
180
dev.open()
181
182
try:
183
# Long-running command with custom timeout
184
result = dev.cli('show route extensive', dev_timeout=120)
185
print("Command completed successfully")
186
except RpcTimeoutError:
187
print("Command timed out after 120 seconds")
188
189
dev.close()
190
```
191
192
### XML RPC Exploration
193
194
```python
195
from jnpr.junos import Device
196
197
dev = Device(host='router1.example.com', user='admin', passwd='secret')
198
dev.open()
199
200
# See the XML RPC equivalent of a CLI command
201
rpc_xml = dev.display_xml_rpc('show interfaces ge-0/0/0')
202
print("RPC XML:")
203
print(rpc_xml)
204
205
# Convert CLI to RPC string
206
rpc_string = dev.cli_to_rpc_string('show route protocol bgp')
207
print("RPC String:")
208
print(rpc_string)
209
210
dev.close()
211
```
212
213
### Template Usage
214
215
```python
216
from jnpr.junos import Device
217
218
dev = Device(host='router1.example.com', user='admin', passwd='secret')
219
dev.open()
220
221
# Load a Jinja2 template
222
template = dev.Template('interface-config.j2')
223
224
# Render template with variables
225
config = template.render(
226
interface='ge-0/0/0',
227
description='WAN Connection',
228
ip_address='192.168.1.1/24'
229
)
230
231
# Use rendered configuration
232
dev.cu.load(config, format='text')
233
234
dev.close()
235
```
236
237
### Advanced RPC Usage
238
239
```python
240
from jnpr.junos import Device
241
from lxml import etree
242
243
dev = Device(host='router1.example.com', user='admin', passwd='secret')
244
dev.open()
245
246
# Create custom RPC XML
247
rpc_xml = etree.Element('get-interface-information')
248
interface_elem = etree.SubElement(rpc_xml, 'interface-name')
249
interface_elem.text = 'ge-0/0/0'
250
251
# Execute custom RPC
252
result = dev.execute(rpc_xml)
253
254
# Process XML response
255
for interface in result.xpath('.//physical-interface'):
256
name = interface.findtext('name')
257
status = interface.findtext('oper-status')
258
print(f"Interface {name}: {status}")
259
260
dev.close()
261
```
262
263
### Error Handling
264
265
```python
266
from jnpr.junos import Device
267
from jnpr.junos.exception import RpcError, RpcTimeoutError
268
269
dev = Device(host='router1.example.com', user='admin', passwd='secret')
270
dev.open()
271
272
try:
273
# Execute command that might fail
274
result = dev.cli('show interfaces ge-99/99/99')
275
except RpcError as e:
276
print(f"RPC Error: {e}")
277
print(f"Error message: {e.message}")
278
except RpcTimeoutError:
279
print("Command execution timed out")
280
281
dev.close()
282
```
283
284
## Types
285
286
```python { .api }
287
# CLI command and output types
288
CliCommand = str # CLI command string
289
CliOutput = str # CLI command output
290
291
# RPC types
292
RpcElement = object # XML Element for RPC commands/responses
293
RpcResponse = object # Parsed RPC response
294
295
# Template types
296
JinjaTemplate = object # Jinja2 template object
297
TemplateVars = dict[str, any] # Template variable dictionary
298
299
# Format options
300
OutputFormat = str # 'text', 'xml', 'json'
301
```