Hi,
here is my little script to send the set commands in a bunch to my vyos to reach the api.
import requests
import json
# Your VyOS API URL and key
url = "https://vyos/configure"
key = '123456789'
# Read commands from the file
with open("setcommand.txt", "r") as file:
commands = file.readlines()
# Parse commands and prepare data for the API
data_list = []
for command in commands:
if command.startswith("set"):
path = command.split()[1:]
# Remove quotes from the command values
path = [p.strip("'\"") for p in path]
data_list.append({"op": "set", "path": path})
# Convert the list of commands to JSON
payload_data = json.dumps(data_list)
# Prepare the payload
payload = {
'data': payload_data,
'key': key
}
# Send commands to the API
response = requests.post(url, data=payload, verify=False)
# Print the response
print(response.text)
{"success": false, "error": "Configuration path: [firewall group port-group ad-ports-tcp description TCP Ports] is not valid\n\nSet failed\n", "data": null}
My question how must be formated to send this set command to the vyos api
I have not found a example in the docu
set firewall group port-group ad-ports-tcp description ‘TCP Ports’
There is an example of python3 script
#!/usr/bin/env python3
import json
import re
import requests
import urllib3
def convert_to_json_commands(input_str):
commands = input_str.strip().split('\n')
result = []
for command in commands:
# Use regular expressions to extract parts based on desired pattern
parts = re.findall(r"[^\s']+|'[^']+'", command)
path = [part.replace("'", "") for part in parts[1:]]
operation = {"op": "set", "path": path}
result.append(operation)
return json.dumps(result, indent=4)
def configure_vyos(address, key, data):
headers = {}
url = f'https://{address}/configure'
payload = {'data': data, 'key': key}
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
response = requests.post(url, headers=headers, data=payload, verify=False)
if response.status_code == 200:
print("Configuration successful.")
print(f"Address '{address}' configured.")
else:
print("Configuration failed. Status code:", response.status_code)
print("Response:", response.text)
if __name__ == '__main__':
commands = '''
set interfaces dummy dum1 address '10.11.0.1/32'
set protocols static route 192.0.2.0/24 next-hop 203.0.113.1
set firewall group port-group ad-ports-tcp description 'TCP Ports'
'''
config_json = convert_to_json_commands(commands)
configure_vyos('192.168.122.14', 'foo', config_json)
check:
$ ./configure_via_api.py
Configuration successful.
Address '192.168.122.14' configured.
vyos@r14# show firewall
group {
port-group ad-ports-tcp {
description "TCP Ports"
}
}
[edit]
vyos@r14#
So the format
curl -k --location --request POST 'https://vyos/configure' \
--form data='[{"op": "set","path":["interfaces","vxlan","vxlan1","remote","203.0.113.99"]}, {"op": "set","path":["interfaces","vxlan","vxlan1","vni","1"]}]' \
--form key='MY-HTTPS-API-PLAINTEXT-KEY'
1 Like