f.dns.tohostname 能用cosocket 替代吗 or 用其他的替代?

58 views
Skip to first unread message

g_l...@qq.com

unread,
Aug 8, 2017, 4:52:35 AM8/8/17
to openresty


f=require("socket")
print(f.dns.tohostname('8.8.8.8'))
google-public-dns-a.google.com table: 0x7f9a33d01cb0


以上功能 获取 hostname ,怎么实现 除 linux 命令 host,  第3方的 socket 库外, 最好是  春哥大神写的库。

g_l...@qq.com

unread,
Aug 8, 2017, 5:25:15 AM8/8/17
to openresty

cosocket 没有这个方法吗?gethostname()


在 2017年8月8日星期二 UTC+8下午4:52:35,g_l...@qq.com写道:

tokers

unread,
Aug 8, 2017, 9:36:20 PM8/8/17
to openresty
Hi!
nginx 有个变量 $hostname 的,你可以通过

ngx.var.hostname 来获取,不过有使用阶段限制(没法在 init_by_lua 和 init_worker_by_lua 使用)
如果你想要在这两个阶段使用,建议你使用 ffi,自己调用 gethostname。

Sen Han

unread,
Aug 8, 2017, 9:47:01 PM8/8/17
to open...@googlegroups.com
https://github.com/openresty/lua-resty-dns#reverse_query

g_linux是要的这个么?ip -> hostname逆解析 完全异步的cosocket实现

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

tokers

unread,
Aug 8, 2017, 9:48:12 PM8/8/17
to openresty
囧,理解错意思了 😆


On Tuesday, August 8, 2017 at 4:52:35 PM UTC+8, g_l...@qq.com wrote:

g_l...@qq.com

unread,
Aug 8, 2017, 9:48:38 PM8/8/17
to openresty
谢谢你的回复,不过 ngx.var.hostname 是获取到服务端的主机名,我想获取客服端的主机名字。

思路是这样的。

1 获取客服端的IP地址    
2 获取到的客服端ip 地址查询出客服的主机名字。

在 2017年8月9日星期三 UTC+8上午9:36:20,tokers写道:

g_l...@qq.com

unread,
Aug 8, 2017, 9:50:09 PM8/8/17
to openresty
哥哥

思路是这样的。

1 获取客服端的IP地址    
2 获取到的客服端ip 地址查询出客服的主机名字。

在 2017年8月9日星期三 UTC+8上午9:47:01,yunthanatos写道:
https://github.com/openresty/lua-resty-dns#reverse_query

g_linux是要的这个么?ip -> hostname逆解析 完全异步的cosocket实现
2017-08-09 9:36 GMT+08:00 tokers <zcha...@gmail.com>:
Hi!
nginx 有个变量 $hostname 的,你可以通过

ngx.var.hostname 来获取,不过有使用阶段限制(没法在 init_by_lua 和 init_worker_by_lua 使用)
如果你想要在这两个阶段使用,建议你使用 ffi,自己调用 gethostname。


On Tuesday, August 8, 2017 at 4:52:35 PM UTC+8, g_l...@qq.com wrote:


f=require("socket")
print(f.dns.tohostname('8.8.8.8'))
google-public-dns-a.google.com table: 0x7f9a33d01cb0


以上功能 获取 hostname ,怎么实现 除 linux 命令 host,  第3方的 socket 库外, 最好是  春哥大神写的库。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

g_l...@qq.com

unread,
Aug 8, 2017, 9:51:58 PM8/8/17
to openresty
哥哥
   local resolver = require "resty.dns.resolver"
    local ptr4 = resolver.arpa_str("1.2.3.4")
这个我昨天测试过了。。不能达到我的要求。。和 host 命令出来的效果 区别很大。

我只获取 客服端的主机名字。

在 2017年8月9日星期三 UTC+8上午9:47:01,yunthanatos写道:


g_linux是要的这个么?ip -> hostname逆解析 完全异步的cosocket实现
2017-08-09 9:36 GMT+08:00 tokers <zcha...@gmail.com>:
Hi!
nginx 有个变量 $hostname 的,你可以通过

ngx.var.hostname 来获取,不过有使用阶段限制(没法在 init_by_lua 和 init_worker_by_lua 使用)
如果你想要在这两个阶段使用,建议你使用 ffi,自己调用 gethostname。


On Tuesday, August 8, 2017 at 4:52:35 PM UTC+8, g_l...@qq.com wrote:


f=require("socket")
print(f.dns.tohostname('8.8.8.8'))
google-public-dns-a.google.com table: 0x7f9a33d01cb0


以上功能 获取 hostname ,怎么实现 除 linux 命令 host,  第3方的 socket 库外, 最好是  春哥大神写的库。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

Sen Han

unread,
Aug 8, 2017, 10:04:17 PM8/8/17
to open...@googlegroups.com
按照文档所述,应该使用reverse_query方法:

syntax: answers, err = r:reverse_query(address)

