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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm MAC vendor lookup step by step in the Java 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 Java programming language

Java Program to Lookup MAC Vendor Information

Program 1:

This Java program uses the MAC Vendors API (http://api.macvendors.com/) to look up the manufacturer of a given MAC address.

  1. main Method:

    • The main method takes command-line arguments representing MAC addresses and invokes the get method for each argument to perform the lookup.
  2. get Method:

    • This method takes a MAC address as input.
    • It constructs a URL using the baseURL and the MAC address.
    • It establishes an HTTP connection and sends a GET request to the URL.
    • The response from the API is parsed and returned as a string.

This program provides a simple interface for performing MAC vendor lookups.

Program 2:

This Java program also uses the MAC Vendors API, but it employs the Java 11 HttpClient API for making HTTP requests.

  1. main Method:

    • The main method defines a list of MAC addresses and iterates through them, performing vendor lookups.
  2. getMacVendor Method:

    • This method takes a MAC address as input.
    • It constructs a URI using the BASE_URL and the MAC address.
    • It creates an HTTP client and a GET request using the constructed URI.
    • The request is sent to the API, and the response is parsed and returned as a string.

This program demonstrates the use of the newer HttpClient API for performing HTTP requests in Java.

Sample Usage:

To use these programs, follow these steps:

  1. Save the Java code in a file, e.g., Lookup.java.
  2. Compile the program using javac Lookup.java.
  3. Run the program using java Lookup [MAC address(es)].

For example, to look up the vendor for the MAC address "88:53:2E:67:07:BE", you would run:

java Lookup 88:53:2E:67:07:BE

Output:

The output of the programs will be the vendor information for the specified MAC addresses. For example, for the MAC address "88:53:2E:67:07:BE", the output might be:

88:53:2E:67:07:BE: Dell Inc.

Source code in the java programming language

package com.jamesdonnell.MACVendor;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/** MAC Vendor Lookup class.
 * www.JamesDonnell.com
 * @author James A. Donnell Jr. */
public class Lookup {
	/** Base URL for API. The API from www.macvendors.com was chosen. */
	private static final String baseURL = "http://api.macvendors.com/";

	/** Performs lookup on MAC address(es) supplied in arguments.
	 * @param args MAC address(es) to lookup. */
	public static void main(String[] args) {
		for (String arguments : args)
			System.out.println(arguments + ": " + get(arguments));
	}

	/** Performs lookup on supplied MAC address.
	 * @param macAddress MAC address to lookup.
	 * @return Manufacturer of MAC address. */
	private static String get(String macAddress) {
		try {
			StringBuilder result = new StringBuilder();
			URL url = new URL(baseURL + macAddress);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
			rd.close();
			return result.toString();
		} catch (FileNotFoundException e) {
			// MAC not found
			return "N/A";
		} catch (IOException e) {
			// Error during lookup, either network or API.
			return null;
		}
	}
}


 
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

public final class MacVendorLookup {

	public static void main(String[] aArgs) throws InterruptedException, IOException {		
		for ( String macAddress : macAddresses ) {
			HttpResponse<String> response = getMacVendor(macAddress);
			System.out.println(macAddress + "  " + response.statusCode() + "  " + response.body());
			
			TimeUnit.SECONDS.sleep(2);
		}		
	}

	private static HttpResponse<String> getMacVendor(String aMacAddress) throws IOException, InterruptedException {				
		URI uri = URI.create(BASE_URL + aMacAddress);
		HttpClient client = HttpClient.newHttpClient();
		HttpRequest request = HttpRequest
			.newBuilder()
		    .uri(uri)
		    .header("accept", "application/json")
		    .GET()
		    .build();
		
		HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
		
		return response;
	}
	
	private static final String BASE_URL = "http://api.macvendors.com/";
	
	private static final List<String> macAddresses = List.of("88:53:2E:67:07:BE",
															 "D4:F4:6F:C9:EF:8D",
															 "FC:FB:FB:01:FA:21",
															 "4c:72:b9:56:fe:bc",
															 "00-14-22-01-23-45"
															 );	
	
}


  

You may also check:How to resolve the algorithm Sokoban step by step in the Elixir programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the OCaml programming language
You may also check:How to resolve the algorithm Subleq step by step in the Racket programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Erlang programming language
You may also check:How to resolve the algorithm Faces from a mesh step by step in the Go programming language