1 | |
2 |
|
3 | |
4 | * Get device info |
5 | * Get the hostname, software version, platform (chassis part number) and serial number of a Cisco IOS XE device over RESTCONF. |
6 | */ |
7 | export async function main(auth: RT.CiscoIosxe) { |
8 | const headers = { |
9 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
10 | Accept: "application/yang-data+json", |
11 | } |
12 |
|
13 | const [hostnameResponse, hardwareResponse] = await Promise.all([ |
14 | fetch( |
15 | new URL( |
16 | `${auth.base_url.replace(/\/+$/, "")}/restconf/data/Cisco-IOS-XE-native:native/hostname` |
17 | ), |
18 | { headers } |
19 | ), |
20 | fetch( |
21 | new URL( |
22 | `${auth.base_url.replace(/\/+$/, "")}/restconf/data/Cisco-IOS-XE-device-hardware-oper:device-hardware-data/device-hardware` |
23 | ), |
24 | { headers } |
25 | ), |
26 | ]) |
27 |
|
28 | if (!hostnameResponse.ok) { |
29 | throw new Error( |
30 | `${hostnameResponse.status} ${await hostnameResponse.text()}` |
31 | ) |
32 | } |
33 | if (!hardwareResponse.ok) { |
34 | throw new Error( |
35 | `${hardwareResponse.status} ${await hardwareResponse.text()}` |
36 | ) |
37 | } |
38 |
|
39 | const hostname = (await hostnameResponse.json())[ |
40 | "Cisco-IOS-XE-native:hostname" |
41 | ] |
42 | |
43 | |
44 | const hardwareBody = await hardwareResponse.json() |
45 | const hardware = |
46 | hardwareBody["Cisco-IOS-XE-device-hardware-oper:device-hardware"] ?? |
47 | hardwareBody["Cisco-IOS-XE-device-hardware-oper:device-hardware-data"]?.[ |
48 | "device-hardware" |
49 | ] |
50 | const system = hardware?.["device-system-data"] ?? {} |
51 | |
52 | const chassis = (hardware?.["device-inventory"] ?? []) |
53 | .filter((item: any) => item["hw-type"] === "hw-type-chassis") |
54 | .sort( |
55 | (a: any, b: any) => Number(a["hw-dev-index"]) - Number(b["hw-dev-index"]) |
56 | ) |
57 |
|
58 | return { |
59 | hostname, |
60 | software_version: system["software-version"], |
61 | platform: chassis[0]?.["part-number"], |
62 | serial_number: chassis[0]?.["serial-number"], |
63 | chassis, |
64 | system_data: system, |
65 | } |
66 | } |
67 |
|