1 | |
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 | |
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 |
|