1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# Examples
Common usage patterns and examples for PyVyOS.
## Basic Connection
```python
from pyvyos import VyDevice
device = VyDevice(hostname="192.168.1.1", apikey="your-api-key", verify=False)
```
## Viewing System Information
```python
response = device.show(path=["system", "image"])
if not response.error:
print(response.result)
```
## Interface Configuration
Add, view, get, or remove interface configuration:
```python
# Add address
device.configure_set(path=["interfaces", "ethernet", "eth0", "address", "192.168.1.1/24"])
# View configuration
device.retrieve_show_config(path=["interfaces", "ethernet", "eth0"])
# Get address value
device.retrieve_return_values(path=["interfaces", "ethernet", "eth0", "address"])
# Remove interface
device.configure_delete(path=["interfaces", "dummy", "dum1"])
```
## Multiple Operations
Execute multiple configuration changes atomically:
```python
device.configure_multiple_op(op_path=[
{"op": "set", "path": ["interfaces", "dummy", "dum1", "address", "192.168.1.1/24"]},
{"op": "set", "path": ["interfaces", "dummy", "dum1", "description", "Test"]},
{"op": "delete", "path": ["interfaces", "dummy", "old1"]}
])
```
## Configuration Backup
```python
device.config_file_save(file="/config/backup-2024.config")
device.config_file_load(file="/config/backup-2024.config")
```
## SSH Key Generation
```python
import random, string
randstring = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(20))
device.generate(path=["ssh", "client-key", f'/tmp/key_{randstring}'])
```
## System Operations
```python
device.reboot(path=["now"])
device.poweroff(path=["now"])
```
## Error Handling
```python
response = device.configure_set(path=["invalid", "path"])
if response.error:
print(f"Error {response.status}: {response.error}")
else:
print("Success:", response.result)
```
## Working with Responses
```python
response = device.show(path=["system"])
print(f"Status: {response.status}")
if not response.error:
data = response.result # Process data...
print(f"Request: {response.request}") # API key sanitized
```
|