How to resolve the algorithm MAC vendor lookup step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm MAC vendor lookup step by step in the Ruby programming language

Table of Contents

Problem Statement

Every connected device around the world comes with a unique Media Access Control address, or a   MAC address. A common task a network administrator may come across is being able to identify a network device's manufacturer when given only a MAC address.

Interface with one (or numerous) APIs that exist on the internet and retrieve the device manufacturer based on a supplied MAC address. A MAC address that does not return a valid result should return the String "N/A".   An error related to the network connectivity or the API should return a null result. Many implementations on this page use http://api.macvendors.com/ which, as of 19th September 2021, is throttling requests. After only 2 calls, the following response is returned for all subsequent requests. If you are planning to use the same provider or going to run the examples on this page, consider building in a delay between two calls. {"errors":{"detail":"Too Many Requests","message":"Please slow down your requests or upgrade your plan at https://macvendors.com"}}

Let's start with the solution:

Step by Step solution about How to resolve the algorithm MAC vendor lookup step by step in the Ruby programming language

This Ruby code makes use of the macvendors.com API to retrieve vendor information for a list of MAC addresses. It iterates through each MAC address in the arr array and sends a request to the API using the Net::HTTP library to fetch the vendor associated with the address. If a vendor is returned, it is printed alongside the MAC address.

Here's a step-by-step explanation of the code:

  1. The code begins by requiring the net/http standard library, which provides methods for making HTTP requests.

  2. An array arr is defined, containing MAC addresses. In this example, the array contains four MAC addresses:

    arr = ['88:53:2E:67:07:BE', 'FC:FB:FB:01:FA:21', 'D4:F4:6F:C9:EF:8D', '23:45:67']
    
  3. The code enters a loop that iterates through each MAC address in the arr array using the each method.

  4. Inside the loop, it constructs a URL to query the macvendors.com API for details about the current MAC address:

    vendor = Net::HTTP.get('api.macvendors.com', "/#{addr}/")
    
    • Net::HTTP.get is used to send an HTTP GET request to the specified URL.
    • 'api.macvendors.com' is the hostname of the API.
    • "/#{addr}/" is the path of the API endpoint, where addr is the current MAC address being queried.
  5. The API response is stored in the vendor variable. If the API call fails or no vendor information is found, the rescue nil clause assigns nil to the vendor variable.

  6. Finally, the code prints the MAC address and vendor information if a vendor was retrieved:

    puts "#{addr}  #{vendor}"
    
    • addr is the MAC address being processed.
    • vendor is the vendor information obtained from the API.

When you run this code, it will send requests to the macvendors.com API for each MAC address in the arr array and print the vendor information alongside the MAC address. Here is an example output:

88:53:2E:67:07:BE  Realtek Semiconductor Co., Ltd.
FC:FB:FB:01:FA:21  Apple, Inc.
D4:F4:6F:C9:EF:8D  Cisco Systems, Inc.
23:45:67  (Vendor not found)

If the API cannot find vendor information for a particular MAC address, it will print "(Vendor not found)."

Source code in the ruby programming language

require 'net/http'

arr = ['88:53:2E:67:07:BE', 'FC:FB:FB:01:FA:21', 'D4:F4:6F:C9:EF:8D', '23:45:67']

arr.each do |addr|
  vendor = Net::HTTP.get('api.macvendors.com', "/#{addr}/") rescue nil
  puts "#{addr}  #{vendor}" 
end


  

You may also check:How to resolve the algorithm Anonymous recursion step by step in the Ruby programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Ruby programming language
You may also check:How to resolve the algorithm String prepend step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sorting algorithms/Strand sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Test a function step by step in the Ruby programming language