Lua功能检查ipv4或ipv6还是字符串 [英] Lua function check if ipv4 or ipv6 or string

查看:352
本文介绍了Lua功能检查ipv4或ipv6还是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个函数,我可以将空格修剪后的字符串传递给它,它将为错误(不是字符串)返回
0,为ipv4返回1,对于ipv6返回2,对于不是ip的字符串返回3 .

I'd like to have a function that I can pass a whitespace trimmed string to and it will return
0 for error (not a string) 1 for ipv4 2 for ipv6 3 for a string thats not an ip.

IPv6具有以下规则:

Ipv6 has these rules:

Ipv6由以冒号(:)分隔的8组16位十六进制值表示. 十六进制数字不区分大小写
缩写规则:
1:省略16位值中的前导零
2:用双冒号

Ipv6 is represented by 8 groups of 16-bit hexadecimal values separated by colons (:)
The hexadecimal digits are case-insensitive
Abbreviation rules:
1: Omit leading zeroes in a 16-bit value
2: Replace one or more groups of consecutive zeroes by a double colon

wiki示例显示了三个完全相同的ipv6方式:

wiki example showing 3 ways that are all the same ipv6:

fe80:0000:0000:0000:0202:b3ff:fe1e:8329
fe80:0:0:0:202:b3ff:fe1e:8329
fe80::202:b3ff:fe1e:8329 

我敢肯定,对于ipv4,您只需检查三个即可.然后检查字符串是否全部为
数字,并将.计数为数字,最后检查是否为字符串
将在if语句的末尾,因此如果其不是ipv4/6及其字符串,然后
返回3

I'm reasonably sure for ipv4 you just check for three . then check the string is all
numbers and the .'s are counted as numbers and the last check for just a string
would be at the end of an if statement so if its not ipv4/6 and its a string then
it returns 3

推荐答案

这似乎是一个非常基本的问题.我认为此功能可以满足您的需求...

this seems like a pretty basic problem to solve. i think this function does what you need...

function GetIPType(ip)
    -- must pass in a string value
    if ip == nil or type(ip) ~= "string" then
        return 0
    end

    -- check for format 1.11.111.111 for ipv4
    local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
    if (#chunks == 4) then
        for _,v in pairs(chunks) do
            if (tonumber(v) < 0 or tonumber(v) > 255) then
                return 0
            end
        end
        return 1
    else
        return 0
    end

    -- check for ipv6 format, should be 8 'chunks' of numbers/letters
    local _, chunks = ip:gsub("[%a%d]+%:?", "")
    if chunks == 8 then
        return 2
    end

    -- if we get here, assume we've been given a random string
    return 3
end

使用以下代码对其进行了测试:

tested it with this code:

local IPType = {
    [0] = "Error",
    [1] = "IPv4",
    [2] = "IPv6",
    [3] = "string",
}


local ips = {
    "128.1.0.1",        -- ipv4
    "223.255.254.254",  -- ipv4
    "999.12345.0.0001",     -- invalid ipv4
    "1050:0:0:0:5:600:300c:326b",               -- ipv6
    "1050:0000:0000:0000:0005:0600:300c:326b",  -- ipv6
    "1050:::600:5:1000::",  -- contracted ipv6
    "129.garbage.9.1",  -- string
    129.10              -- error
}

for k,v in pairs(ips) do
    print(v, IPType[GetIPType(v)])
end

生成了以下输出:

128.1.0.1   IPv4
223.255.254.254 IPv4
1050:0:0:0:5:600:300c:326b  IPv6
1050:0000:0000:0000:0005:0600:300c:326b IPv6
129.garbage.9.1 string
129.1   Error

将来,如果您实际张贴尝试编写的代码来解决特定问题,并获得我们的帮助,我们将获得更多有用的反馈.如常见问题所述,SO不是个人代码编写服务.但是,我会给您带来疑问的好处,因为您看起来很新,而且这可能会使其他人受益.上面的代码是基本代码,因此,如果没有捕获我不知道的附带测试用例,请随时进行更新.

in the future, you'll get more helpful feedback if you actually post the code you've attempted to write to solve your particular problem, and let us know where you need help. SO isn't a personal code writing service, as stated in the faq. however, i'll give you the benefit of the doubt since you look new and this is something that could potentially benefit other people. the code above is basic, so feel free to update it if it doesn't catch fringe test cases i don't know about.

这篇关于Lua功能检查ipv4或ipv6还是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