0

Get running config

by
Published today

Read the running configuration as JSON, whole or for a given RESTCONF data path.

Script cisco_iosxe Verified

The script

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

3
/**
4
 * Get running config
5
 * Read the running configuration as JSON, whole (Cisco-IOS-XE-native:native) or for a given RESTCONF data path such as Cisco-IOS-XE-native:native/interface/GigabitEthernet=1%2F0%2F1.
6
 */
7
export async function main(
8
  auth: RT.CiscoIosxe,
9
  path: string | undefined,
10
  depth: number | undefined,
11
  fields: string | undefined
12
) {
13
  const dataPath = (path || "Cisco-IOS-XE-native:native").replace(/^\/+/, "")
14
  const url = new URL(
15
    `${auth.base_url.replace(/\/+$/, "")}/restconf/data/${dataPath}`
16
  )
17
  if (depth) {
18
    url.searchParams.append("depth", String(depth))
19
  }
20
  if (fields != null && fields !== "") {
21
    url.searchParams.append("fields", fields)
22
  }
23

24
  const response = await fetch(url, {
25
    headers: {
26
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
27
      Accept: "application/yang-data+json",
28
    },
29
  })
30

31
  if (!response.ok) {
32
    throw new Error(`${response.status} ${await response.text()}`)
33
  }
34
  // 204: the path exists but holds no data.
35
  if (response.status === 204) return {}
36

37
  return await response.json()
38
}
39