0

List networks

by
Published today

List IPv4 networks one page at a time, optionally filtered by network view, exact CIDR, a regular expression on the CIDR (e.g. ^10\.20\.) or comment. Pass the returned next_page_id as page_id to get the next page; it is null on the last page.

Script infoblox_nios Verified

The script

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

3
function wapiUrl(auth: RT.InfobloxNios, path: string) {
4
  const host = auth.host
5
    .trim()
6
    .replace(/^https?:\/\//, "")
7
    .replace(/\/+$/, "")
8
  const version = auth.wapi_version.trim().replace(/^v/, "")
9
  return new URL(`https://${host}/wapi/v${version}/${path}`)
10
}
11

12
function basicAuth(auth: RT.InfobloxNios) {
13
  const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`)
14
  return `Basic ${btoa(String.fromCharCode(...bytes))}`
15
}
16

17
/**
18
 * List networks
19
 * List IPv4 networks one page at a time, optionally filtered by network view, exact CIDR, a regular expression on the CIDR (e.g. ^10\.20\.) or comment. Pass the returned next_page_id as page_id to get the next page; it is null on the last page.
20
 */
21
export async function main(
22
  auth: RT.InfobloxNios,
23
  network_view: string | undefined,
24
  network: string | undefined,
25
  network_regex: string | undefined,
26
  comment: string | undefined,
27
  max_results: number | undefined,
28
  page_id: string | undefined,
29
  return_fields: string[] | undefined
30
) {
31
  const url = wapiUrl(auth, "network")
32
  if (network_view) url.searchParams.append("network_view", network_view)
33
  if (network) url.searchParams.append("network", network)
34
  if (network_regex) url.searchParams.append("network~", network_regex)
35
  if (comment) url.searchParams.append("comment", comment)
36
  const fields = ["extattrs", ...(return_fields ?? [])]
37
  url.searchParams.append("_return_fields+", fields.join(","))
38
  url.searchParams.append("_paging", "1")
39
  url.searchParams.append("_return_as_object", "1")
40
  url.searchParams.append("_max_results", String(max_results || 100))
41
  if (page_id) url.searchParams.append("_page_id", page_id)
42

43
  const response = await fetch(url, {
44
    headers: {
45
      Authorization: basicAuth(auth),
46
      Accept: "application/json",
47
    },
48
  })
49

50
  if (!response.ok) {
51
    throw new Error(`${response.status} ${await response.text()}`)
52
  }
53

54
  const data = await response.json()
55
  return { result: data.result, next_page_id: data.next_page_id ?? null }
56
}
57