MAC vendor lookup: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 6 users not shown)
Line 186:
{{out}}
<pre>Hon Hai Precision Ind. Co.,Ltd.</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM Block% 599 : REM Memory buffer to receive GET info
INSTALL @lib$ + "SOCKLIB" : PROC_initsockets
READ Mac$
WHILE Mac$ > ""
WAIT 100 : REM 1 sec pause to avoid 'Too Many Requests' errors
Socket=FN_tcpconnect("api.macvendors.com", "80")
I%=FN_writelinesocket(Socket, "GET /" + Mac$ + " HTTP/1.0")
I%=FN_writelinesocket(Socket, "")
REPEAT N%=FN_readsocket(Socket, Block%, 600) UNTIL N% > 0
PROC_closesocket(Socket)
PRINT Mac$ TAB(24);
CASE LEFT$($(Block% + 9), 3) OF
WHEN "200"
Block%?N%=0 : REM Add terminating 0x00
REPEAT N%-=1 UNTIL Block%?N% < 32 : REM Leave only last line
PRINT $$(Block% + N% + 1) : REM Print string from memory
WHEN "404"
PRINT "N/A"
OTHERWISE
PRINT "ERROR"
ENDCASE
READ Mac$
ENDWHILE
PROC_exitsockets
END
DATA 88:53:2E:67:07:BE, FC:FB:FB:01:FA:21, D4:F4:6F:C9:EF:8D, 10-11-22-33-44-55,</syntaxhighlight>
{{out}}
 
<pre>
88:53:2E:67:07:BE Intel Corporate
FC:FB:FB:01:FA:21 Cisco Systems, Inc
D4:F4:6F:C9:EF:8D Apple, Inc.
10-11-22-33-44-55 N/A
</pre>
 
=={{header|C}}==
Line 671 ⟶ 711:
}</syntaxhighlight>
 
===Java 11===
<syntaxhighlight>
<syntaxhighlight lang="java">
Using Java version 11
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"
);
}
</syntaxhighlight>
{{ out }}
<pre>
88:53:2E:67:07:BE 200 Intel Corporate
D4:F4:6F:C9:EF:8D 200 Apple, Inc.
FC:FB:FB:01:FA:21 200 Cisco Systems, Inc
4c:72:b9:56:fe:bc 200 PEGATRON CORPORATION
00-14-22-01-23-45 200 Dell Inc.
</pre>
 
=={{header|Javascript}}==
Line 788 ⟶ 880:
declare htmldoc nothing
}
Urls=("88:53:2E:67:07:BE", "FC:FB:FB:01:FA:21", "D4:F4:6F:C9:EF:8D", "23BC:455F:67F4")
url=each(URLs)
While Url {
Print Array$(URL),@(20), httpGet$("httphttps://api.macvendors.com/"+Array$(URL))
Wait 201500
}
}
Line 802 ⟶ 894:
Examples:<syntaxhighlight lang="mathematica">macLookup["00-14-22-01-23-45"]</syntaxhighlight>
Outputs:<pre>Dell Inc.</pre>
 
=={{header|MiniScript}}==
This implementation is for use with the [http://miniscript.org/MiniMicro Mini Micro] version of MiniScript.
<syntaxhighlight lang="miniscript">
macList = ["88:53:2E:67:07:BE", "FC:FB:FB:01:FA:21", "00:02:55:00:00:00",
"D4:F4:6F:C9:EF:8D", "23:45:67", "18:93:D7", "1C:A6:81"]
 
for mac in macList
ret = http.get("http://api.macvendors.com/"+mac)
if ret == "HTTP/1.1 404 Not Found" then ret = "N/A"
print ret
wait 1
end for
</syntaxhighlight>
{{out}}
<pre>
Intel Corporate
Cisco Systems, Inc
IBM Corp
Apple, Inc.
N/A
Texas Instruments
HUAWEI TECHNOLOGIES CO.,LTD
</pre>
 
=={{header|Nim}}==
Line 815 ⟶ 931:
ASRock Incorporation
</pre>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
let mactable = http get "http://standards-oui.ieee.org/oui/oui.csv" | from csv --no-infer
 
def get-mac-org [] {
let assignment = $in | str upcase | str replace --all --regex "[^A-Z0-9]" "" | str substring 0..6
$mactable | where Assignment == $assignment | try {first | get "Organization Name"} catch {"N/A"}
}
 
# Test cases from the Ada entry
let macs = [
# Should succeed
"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"
# Should fail
"23-45-67"
"foobar"
]
 
$macs | each {{MAC: $in, Vendor: ($in | get-mac-org)}}
</syntaxhighlight>
{{out}}
<pre>
╭───┬───────────────────┬──────────────────────╮
│ # │ MAC │ Vendor │
├───┼───────────────────┼──────────────────────┤
│ 0 │ 88:53:2E:67:07:BE │ Intel Corporate │
│ 1 │ D4:F4:6F:C9:EF:8D │ Apple, Inc. │
│ 2 │ FC:FB:FB:01:FA:21 │ Cisco Systems, Inc │
│ 3 │ 4c:72:b9:56:fe:bc │ PEGATRON CORPORATION │
│ 4 │ 00-14-22-01-23-45 │ Dell Inc. │
│ 5 │ 23-45-67 │ N/A │
│ 6 │ foobar │ N/A │
╰───┴───────────────────┴──────────────────────╯
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">
Line 1,405 ⟶ 1,561:
Cisco Systems, Inc
Apple, Inc.
</pre>
 
=={{header|Visual Basic .NET}}==
Based on the C# sample but works with .Net versions prior to Framework 4.5.<br>
Expects the address to be on the command line, if not specified, it defaults to 88:53:2E:67:07:BE.
<syntaxhighlight lang="vbnet">
' MAC Vendor lookup - based on the C# sample.
 
Imports System
Imports System.Net
 
Module LookupMac
 
Public Sub Main(args() As String)
 
Dim macAddress As String = If(args.Length < 1, "88:53:2E:67:07:BE", args(0))
Dim uri As New Uri("http://api.macvendors.com/" & WebUtility.UrlEncode(macAddress))
Dim wc As New WebClient()
Console.Out.WriteLine(macAddress & " " & wc.DownloadString(uri))
 
End Sub
 
End Module
</syntaxhighlight>
{{out}}
With no address on the command line:
<pre>
88:53:2E:67:07:BE Intel Corporate
</pre>
 
With FC:FB:FB:01:FA:21 on the command line:
<pre>
FC:FB:FB:01:FA:21 Cisco Systems, Inc
</pre>
 
Line 1,411 ⟶ 1,600:
 
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.
<syntaxhighlight lang="ecmascriptwren">/* mac_vendor_lookupMAC_vendor_lookup.wren */
class MAC {
foreign static lookup(address)
Line 1,422 ⟶ 1,611:
which we embed in the following Go program and run it.
{{libheader|WrenGo}}
<syntaxhighlight lang="go">/* mac_vendor_lookupMAC_vendor_lookup.go */
package main
 
Line 1,452 ⟶ 1,641:
func main() {
vm := wren.NewVM()
fileName := "mac_vendor_lookupMAC_vendor_lookup.wren"
methodMap := wren.MethodMap{"static lookup(_)": macLookup}
classMap := wren.ClassMap{"MAC": wren.NewClass(nil, nil, methodMap)}
9,476

edits