arpa_str只是格式化生成了PTR查询时的ip逆解析域名。


订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com

g_l...@qq.com

unread,
Aug 8, 2017, 10:05:28 PM8/8/17
to openresty
会出现403 的。

在 2017年8月9日星期三 UTC+8上午10:04:17,yunthanatos写道:

g_l...@qq.com

unread,
Aug 8, 2017, 10:16:07 PM8/8/17
to openresty
哥哥

您未获授权,无法查看此网页。

HTTP ERROR 403






body_filter_by_lua_block
{

local resolver = require "resty.dns.resolver"
answers, err = resolver:reverse_query('8.8.8.8')
ngx.arg[1]=answers
}


在 2017年8月9日星期三 UTC+8上午10:04:17,yunthanatos写道:
按照文档所述,应该使用reverse_query方法:

Sen Han

unread,
Aug 8, 2017, 10:23:59 PM8/8/17
to open...@googlegroups.com
刚才测试了下,这个功能是没有问题的,能够正常逆解析。(lua-resty-dns v0.19)

$ answers, err = r:reverse_query("8.8.8.8")

print_r dump:
+1+section [1] <number>
  +ptrdname [google-public-dns-a.google.com] <string>
  +type [12] <number>
  +class [1] <number>
  +name [8.8.8.8.in-addr.arpa] <string>
  +ttl [86399] <number>

订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com

Sen Han

unread,
Aug 8, 2017, 10:28:56 PM8/8/17
to open...@googlegroups.com
可以追查下你那儿的所有异常情况,以便定位出程序问题所在。另外,函数返回的err都是需要认真处理的。

最小测试用例:


local function print_r(root,func)
    local cache = {  [root] = "." }
    local function _dump(t,space,name)
        local temp = {}
        for k,v in pairs(t) do
            local key = tostring(k)
            if cache[v] then
                table.insert(temp,"+" .. key .. " {" .. cache[v].."}")
            elseif type(v) == "table" then
                local new_key = name .. "." .. key
                cache[v] = new_key
                table.insert(temp,"+" .. key .. _dump(v,space ..
                  (next(t,k) and "|" or " " ).. string.rep(" ",#key),new_key))
            else
                local str=tostring(v)
                --str=string.gsub(str,'\n','\\n')
                --table.insert(temp,"+" .. key .. " [" .. str .."]")
                table.insert(temp,"+" .. key .. " [" .. str .."]".." <"..type(v)..">")
            end
        end
        return table.concat(temp,"\n"..space)
    end
    if type(func)=="function" then
        func("print_r dump:\n".._dump(root, "",""))
    else
        print("print_r dump:\n".._dump(root, "",""))
    end
end
 
local resolver = require "resty.resolver"
local r, err = resolver:new{
    nameservers = { "8.8.8.8" }
}
if not r then
    ngx.say("failed to instantiate resolver: ", err)
    return
end

answers, err = r:reverse_query("8.8.8.8")
if type(answers)=="table" then
    print_r(answers,ngx.say)
end
ngx.say(tostring(err))

g_l...@qq.com

unread,
Aug 8, 2017, 10:35:23 PM8/8/17
to openresty
非常感谢你。我的返回为nil

在 2017年8月9日星期三 UTC+8上午10:28:56,yunthanatos写道:

g_l...@qq.com

unread,
Aug 8, 2017, 10:37:14 PM8/8/17
to openresty
body_filter_by_lua_block
{

local resolver = require "resty.dns.resolver"
answers, err = resolver:reverse_query("8.8.8.8")
ngx.arg[1]=type(answers)
}


难道不能放在  body_filter_by_lua_block?

Sen Han

unread,
Aug 8, 2017, 10:46:58 PM8/8/17
to open...@googlegroups.com

local resolver = require "resty.resolver"
local r, err = resolver:new{
    nameservers = { "8.8.8.8" }
}
if not r then
    ngx.say("failed to instantiate resolver: ", err)
    return
end
answers, err = r:reverse_query("8.8.8.8")

是因为代码使用有误才会异常...  



--

g_l...@qq.com

unread,
Aug 8, 2017, 11:04:41 PM8/8/17
to openresty

很感谢你。我搞定了。  能加你微信不。
在 2017年8月9日星期三 UTC+8上午10:46:58,yunthanatos写道:

local resolver = require "resty.resolver"
local r, err = resolver:new{
    nameservers = { "8.8.8.8" }
}
if not r then
    ngx.say("failed to instantiate resolver: ", err)
    return
end
answers, err = r:reverse_query("8.8.8.8")

是因为代码使用有误才会异常...  


2017-08-09 10:37 GMT+08:00 <g_l...@qq.com>:
body_filter_by_lua_block
{

local resolver = require "resty.dns.resolver"
answers, err = resolver:reverse_query("8.8.8.8")
ngx.arg[1]=type(answers)
}


难道不能放在  body_filter_by_lua_block?

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages