MAC vendor lookup: Difference between revisions
(→{{header|zkl}}: added test) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
||
Line 78: | Line 78: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2016.12}} |
|||
<lang perl6>use HTTP::UserAgent; |
|||
my $ua = HTTP::UserAgent.new; |
|||
$ua.timeout = 10; # seconds |
|||
my $server = 'http://api.macvendors.com/'; |
|||
sub lookup ($mac) { |
|||
my $response = $ua.get: "$server+$mac"; |
|||
return $response.is-success ?? $response.content !! 'N/A'; |
|||
CATCH { # Normally you would report some information about what |
|||
default { Nil } # went wrong, but the task specifies to ignore errors. |
|||
} |
|||
} |
|||
for < |
|||
BC:5F:F4 |
|||
FC-A1-3E |
|||
10:dd:b1 |
|||
00,0d,4b |
|||
23:45:67 |
|||
> -> $mac { say lookup $mac }</lang> |
|||
{{out}} |
|||
<pre>ASRock Incorporation |
|||
Samsung Electronics Co.,Ltd |
|||
Apple, Inc. |
|||
Roku, Inc. |
|||
N/A |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
Revision as of 19:17, 15 January 2017
You are encouraged to solve this task according to the task description, using any language you may know.
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.
- Basic Task
The task is to 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". A error related to the network connectivity or the API should return a null result.
Lua
<lang lua> -- Requires LuaSocket extension by Lua -- Created by James A. Donnell Jr. -- www.JamesDonnell.com
local baseURL = "http://api.macvendors.com/"
local function lookup(macAddress) http = require "socket.http" result, statuscode, content = http.request(baseURL .. macAddress) return result end
local macAddress = "FC-A1-3E-2A-1C-33" print(lookup(macAddress)) </lang>
Java
<lang java> 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; } } } </lang>
Perl 6
<lang perl6>use HTTP::UserAgent;
my $ua = HTTP::UserAgent.new;
$ua.timeout = 10; # seconds
my $server = 'http://api.macvendors.com/';
sub lookup ($mac) {
my $response = $ua.get: "$server+$mac";
return $response.is-success ?? $response.content !! 'N/A';
CATCH { # Normally you would report some information about what default { Nil } # went wrong, but the task specifies to ignore errors. }
}
for < BC:5F:F4 FC-A1-3E 10:dd:b1 00,0d,4b 23:45:67 > -> $mac { say lookup $mac }</lang>
- Output:
ASRock Incorporation Samsung Electronics Co.,Ltd Apple, Inc. Roku, Inc. N/A
zkl
Uses libcurl (the multiprotocol file transfer library) to do the web query <lang zkl>var [const] CURL=Import("zklCurl"); // libcurl const MAC_VENDORS="http://api.macvendors.com/";
fcn lookUp(macAddress){
httpAddr:=MAC_VENDORS + macAddress; vender:=CURL().get(httpAddr); //-->(Data,bytes of header,bytes of trailer) vender=vender[0].del(0,vender[1]); // remove HTTP header vender.text; // Data --> String (Data is a byte bucket)
}</lang> <lang zkl>lookUp("FC-A1-3E-2A-1C-33").println(); lookUp("4c:72:b9:56:fe:bc").println(); lookUp("foobar").println();</lang>
- Output:
Samsung Electronics Co.,Ltd PEGATRON CORPORATION Vendor not found