DNS query: Difference between revisions

Content added Content deleted
(Corrected headers)
m (syntax highlighting fixup automation)
Line 7: Line 7:
=={{header|Ada}}==
=={{header|Ada}}==
{{works with|GNAT GPL|Any - package Gnat.Sockets supports only IPv4 as of Jun 2011}}
{{works with|GNAT GPL|Any - package Gnat.Sockets supports only IPv4 as of Jun 2011}}
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use GNAT.Sockets;
with GNAT.Sockets; use GNAT.Sockets;


Line 19: Line 19:
Inet_Addr_V4 := Addresses (Host);
Inet_Addr_V4 := Addresses (Host);
Put ("IPv4: " & Image (Value => Inet_Addr_V4));
Put ("IPv4: " & Image (Value => Inet_Addr_V4));
end DNSQuerying;</lang>
end DNSQuerying;</syntaxhighlight>


{{works with|GNAT GPL|2019 - support for IPv6 was added to Gnat.Sockets sometime before this}}
{{works with|GNAT GPL|2019 - support for IPv6 was added to Gnat.Sockets sometime before this}}


<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use Gnat.Sockets;
with GNAT.Sockets; use Gnat.Sockets;
procedure DNSQuerying is
procedure DNSQuerying is
Line 36: Line 36:
begin
begin
Print_Address_Info ("ipv6.google.com", "https", Family_Inet6);
Print_Address_Info ("ipv6.google.com", "https", Family_Inet6);
end DNSQuerying;</lang>
end DNSQuerying;</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>


/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
Line 205: Line 205:
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
bx lr @ return
</syntaxhighlight>
</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
This code uses Windows built-in 'nslookup' command (and a temporary file):
This code uses Windows built-in 'nslookup' command (and a temporary file):
<lang AutoHotkey>Url := "www.kame.net" , LogFile := "Ping_" A_Now ".log"
<syntaxhighlight lang="autohotkey">Url := "www.kame.net" , LogFile := "Ping_" A_Now ".log"
Runwait, %comspec% /c nslookup %Url%>%LogFile%, , hide
Runwait, %comspec% /c nslookup %Url%>%LogFile%, , hide
FileRead, Contents, %LogFile%
FileRead, Contents, %LogFile%
FileDelete, %LogFile%
FileDelete, %LogFile%
RegExMatch(Contents,"Addresses:.+(`r?`n\s+.+)*",Match)
RegExMatch(Contents,"Addresses:.+(`r?`n\s+.+)*",Match)
MsgBox, % RegExReplace(Match,"(Addresses:|[ `t])")</lang>
MsgBox, % RegExReplace(Match,"(Addresses:|[ `t])")</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>:: DNS Query Task from Rosetta Code Wiki
<syntaxhighlight lang="dos">:: DNS Query Task from Rosetta Code Wiki
:: Batch File Implementation
:: Batch File Implementation


