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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# PyVyOS Documentation
PyVyOS is a Python library for interacting with VyOS devices via their API. This documentation provides a guide on how to use PyVyOS to manage your VyOS devices programmatically.
You can find the complete PyVyOS documentation on [Read the Docs](https://pyvyos.readthedocs.io/en/latest/).
## Installation
You can install PyVyOS using pip:
```bash
pip install pyvyos
```
## Getting Started
### Importing and Disabling Warnings for verify=False
Before using PyVyOS, it's a good practice to disable urllib3 warnings and import the required modules, IF you use verify=False:
```
import urllib3
urllib3.disable_warnings()
```
### Using API Response Class
PyVyOS uses a custom ApiResponse data class to handle API responses:
```
@dataclass
class ApiResponse:
status: int
request: dict
result: dict
error: str
```
### Initializing a VyDevice Object
To interact with your VyOS device, you'll need to create an instance of the VyDevice class. You can set up your device using the following code,
assuming you've stored your credentials as environment variables:
```
from dotenv import load_dotenv
# Load environment variables from a .env file
load_dotenv()
# Retrieve VyOS device connection details from environment variables
hostname = os.getenv('VYDEVICE_HOSTNAME')
apikey = os.getenv('VYDEVICE_APIKEY')
port = os.getenv('VYDEVICE_PORT')
protocol = os.getenv('VYDEVICE_PROTOCOL')
verify_ssl = os.getenv('VYDEVICE_VERIFY_SSL')
# Convert the verify_ssl value to a boolean
verify = verify_ssl.lower() == "true" if verify_ssl else True
# Create an instance of the VyOS device
device = VyDevice(hostname=hostname, apikey=apikey, port=port, protocol=protocol, verify=verify)
```
## Using PyVyOS
Once you have created a VyDevice object, you can use it to interact with your VyOS device using various methods provided by the library.
### Reset
The reset method allows you to run a reset command:
```
# Execute the reset command
response = device.reset(path=["conntrack-sync", "internal-cache"])
# Check for errors and print the result
if not response.error:
print(response.result)
```
### Retrieve Show Configuration
The retrieve_show_config method retrieves the VyOS configuration:
```
# Retrieve the VyOS configuration
response = device.retrieve_show_config(path=[])
# Check for errors and print the result
if not response.error:
print(response.result)
```
### Retrieve Return Values
```
# Retrieve VyOS return values for a specific interface
response = device.retrieve_return_values(path=["interfaces", "dummy", "dum1", "address"])
print(response.result)
```
### Configure Delete
```
# Delete a VyOS interface configuration
response = device.configure_delete(path=["interfaces", "dummy", "dum1"])
```
### Generate
```
# Generate an SSH key with a random string in the name
randstring = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(20))
keyrand = f'/tmp/key_{randstring}'
response = device.generate(path=["ssh", "client-key", keyrand])
```
### Show
```
# Show VyOS system image information
response = device.show(path=["system", "image"])
print(response.result)
```
### Reset
```
# Reset VyOS with specific parameters
response = device.reset(path=["conntrack-sync", "internal-cache"])
```
### Configure Set
The configure_set method sets a VyOS configuration:
```
# Set a VyOS configuration
response = device.configure_set(path=["interfaces", "ethernet", "eth0", "address", "192.168.1.1/24"])
# Check for errors and print the result
if not response.error:
print(response.result)
```
### Config File Save
```
# Save VyOS configuration without specifying a file (default location)
response = device.config_file_save()
```
### Config File Save with custom filename
```
# Save VyOS configuration to a specific file
response = device.config_file_save(file="/config/test300.config")
```
### Config File Load
```
# Load VyOS configuration from a specific file
response = device.config_file_load(file="/config/test300.config")
```
|