0

List firewall addresses

by
Published today

List the IPv4 firewall address objects of an ADOM, one page at a time. filter is a three-part condition such as ["name", "like", "host_%"]. Pass next_offset back as offset to get the next page (null when this was the last page).

Script fortimanager Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * List firewall addresses
5
 * List the IPv4 firewall address objects of an ADOM, one page at a time. filter is a three-part condition such as ["name", "like", "host_%"]. Pass next_offset back as offset to get the next page (null when this was the last page).
6
 */
7
export async function main(
8
  auth: RT.Fortimanager,
9
  adom: string,
10
  filter: string[] | undefined,
11
  fields: string[] | undefined,
12
  offset: number | undefined,
13
  limit: number | undefined
14
) {
15
  // The ADOM is a segment of the JSON-RPC url: a "/" would address another table.
16
  if (adom.includes("/")) throw new Error(`Invalid adom: ${adom}`)
17
  const start = offset ?? 0
18
  const size = limit || 100
19
  const params: { [key: string]: any } = {
20
    url: `/pm/config/adom/${adom}/obj/firewall/address`,
21
    range: [start, size],
22
  }
23
  if (filter?.length) {
24
    params.filter = filter
25
  }
26
  if (fields?.length) {
27
    params.fields = fields
28
  }
29

30
  const response = await fetch(`${auth.base_url.replace(/\/+$/, "")}/jsonrpc`, {
31
    method: "POST",
32
    headers: {
33
      Authorization: `Bearer ${auth.token}`,
34
      "Content-Type": "application/json",
35
      Accept: "application/json",
36
    },
37
    body: JSON.stringify({
38
      id: 1,
39
      method: "get",
40
      params: [params],
41
      verbose: 1,
42
    }),
43
  })
44

45
  if (!response.ok) {
46
    throw new Error(`${response.status} ${await response.text()}`)
47
  }
48

49
  const json = await response.json()
50
  const result = json.result?.[0]
51
  if (result?.status?.code !== 0) {
52
    throw new Error(
53
      `FortiManager error: ${JSON.stringify(json.result ?? json)}`
54
    )
55
  }
56

57
  const addresses: any[] = result.data ?? []
58
  return {
59
    addresses,
60
    offset: start,
61
    limit: size,
62
    next_offset: addresses.length === size ? start + size : null,
63
  }
64
}
65