Edits history of script submission #24141 for ' Get endpoint by MAC (cisco_ise)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get endpoint by MAC
     * Get one endpoint by its MAC address (ISE uses the MAC as the endpoint name). Any separator or case is accepted, e.g. 11:22:33:44:55:66, 11-22-33-44-55-66 or 1122.3344.5566.
     */
    export async function main(auth: RT.CiscoIse, mac: string) {
      // ISE stores MACs as AA:BB:CC:DD:EE:FF
      const hex = mac.replace(/[-:.\s]/g, "").toUpperCase()
      if (!/^[0-9A-F]{12}$/.test(hex))
        throw new Error(`Invalid MAC address: ${mac}`)
      const macAddress = hex.match(/../g)!.join(":")
    
      const base = new URL(auth.base_url)
      if (auth.ers_port) base.port = String(auth.ers_port)
      const url = new URL(
        `/ers/config/endpoint/name/${encodeURIComponent(macAddress)}`,
        base
      )
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago