Lua socket dns

DNS

Name resolution functions return all information obtained from the resolver in a table of the form:
resolved = {
name = canonic-name,
alias = alias-list,
ip = ip-address-list
}
Note that the alias list can be empty.

socket.dns.gethostname()
:Returns the standard host name for the machine as a string.

socket.dns.tohostname(address)
:Converts from IP address to host name.
Address can be an IP address or host name.
The function returns a string with the canonic host name of the given address, followed by a table with all information returned by the resolver. In case of error, the function returns nil followed by an error message.

socket.dns.toip(address)
Converts from host name to IP address.
Address can be an IP address or host name.
Returns a string with the first IP address found for address, followed by a table with all information returned by the resolver. In case of error, the function returns nil followed by an error message.

local socket = require("socket")

host = socket.dns.gethostname() --返回本机主机名
print(host)

s , resolver =  socket.dns.toip("www.baidu.com") --转为IP
print(s)

ss , tt = socket.dns.tohostname("www.baidu.com") 
print(ss)
print(tt)
for k, v in pairs(tt) do
    print(k, v)
end

print(tt.name)

for k, v in pairs(tt.ip) do
    print(k, v)
end

for k, v in pairs(tt.alias) do
    print(k, v)
end

输出结果:
super-ubuntu
14.215.177.38
www.a.shifen.com
table: 0x1057560
name    www.a.shifen.com
ip  table: 0x1057770
alias   table: 0x10576e0
www.a.shifen.com
1   14.215.177.39
2   14.215.177.38
1   www.baidu.com

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/80416985
DNS
LUA
今日推荐