Line 254: Line 254:
)
)
endlocal
endlocal
goto :EOF</lang>
goto :EOF</syntaxhighlight>
{{Out}}
{{Out}}
<pre>DOMAIN: "www.kame.net"
<pre>DOMAIN: "www.kame.net"
Line 264: Line 264:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> name$ = "www.kame.net"
<syntaxhighlight lang="bbcbasic"> name$ = "www.kame.net"
AF_INET = 2
AF_INET = 2
Line 316: Line 316:
SYS `WSACleanup`
SYS `WSACleanup`
</syntaxhighlight>
</lang>
Output:
Output:
<pre>IPv4 address = 203.178.141.194
<pre>IPv4 address = 203.178.141.194
Line 324: Line 324:
This solution uses <code>getaddrinfo()</code>, a standard function from RFC 3493. This code resembles an example from [http://www.openbsd.org/cgi-bin/man.cgi?query=getaddrinfo&apropos=0&sektion=3&manpath=OpenBSD+Current&arch=i386&format=html getaddrinfo(3)], the [[BSD]] manual page. Whereas the man page code connects to <code>www.kame.net</code>, this code only prints the numeric addresses.
This solution uses <code>getaddrinfo()</code>, a standard function from RFC 3493. This code resembles an example from [http://www.openbsd.org/cgi-bin/man.cgi?query=getaddrinfo&apropos=0&sektion=3&manpath=OpenBSD+Current&arch=i386&format=html getaddrinfo(3)], the [[BSD]] manual page. Whereas the man page code connects to <code>www.kame.net</code>, this code only prints the numeric addresses.


<lang c>#include <sys/types.h>
<syntaxhighlight lang="c">#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netdb.h> /* getaddrinfo, getnameinfo */
#include <netdb.h> /* getaddrinfo, getnameinfo */
Line 381: Line 381:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Implementation takes a host name string as a parameter, and returns the IP addresses in a comma-delimited string. Note that a failed lookup throws a SocketException.
Implementation takes a host name string as a parameter, and returns the IP addresses in a comma-delimited string. Note that a failed lookup throws a SocketException.
<lang csharp>
<syntaxhighlight lang="csharp">
private string LookupDns(string s)
private string LookupDns(string s)
{
{
Line 404: Line 404:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
This example makes use of the Boost library. The asio bit is header-only, but requires linking boost-system (e.g. -lboost_system). The compiler may also need to be told to use C++11 semantics (e.g. -std=c++11).
This example makes use of the Boost library. The asio bit is header-only, but requires linking boost-system (e.g. -lboost_system). The compiler may also need to be told to use C++11 semantics (e.g. -std=c++11).
<syntaxhighlight lang="c++">
<lang C++>
#include <boost/asio.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <iostream>
Line 429: Line 429:
return rc;
return rc;
}
}
</syntaxhighlight>
</lang>


=={{header|Caché ObjectScript}}==
=={{header|Caché ObjectScript}}==


<lang cos>
<syntaxhighlight lang="cos">
Class Utils.Net Extends %RegisteredObject
Class Utils.Net Extends %RegisteredObject
{
{
Line 482: Line 482:


}
}
</syntaxhighlight>
</lang>


<lang cos>
<syntaxhighlight lang="cos">
Class Utils.OS Extends %RegisteredObject
Class Utils.OS Extends %RegisteredObject
{
{
Line 519: Line 519:


}
}
</syntaxhighlight>
</lang>
{{out|Examples}}
{{out|Examples}}
<pre>
<pre>
Line 536: Line 536:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(import java.net.InetAddress java.net.Inet4Address java.net.Inet6Address)
<syntaxhighlight lang="clojure">(import java.net.InetAddress java.net.Inet4Address java.net.Inet6Address)


(doseq [addr (InetAddress/getAllByName "www.kame.net")]
(doseq [addr (InetAddress/getAllByName "www.kame.net")]
(cond
(cond
(instance? Inet4Address addr) (println "IPv4:" (.getHostAddress addr))
(instance? Inet4Address addr) (println "IPv4:" (.getHostAddress addr))
(instance? Inet6Address addr) (println "IPv6:" (.getHostAddress addr))))</lang>
(instance? Inet6Address addr) (println "IPv6:" (.getHostAddress addr))))</syntaxhighlight>
Output:
Output:
<pre>IPv4: 203.178.141.194
<pre>IPv4: 203.178.141.194
Line 547: Line 547:


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<lang coffeescript>
<syntaxhighlight lang="coffeescript">
# runs under node.js
# runs under node.js
dns = require 'dns'
dns = require 'dns'
Line 558: Line 558:
console.log 'IP6'
console.log 'IP6'
console.log addresses
console.log addresses
</syntaxhighlight>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
common lisp does not have a standard network api. the following examples are using native implementations
common lisp does not have a standard network api. the following examples are using native implementations
{{works with|SBCL}}
{{works with|SBCL}}
<lang lisp>(sb-bsd-sockets:host-ent-addresses
<syntaxhighlight lang="lisp">(sb-bsd-sockets:host-ent-addresses
(sb-bsd-sockets:get-host-by-name "www.rosettacode.org"))
(sb-bsd-sockets:get-host-by-name "www.rosettacode.org"))
(#(71 19 147 227))</lang>
(#(71 19 147 227))</syntaxhighlight>
{{works with|CMUCL}}
{{works with|CMUCL}}
<lang lisp>(let ((hostname (extensions:lookup-host-entry "www.rosettacode.org")))
<syntaxhighlight lang="lisp">(let ((hostname (extensions:lookup-host-entry "www.rosettacode.org")))
(print (map 'list #'extensions:ip-string (host-entry-addr-list hostname))))
(print (map 'list #'extensions:ip-string (host-entry-addr-list hostname))))
("71.19.147.227")</lang>
("71.19.147.227")</syntaxhighlight>
{{works with|Clozure}}
{{works with|Clozure}}
<lang lisp>(ipaddr-to-dotted (lookup-hostname "www.rosettacode.org"))
<syntaxhighlight lang="lisp">(ipaddr-to-dotted (lookup-hostname "www.rosettacode.org"))
"104.28.10.103"</lang>
"104.28.10.103"</syntaxhighlight>
{{works with|Clisp}}
{{works with|Clisp}}
<lang lisp>(hostent-addr-list (resolve-host-ipaddr "www.rosettacode.org"))
<syntaxhighlight lang="lisp">(hostent-addr-list (resolve-host-ipaddr "www.rosettacode.org"))
("104.28.11.103" "104.28.10.103")</lang>
("104.28.11.103" "104.28.10.103")</syntaxhighlight>
the usocket library contains a (get-hosts-by-name) function in all of its [http://trac.common-lisp.net/usocket/browser/usocket/trunk/backend backends]. unfortunately it does not expose the functions in its public interface. but since the license is MIT, it may be a suitable source to copy code for your own use.
the usocket library contains a (get-hosts-by-name) function in all of its [http://trac.common-lisp.net/usocket/browser/usocket/trunk/backend backends]. unfortunately it does not expose the functions in its public interface. but since the license is MIT, it may be a suitable source to copy code for your own use.


{{libheader|iolib}} is a portable library that:
{{libheader|iolib}} is a portable library that:
{{works with|SBCL}}{{works with|CMUCL}}{{works with|Clisp}}{{works with|Clozure}}
{{works with|SBCL}}{{works with|CMUCL}}{{works with|Clisp}}{{works with|Clozure}}
<lang lisp>(iolib:lookup-hostname "www.kame.net" :ipv6 t)
<syntaxhighlight lang="lisp">(iolib:lookup-hostname "www.kame.net" :ipv6 t)


#/IOLIB.SOCKETS:IP/203.178.141.194
#/IOLIB.SOCKETS:IP/203.178.141.194
Line 586: Line 586:
"orange.kame.net"
"orange.kame.net"
(("orange.kame.net" . #/IOLIB.SOCKETS:IP/203.178.141.194)
(("orange.kame.net" . #/IOLIB.SOCKETS:IP/203.178.141.194)
("orange.kame.net" . #/IOLIB.SOCKETS:IP/2001:200:dff:fff1:216:3eff:feb1:44d7))</lang>
("orange.kame.net" . #/IOLIB.SOCKETS:IP/2001:200:dff:fff1:216:3eff:feb1:44d7))</syntaxhighlight>


In Allegro Common Lisp there's a nice standard library called [http://franz.com/support/documentation/current/doc/socket.htm socket].
In Allegro Common Lisp there's a nice standard library called [http://franz.com/support/documentation/current/doc/socket.htm socket].
{{works with|Allegro}}
{{works with|Allegro}}
<lang lisp>(socket:ipaddr-to-dotted
<syntaxhighlight lang="lisp">(socket:ipaddr-to-dotted
(socket:dns-query "www.rosettacode.org"))
(socket:dns-query "www.rosettacode.org"))
"104.28.10.103"</lang>
"104.28.10.103"</syntaxhighlight>


In Lispworks the [http://www.lispworks.com/documentation/lw71/LW/html/lw-269.htm COMM] package provides information about IP addresses.
In Lispworks the [http://www.lispworks.com/documentation/lw71/LW/html/lw-269.htm COMM] package provides information about IP addresses.


{{works with|Lispworks}}
{{works with|Lispworks}}
<lang lisp>(require "comm")
<syntaxhighlight lang="lisp">(require "comm")
(comm:ip-address-string (comm:get-host-entry "www.rosettacode.org" :fields '(:address)))
(comm:ip-address-string (comm:get-host-entry "www.rosettacode.org" :fields '(:address)))
"104.28.10.103"
"104.28.10.103"
</syntaxhighlight>
</lang>


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang ruby>require "socket"
<syntaxhighlight lang="ruby">require "socket"


Socket::Addrinfo.resolve(
Socket::Addrinfo.resolve(
Line 611: Line 611:
).each { |a|
).each { |a|
puts a.ip_address.address
puts a.ip_address.address
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 619: Line 619:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.socket;
<syntaxhighlight lang="d">import std.stdio, std.socket;


void main() {
void main() {
Line 629: Line 629:
a = getAddressInfo(domain, port, AddressFamily.INET6);
a = getAddressInfo(domain, port, AddressFamily.INET6);
writefln("IPv6 address for %s: %s", domain, a[0].address);
writefln("IPv6 address for %s: %s", domain, a[0].address);
}</lang>
}</syntaxhighlight>
<pre>IPv4 address for www.kame.net: 203.178.141.194:80
<pre>IPv4 address for www.kame.net: 203.178.141.194:80
IPv6 address for www.kame.net: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80</pre>
IPv6 address for www.kame.net: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80</pre>
Line 636: Line 636:
The included Indy components wrap GetAddressInfo.
The included Indy components wrap GetAddressInfo.


<lang Delphi>program DNSQuerying;
<syntaxhighlight lang="delphi">program DNSQuerying;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 656: Line 656:
lStack.Free;
lStack.Free;
end;
end;
end.</lang>
end.</syntaxhighlight>


Output:
Output:
Line 664: Line 664:


=={{header|Erlang}}==
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
33> {ok, {hostent, Host, Aliases, AddrType, Bytes, AddrList}} = inet:gethostbyname("www.kame.net", inet).
33> {ok, {hostent, Host, Aliases, AddrType, Bytes, AddrList}} = inet:gethostbyname("www.kame.net", inet).
{ok,{hostent,"orange.kame.net",
{ok,{hostent,"orange.kame.net",
Line 679: Line 679:
37> [inet_parse:ntoa(Addr) || Addr <- AddrList].
37> [inet_parse:ntoa(Addr) || Addr <- AddrList].
["2001:200:DFF:FFF1:216:3EFF:FEB1:44D7"]
["2001:200:DFF:FFF1:216:3EFF:FEB1:44D7"]
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: dns io kernel sequences ;
<syntaxhighlight lang="factor">USING: dns io kernel sequences ;


"www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi
"www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi
[ message>names second print ] bi@</lang>
[ message>names second print ] bi@</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 693: Line 693:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>for a = callJava["java.net.InetAddress", "getAllByName", "www.kame.net"]
<syntaxhighlight lang="frink">for a = callJava["java.net.InetAddress", "getAllByName", "www.kame.net"]
println[a.getHostAddress[]]</lang>
println[a.getHostAddress[]]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 703: Line 703:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 716: Line 716:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 723: Line 723:


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang groovy>def addresses = InetAddress.getAllByName('www.kame.net')
<syntaxhighlight lang="groovy">def addresses = InetAddress.getAllByName('www.kame.net')
println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}"
println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}"
println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"</lang>
println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"</syntaxhighlight>
Output:
Output:
<pre>IPv4: 203.178.141.194
<pre>IPv4: 203.178.141.194
Line 731: Line 731:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>module Main where
<syntaxhighlight lang="haskell">module Main where


import Network.Socket
import Network.Socket
Line 747: Line 747:
main = showIPs "www.kame.net"
main = showIPs "www.kame.net"
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 759: Line 759:
The following was tested only in Unicon:
The following was tested only in Unicon:


<lang unicon>
<syntaxhighlight lang="unicon">
procedure main(A)
procedure main(A)
host := gethost( A[1] | "www.kame.net") | stop("can't translate")
host := gethost( A[1] | "www.kame.net") | stop("can't translate")
write(host.name, ": ", host.addresses)
write(host.name, ": ", host.addresses)
end
end
</syntaxhighlight>
</lang>


Sample Run:
Sample Run:
Line 785: Line 785:
Anyways:
Anyways:


<lang J> 2!:0'dig -4 +short www.kame.net'
<syntaxhighlight lang="j"> 2!:0'dig -4 +short www.kame.net'
orange.kame.net.
orange.kame.net.
203.178.141.194
203.178.141.194
Line 791: Line 791:
2!:0'dig -6 +short www.kame.net'
2!:0'dig -6 +short www.kame.net'
|interface error
|interface error
| 2!:0'dig -6 +short www.kame.net'</lang>
| 2!:0'dig -6 +short www.kame.net'</syntaxhighlight>


Put differently: in the IPv4 DNS system, www.kame.net is currently a CNAME record for orange.kame.net which had the address 203.178.141.194.
Put differently: in the IPv4 DNS system, www.kame.net is currently a CNAME record for orange.kame.net which had the address 203.178.141.194.
Line 798: Line 798:


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.net.InetAddress;
<syntaxhighlight lang="java">import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.Inet6Address;
Line 819: Line 819:
}
}
}
}
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 827: Line 827:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang JavaScript>const dns = require("dns");
<syntaxhighlight lang="javascript">const dns = require("dns");


dns.lookup("www.kame.net", {
dns.lookup("www.kame.net", {
Line 835: Line 835:
console.log(addresses);
console.log(addresses);
})
})
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 844: Line 844:
=={{header|Julia}}==
=={{header|Julia}}==
As entered at the REPL command line:
As entered at the REPL command line:
<lang julia>
<syntaxhighlight lang="julia">
julia> using Sockets
julia> using Sockets


Line 853: Line 853:
ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
ip"2001:200:dff:fff1:216:3eff:feb1:44d7"


</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


import java.net.InetAddress
import java.net.InetAddress
Line 882: Line 882:
fun main(args: Array<String>) {
fun main(args: Array<String>) {
showIPAddresses("www.kame.net")
showIPAddresses("www.kame.net")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 894: Line 894:
=={{header|Lasso}}==
=={{header|Lasso}}==
The DNS lookup methods in Lasso do not support IPv6 addresses at this time, only IPv4.
The DNS lookup methods in Lasso do not support IPv6 addresses at this time, only IPv4.
<lang Lasso>dns_lookup('www.kame.net', -type='A')</lang>
<syntaxhighlight lang="lasso">dns_lookup('www.kame.net', -type='A')</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
Line 900: Line 900:
{{libheader|LuaSocket}}
{{libheader|LuaSocket}}


<lang lua>local socket = require('socket')
<syntaxhighlight lang="lua">local socket = require('socket')
local ip_tbl = socket.dns.getaddrinfo('www.kame.net')
local ip_tbl = socket.dns.getaddrinfo('www.kame.net')


Line 906: Line 906:
io.write(string.format('%s: %s\n', v.family, v.addr))
io.write(string.format('%s: %s\n', v.family, v.addr))
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 916: Line 916:
Neko does not yet support ipv6. ipv4 addresses are returned as Int32.
Neko does not yet support ipv6. ipv4 addresses are returned as Int32.


<lang ActionScript>/* dns in neko */
<syntaxhighlight lang="actionscript">/* dns in neko */
var host_resolve = $loader.loadprim("std@host_resolve", 1);
var host_resolve = $loader.loadprim("std@host_resolve", 1);
var host_to_string = $loader.loadprim("std@host_to_string", 1);
var host_to_string = $loader.loadprim("std@host_to_string", 1);
Line 924: Line 924:


$print("www.kame.net: ", ip, ", ", host_to_string(ip), "\n");
$print("www.kame.net: ", ip, ", ", host_to_string(ip), "\n");
$print(host_to_string(ip), ": ", host_reverse(ip), "\n");</lang>
$print(host_to_string(ip), ": ", host_reverse(ip), "\n");</syntaxhighlight>


{{out}}
{{out}}
Line 933: Line 933:


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang netrexx>
<syntaxhighlight lang="netrexx">
/* NetRexx */
/* NetRexx */
options replace format comments java crossref symbols nobinary
options replace format comments java crossref symbols nobinary
Line 947: Line 947:
end
end
end ir
end ir
</syntaxhighlight>
</lang>
;Output
;Output
<pre>
<pre>
Line 955: Line 955:


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang newLISP>


(define (dnsLookup site , ipv)
(define (dnsLookup site , ipv)
Line 968: Line 968:


(dnsLookup "www.kame.net")
(dnsLookup "www.kame.net")
</syntaxhighlight>
</lang>
;Output
;Output
<pre>
<pre>
Line 977: Line 977:
=={{header|Nim}}==
=={{header|Nim}}==


<lang nim>import nativesockets
<syntaxhighlight lang="nim">import nativesockets


iterator items(ai: ptr AddrInfo): ptr AddrInfo =
iterator items(ai: ptr AddrInfo): ptr AddrInfo =
Line 992: Line 992:
echo getAddrString i.aiAddr
echo getAddrString i.aiAddr


when isMainModule: main()</lang>
when isMainModule: main()</syntaxhighlight>


{{out}}
{{out}}
Line 1,001: Line 1,001:
{{Works with|oo2c version 2}}
{{Works with|oo2c version 2}}
IO:Address module supports only IPv4
IO:Address module supports only IPv4
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE DNSQuery;
MODULE DNSQuery;
IMPORT
IMPORT
Line 1,018: Line 1,018:
Do;
Do;
END DNSQuery.
END DNSQuery.
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,025: Line 1,025:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>use System.IO.Net;
<syntaxhighlight lang="objeck">use System.IO.Net;


class Rosetta {
class Rosetta {
Line 1,034: Line 1,034:
};
};
}
}
}</lang>
}</syntaxhighlight>


Output:
Output:
Line 1,043: Line 1,043:
=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>let dns_query ~host ~ai_family =
<syntaxhighlight lang="ocaml">let dns_query ~host ~ai_family =
let opts = [
let opts = [
Unix.AI_FAMILY ai_family;
Unix.AI_FAMILY ai_family;
Line 1,062: Line 1,062:
Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET);
Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET);
Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6);
Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6);
;;</lang>
;;</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use feature 'say';
<syntaxhighlight lang="perl">use feature 'say';
use Socket qw(getaddrinfo getnameinfo);
use Socket qw(getaddrinfo getnameinfo);


Line 1,071: Line 1,071:
die "getaddrinfo error: $err" if $err;
die "getaddrinfo error: $err" if $err;


say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res</lang>
say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res</syntaxhighlight>
{{out}}
{{out}}
<pre>203.178.141.194
<pre>203.178.141.194
Line 1,079: Line 1,079:
Translated from C/MSDN/several man pages.<br>
Translated from C/MSDN/several man pages.<br>
<b>NB:</b> may warrant further testing, see output.
<b>NB:</b> may warrant further testing, see output.
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">cffi</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">cffi</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,223: Line 1,223:
<span style="color: #0000FF;">?</span><span style="color: #000000;">get_name_info</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"www.kame.net"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">get_name_info</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"www.kame.net"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
Note that windows nslookup shows an IPv6 that this does not, whereas
Note that windows nslookup shows an IPv6 that this does not, whereas
Line 1,244: Line 1,244:
=={{header|PHP}}==
=={{header|PHP}}==
Works for PHP5 (Windows > 5.3.0)
Works for PHP5 (Windows > 5.3.0)
<lang php><?php
<syntaxhighlight lang="php"><?php
$ipv4_record = dns_get_record("www.kame.net",DNS_A);
$ipv4_record = dns_get_record("www.kame.net",DNS_A);
$ipv6_record = dns_get_record("www.kame.net",DNS_AAAA);
$ipv6_record = dns_get_record("www.kame.net",DNS_AAAA);
print "ipv4: " . $ipv4_record[0]["ip"] . "\n";
print "ipv4: " . $ipv4_record[0]["ip"] . "\n";
print "ipv6: " . $ipv6_record[0]["ipv6"] . "\n";
print "ipv6: " . $ipv6_record[0]["ipv6"] . "\n";
?></lang>
?></syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(make
<syntaxhighlight lang="picolisp">(make
(in '(host "www.kame.net")
(in '(host "www.kame.net")
(while (from "address ")
(while (from "address ")
(link (till "^J" T)) ) ) )</lang>
(link (till "^J" T)) ) ) )</syntaxhighlight>
Output:
Output:
<pre>-> ("203.178.141.194" "2001:200:dff:fff1:216:3eff:feb1:44d7")</pre>
<pre>-> ("203.178.141.194" "2001:200:dff:fff1:216:3eff:feb1:44d7")</pre>
Line 1,261: Line 1,261:
=={{header|Pike}}==
=={{header|Pike}}==


<syntaxhighlight lang="pike">
<lang Pike>
> array ips = Protocols.DNS.gethostbyname("www.kame.net")[1] || ({});
> array ips = Protocols.DNS.gethostbyname("www.kame.net")[1] || ({});
> write(ips*"\n");
> write(ips*"\n");
</syntaxhighlight>
</lang>
Output:
Output:
<pre>203.178.141.194
<pre>203.178.141.194
Line 1,270: Line 1,270:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>$DNS = Resolve-DnsName -Name www.kame.net
<syntaxhighlight lang="powershell">$DNS = Resolve-DnsName -Name www.kame.net
Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address</lang>
Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>>>> import socket
<syntaxhighlight lang="python">>>> import socket
>>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80))
>>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80))
>>> for ip in ips: print ip
>>> for ip in ips: print ip
...
...
2001:200:dff:fff1:216:3eff:feb1:44d7
2001:200:dff:fff1:216:3eff:feb1:44d7
203.178.141.194</lang>
203.178.141.194</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
Line 1,287: Line 1,287:
If the following is saved as <tt>dns.cpp</tt>:
If the following is saved as <tt>dns.cpp</tt>:


<lang cpp>
<syntaxhighlight lang="cpp">
#include <Rcpp.h>
#include <Rcpp.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
Line 1,335: Line 1,335:


}
}
</syntaxhighlight>
</lang>


It can be used to perform the task in R:
It can be used to perform the task in R:


<syntaxhighlight lang="r">
<lang R>
library(Rcpp)
library(Rcpp)
sourceCpp("dns.cpp")
sourceCpp("dns.cpp")
Line 1,345: Line 1,345:
## [1] "203.178.141.194"
## [1] "203.178.141.194"
## [2] "2001:200:dff:fff1:216:3eff:feb1:44d7"
## [2] "2001:200:dff:fff1:216:3eff:feb1:44d7"
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
Line 1,351: Line 1,351:
The following finds an IPv4 address. Currently, the API does not support returning an IPv6 address.
The following finds an IPv4 address. Currently, the API does not support returning an IPv6 address.


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


(require net/dns)
(require net/dns)
(dns-get-address "8.8.8.8" "www.kame.net")
(dns-get-address "8.8.8.8" "www.kame.net")
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 1,362: Line 1,362:
{{works with|Rakudo|2017.01}}
{{works with|Rakudo|2017.01}}


<lang perl6>use Net::DNS;
<syntaxhighlight lang="raku" line>use Net::DNS;


my $resolver = Net::DNS.new('8.8.8.8');
my $resolver = Net::DNS.new('8.8.8.8');
Line 1,370: Line 1,370:


say $ip4[0].octets.join: '.';
say $ip4[0].octets.join: '.';
say $ip6[0].octets.».fmt("%.2X").join.comb(4).join: ':';</lang>
say $ip6[0].octets.».fmt("%.2X").join.comb(4).join: ':';</syntaxhighlight>
{{out}}
{{out}}
<pre>203.178.141.194
<pre>203.178.141.194
Line 1,379: Line 1,379:


Execution note: &nbsp; if the information for &nbsp; &nbsp; '''PING &nbsp; -6 &nbsp; ···''' &nbsp; &nbsp; is blank, you may need to install &nbsp; (on some <br>Microsoft Windows systems) &nbsp; the IPV6 interface using the command: &nbsp; &nbsp; ''' IPV6 &nbsp; install '''
Execution note: &nbsp; if the information for &nbsp; &nbsp; '''PING &nbsp; -6 &nbsp; ···''' &nbsp; &nbsp; is blank, you may need to install &nbsp; (on some <br>Microsoft Windows systems) &nbsp; the IPV6 interface using the command: &nbsp; &nbsp; ''' IPV6 &nbsp; install '''
<lang rexx>/*REXX program displays IPV4 and IPV6 addresses for a supplied domain name.*/
<syntaxhighlight lang="rexx">/*REXX program displays IPV4 and IPV6 addresses for a supplied domain name.*/
parse arg tar . /*obtain optional domain name from C.L.*/
parse arg tar . /*obtain optional domain name from C.L.*/
if tar=='' then tar= 'www.kame.net' /*Not specified? Then use the default.*/
if tar=='' then tar= 'www.kame.net' /*Not specified? Then use the default.*/
Line 1,394: Line 1,394:
end /*j*/ /* └─◄ force (TEMP) file integrity.*/
end /*j*/ /* └─◄ force (TEMP) file integrity.*/
/*stick a fork in it, we're all done. */
/*stick a fork in it, we're all done. */
'ERASE' tFID /*clean up (delete) the temporary file.*/</lang>
'ERASE' tFID /*clean up (delete) the temporary file.*/</syntaxhighlight>
'''output''' &nbsp; using the default input:
'''output''' &nbsp; using the default input:
<pre>
<pre>
Line 1,402: Line 1,402:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>irb(main):001:0> require 'socket'
<syntaxhighlight lang="ruby">irb(main):001:0> require 'socket'
=> true
=> true
irb(main):002:0> Addrinfo.getaddrinfo("www.kame.net", nil, nil, :DGRAM) \
irb(main):002:0> Addrinfo.getaddrinfo("www.kame.net", nil, nil, :DGRAM) \
irb(main):003:0* .map! { |ai| ai.ip_address }
irb(main):003:0* .map! { |ai| ai.ip_address }
=> ["203.178.141.194", "2001:200:dff:fff1:216:3eff:feb1:44d7"]</lang>
=> ["203.178.141.194", "2001:200:dff:fff1:216:3eff:feb1:44d7"]</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>use std::net::ToSocketAddrs;
<syntaxhighlight lang="rust">use std::net::ToSocketAddrs;


fn main() {
fn main() {
Line 1,424: Line 1,424:
println!("{}", ip_port.ip());
println!("{}", ip_port.ip());
}
}
}</lang>
}</syntaxhighlight>


Output:<pre>203.178.141.194
Output:<pre>203.178.141.194
Line 1,431: Line 1,431:
=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}
{{libheader|Scala}}
<lang Scala>import java.net._
<syntaxhighlight lang="scala">import java.net._


InetAddress.getAllByName("www.kame.net").foreach(x => println(x.getHostAddress))</lang>
InetAddress.getAllByName("www.kame.net").foreach(x => println(x.getHostAddress))</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
{{works with|Guile}}
{{works with|Guile}}
<lang scheme>; Query DNS
<syntaxhighlight lang="scheme">; Query DNS
(define n (car (hostent:addr-list (gethost "www.kame.net"))))
(define n (car (hostent:addr-list (gethost "www.kame.net"))))


Line 1,443: Line 1,443:
(display (inet-ntoa n))(newline)
(display (inet-ntoa n))(newline)
(display (inet-ntop AF_INET n))(newline)
(display (inet-ntop AF_INET n))(newline)
(display (inet-ntop AF_INET6 n))(newline)</lang>
(display (inet-ntop AF_INET6 n))(newline)</syntaxhighlight>
Output:<pre>203.178.141.194
Output:<pre>203.178.141.194
203.178.141.194
203.178.141.194
Line 1,454: Line 1,454:
The function [http://seed7.sourceforge.net/libraries/socket.htm#numericAddress%28in_socketAddress%29 numericAddress]
The function [http://seed7.sourceforge.net/libraries/socket.htm#numericAddress%28in_socketAddress%29 numericAddress]
is used to get the IP address of the specified host.
is used to get the IP address of the specified host.
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "socket.s7i";
include "socket.s7i";


Line 1,460: Line 1,460:
begin
begin
writeln(numericAddress(inetSocketAddress("www.kame.net", 1024)));
writeln(numericAddress(inetSocketAddress("www.kame.net", 1024)));
end func;</lang>
end func;</syntaxhighlight>


Output:
Output:
Line 1,468: Line 1,468:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var (err, *res) = Socket.getaddrinfo(
<syntaxhighlight lang="ruby">var (err, *res) = Socket.getaddrinfo(
'www.kame.net', 0,
'www.kame.net', 0,
Hash.new(protocol => Socket.IPPROTO_TCP)
Hash.new(protocol => Socket.IPPROTO_TCP)
Line 1,475: Line 1,475:
res.each { |z|
res.each { |z|
say [Socket.getnameinfo(z{:addr}, Socket.NI_NUMERICHOST)][1];
say [Socket.getnameinfo(z{:addr}, Socket.NI_NUMERICHOST)][1];
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,485: Line 1,485:
=={{header|Standard ML}}==
=={{header|Standard ML}}==
The basis library provides IPv4 support only:
The basis library provides IPv4 support only:
<lang sml>- Option.map (NetHostDB.toString o NetHostDB.addr) (NetHostDB.getByName "www.kame.net");
<syntaxhighlight lang="sml">- Option.map (NetHostDB.toString o NetHostDB.addr) (NetHostDB.getByName "www.kame.net");
val it = SOME "203.178.141.194": string option</lang>
val it = SOME "203.178.141.194": string option</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 1,492: Line 1,492:
{{tcllib|dns}}
{{tcllib|dns}}
{{libheader|udp}}
{{libheader|udp}}
<lang tcl>package require udp; # Query by UDP more widely supported, but requires external package
<syntaxhighlight lang="tcl">package require udp; # Query by UDP more widely supported, but requires external package
package require dns
package require dns


Line 1,501: Line 1,501:
update; # Let queries complete
update; # Let queries complete
}
}
puts "primary addresses of $host are:\n\tIPv4» [dns::address $v4]\n\tIPv6» [dns::address $v6]"</lang>
puts "primary addresses of $host are:\n\tIPv4» [dns::address $v4]\n\tIPv6» [dns::address $v6]"</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,511: Line 1,511:
=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
===Using dig===
===Using dig===
<lang bash>
<syntaxhighlight lang="bash">
Aamrun ~ % dig www.kame.net A www.kame.net AAAA +short
Aamrun ~ % dig www.kame.net A www.kame.net AAAA +short
mango.itojun.org.
mango.itojun.org.
Line 1,519: Line 1,519:
2001:2f0:0:8800::1:1
2001:2f0:0:8800::1:1
Aamrun ~ %
Aamrun ~ %
</syntaxhighlight>
</lang>
===Using nslookup===
===Using nslookup===
<lang bash>
<syntaxhighlight lang="bash">
Aamrun ~ % nslookup
Aamrun ~ % nslookup
> set q=AAAA
> set q=AAAA
Line 1,546: Line 1,546:


Aamrun ~ %
Aamrun ~ %
</syntaxhighlight>
</lang>


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function dns_query(url,ver)
Function dns_query(url,ver)
Set r = New RegExp
Set r = New RegExp
Line 1,566: Line 1,566:


Call dns_query(WScript.Arguments(0),WScript.Arguments(1))
Call dns_query(WScript.Arguments(0),WScript.Arguments(1))
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 1,583: Line 1,583:


However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.
<lang ecmascript>/* dns_query.wren */
<syntaxhighlight lang="ecmascript">/* dns_query.wren */


class Net {
class Net {
Line 1,591: Line 1,591:
var host = "orange.kame.net"
var host = "orange.kame.net"
var addrs = Net.lookupHost(host).split(", ")
var addrs = Net.lookupHost(host).split(", ")
System.print(addrs.join("\n"))</lang>
System.print(addrs.join("\n"))</syntaxhighlight>
which we embed in the following Go program and run it:
which we embed in the following Go program and run it:
{{libheader|WrenGo}}
{{libheader|WrenGo}}
<lang go>/* go run dns_query.go */
<syntaxhighlight lang="go">/* go run dns_query.go */


package main
package main
Line 1,624: Line 1,624:
vm.InterpretFile(fileName)
vm.InterpretFile(fileName)
vm.Free()
vm.Free()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,634: Line 1,634:
=={{header|zkl}}==
=={{header|zkl}}==
The addrInfo method is just a front end to POSIX getaddrinfo.
The addrInfo method is just a front end to POSIX getaddrinfo.
<lang zkl>zkl: Network.TCPClientSocket.addrInfo("www.kame.net")
<syntaxhighlight lang="zkl">zkl: Network.TCPClientSocket.addrInfo("www.kame.net")
L("orange.kame.net",L("203.178.141.194","2001:200:dff:fff1:216:3eff:feb1:44d7"))</lang>
L("orange.kame.net",L("203.178.141.194","2001:200:dff:fff1:216:3eff:feb1:44d7"))</syntaxhighlight>