0

Get interfaces

by
Published today

List the interfaces of a Cisco IOS XE device with their admin and operational status, or get a single interface by name.

Script cisco_iosxe Verified

The script

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

3
/**
4
 * Get interfaces
5
 * List the interfaces of a Cisco IOS XE device with their admin and operational status, or get a single interface by name (e.g. GigabitEthernet1/0/1). Reads the Cisco-IOS-XE-interfaces-oper model.
6
 */
7
export async function main(
8
  auth: RT.CiscoIosxe,
9
  name: string | undefined,
10
  full_details: boolean | undefined
11
) {
12
  const base = `${auth.base_url.replace(/\/+$/, "")}/restconf/data/Cisco-IOS-XE-interfaces-oper:interfaces`
13
  // List keys go in the path, percent-encoded: GigabitEthernet1/0/1 -> GigabitEthernet1%2F0%2F1.
14
  const url = new URL(
15
    name ? `${base}/interface=${encodeURIComponent(name)}` : base
16
  )
17

18
  const response = await fetch(url, {
19
    headers: {
20
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
21
      Accept: "application/yang-data+json",
22
    },
23
  })
24

25
  if (!response.ok) {
26
    throw new Error(`${response.status} ${await response.text()}`)
27
  }
28

29
  const data = response.status === 204 ? {} : await response.json()
30
  const interfaces: any[] = name
31
    ? (data["Cisco-IOS-XE-interfaces-oper:interface"] ?? [])
32
    : (data["Cisco-IOS-XE-interfaces-oper:interfaces"]?.interface ?? [])
33

34
  if (full_details) {
35
    return interfaces
36
  }
37
  return interfaces.map((i) => ({
38
    name: i.name,
39
    interface_type: i["interface-type"],
40
    description: i.description,
41
    admin_status: i["admin-status"],
42
    oper_status: i["oper-status"],
43
    ipv4: i.ipv4,
44
    phys_address: i["phys-address"],
45
    speed: i.speed,
46
    mtu: i.mtu,
47
    last_change: i["last-change"],
48
  }))
49
}
50