PyVyOS

PyVyOS is a Python library for configuring and managing VyOS devices through their API.

Key resources:

  • Documentation: Provides installation, configuration, and usage instructions.

  • GitHub repository: Hosts the source code.

  • PyPI: Hosts distribution packages for installation via the Python package installer (pip).

Installation

To install PyVyOS via pip, run:

pip install pyvyos

Getting started

Import and disable warnings for verify=false

import urllib3
urllib3.disable_warnings()

Use API response class

@dataclass
class ApiResponse:
    status: int
    request: dict
    result: dict
    error: str

Initialize a VyDevice object

from dotenv import load_dotenv
load_dotenv()

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')

verify = verify_ssl.lower() == "true" if verify_ssl else True 

device = VyDevice(hostname=hostname, apikey=apikey, port=port, protocol=protocol, verify=verify)

Use PyVyOS

Configure, then set

response = device.configure_set(path=["interfaces", "ethernet", "eth0", "address", "192.168.1.1/24"])
if not response.error:
    print(response.result)

Configure, then show a single object value

response = device.retrieve_return_values(path=["interfaces", "dummy", "dum1", "address"])
print(response.result)

Configure, then show object

response = device.retrieve_show_config(path=[])
if not response.error:
    print(response.result)

Configure, then delete object

response = device.configure_delete(path=["interfaces", "dummy", "dum1"])

Configure, then save

response = device.config_file_save()

Configure, then save file

response = device.config_file_save(file="/config/test300.config")

Show object

response = device.show(path=["system", "image"])
print(response.result)

Generate object

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])

Reset object

response = device.reset(path=["conntrack-sync", "internal-cache"])
if not response.error:
    print(response.result)

Configure, then load file

response = device.config_file_load(file="/config/test300.config")