Download Address objects using SonicOS API
04/26/2023 2 People found this article helpful 252,405 Views
Description
SonicOS API provides an alternative to the SonicOS Command Line Interface (CLI) for configuring selected functions.
This article only explains how to download all the address objects configured on SonicWall, including the default ones. Please follow the below articles for additional assistance:
- Introduction to SonicOS API
- Creating Address Object and Address Group Object using SonicOS API cURL
- Adding Multiple Address Objects Using SonicOS API
- SonicOS API for adding Address group in Postman and cURL
- Add new address object to an existing group - SonicOS API
You are free to choose Swagger, Postman, Git bash, or any application that allows API calls. If you are using a Linux-based operating system, you can execute cURL from the terminal. Please refer to https://sonicos-api.sonicwall.com for the entire list.
Only the first part of this article would change, depending on the SonicWall model you use. Commands are the same for both Gen6 and Gen7 SonicWall devices.
For this article, I'm using Postman App and will be showing the commands to run on cURL for each step.
Resolution
SonicOS API is disabled by default in SonicOS. Any attempt to access SonicOS API while it is disabled results in an HTTP 403 Forbidden error
. To use the SonicOS API, you must enable it, either through the SonicOS Management Interface or from the CLI. Please enable the SonicOS API module in the SonicWall UI.
Gen 7: Enable SonicOS API Gen7
Gen 6: Enable SonicOS API Gen6
The above KB also has the steps on how to log in using API Applications
CAUTION: My SonicWall IP address is 192.168.168.168 with user credentials as admin/password. This has to be kept in mind while running the commands from the below screenshots.
Step 1: Login using SonicOS API
The following 3 steps need to be performed for every API request.
NOTE: https://IP-address:port/-- Replace this with your SonicWall's Public or private IP address with the right management port number (If the management port is 443, you can directly use https:// followed by the IP address without the port number too).
a) The HTTP method should be POST and we need to use the URL: https://192.168.168.168/api/sonicos/auth
Under the authorization tab, select Basic Auth and mention the correct admin credentials.
b) Under the settings tab, turn OFF the Enable SSL certificate verification if the firewall is using a self-signed certificate for management.
c) Under the headers tab, include application/JSON as the value for keys Accept and Content-type.
d) The Gen 7 devices are token-driven. Use the {"override" : true} under the body to override any older tokens. This is used only during login.
After this, click on the Send button and then you can see the response on the section below. The response should contain a message: "success".
e) After this, click on the Send button and then you can see the response on the section below. The response should contain a message: "success".
cURL code:
curl --location --request POST 'https://192.168.168.168/api/sonicos/auth' \
--header 'Accept: application/Json' \
--header 'Content-Type: application/Json' \
--header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=
--data-raw '{"override" : true}'
Step 2: GET command to download the address objects
We use the GET command to retrieve information from the right folder. In the screenshot below, I have a 200 OK which means the command is run successfully and the Response Body will have the output.
This output can be saved to get a JSON file or a text document with extension .txt
cURL code:
curl -L -X GET 'https://192.168.168.168/api/sonicos/address-objects/ipv4' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
-d '{"override": true}'
Similarly, address groups can also be retrieved and saved. Just the URI folder name changes for the GET command.
cURL code:
curl -L -X GET 'https://192.168.168.168/api/sonicos/address-objects/ipv4' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
-d '{"override": true}'
Step 3: Logout using SonicOS API
It is recommended to log out from SonicWall via API. Since we have not made any configurational changes, there is no need to run a commit.curl -k -i -u "admin:password" -X DELETE https://192.168.188.200/api/sonicos/auth
Related Articles
Categories