$curl -X POST -d '{"address":"192.168.42.42"}' -H "Content-Type: application/json" -k -u $key:$secret https://opnsense/api/firewall/alias_util/add/test{"status":"failed","status_msg":"nonexistent alias test"}
import jsonimport uuidimport csvwith open('opnsense_aliases.json') as user_file: parsed_json = json.load(user_file)cur_items=parsed_json['aliases']['alias']with open('/tmp/pfsense_alias.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: #print(row) item_name = row['name'] item_data = row['data'] item_type = row['type'] item_description = row['description'] if len(row['data'].split(" "))>1: item_data = "\n".join(row['data'].split(" ")) item_uuid = str(uuid.uuid4()) y = {'enabled': '1', 'name': item_name, 'type': item_type, 'proto': '', 'interface': '', 'counters': '0', 'updatefreq': '', 'content': item_data, 'categories': '', 'description': item_description } cur_items[item_uuid] = yprint(json.dumps(parsed_json, indent=2))
Here is a python script which creates a json file for upload.
Quote from: trumee on October 28, 2023, 04:09:31 pmHere is a python script which creates a json file for upload. @trumee would you be willing to explain how to use your script?
python -m pip install MODULE_NAME
My previous post is how I believe the whole process should work, but it appears there may currently be a bug with the alias.json import function.See this thread for details:https://forum.opnsense.org/index.php?topic=39297.0
import jsonimport uuidimport csvjsonfile = 'C:/path/to/aliases.json'csvfile = 'C:/path/to/opnsense_alias.csv'# jsonfile = '/path/to/aliases.json'# csvfile = '/path/to/opnsense_alias.csv'with open(jsonfile) as user_file: parsed_json = json.load(user_file)cur_items=parsed_json['aliases']['alias']with open(csvfile, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: #print(row) item_name = row['name'] item_data = row['data'] item_type = row['type'] item_description = row['description'] if len(row['data'].split(" "))>1: item_data = "\n".join(row['data'].split(" ")) item_uuid = str(uuid.uuid4()) new_alias = {'enabled': '1', 'name': item_name, 'type': item_type, 'proto': '', 'interface': '', 'counters': '0', 'updatefreq': '', 'content': item_data, 'categories': '', 'description': item_description } cur_items[item_uuid] = new_aliaswith open(jsonfile, 'w', encoding="utf-8") as f: json.dump(parsed_json, f, indent=2)