(bash) 检查子网范围文件中的 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30461623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:02:53  来源:igfitidea点击:

(bash) check if IP in subnet range file

regexlinuxbashshellgrep

提问by Stefano Guarella

I've a list of subnet range in a file:

我有一个文件中的子网范围列表:

2.32.0.0-2.47.255.255-255.240.0.0
2.112.0.0-2.119.255.255-255.248.0.0
2.156.0.0-2.159.255.255-255.252.0.0
2.192.0.0-2.199.255.255-255.248.0.0
...

(The file format is: {startip}-{endip}-{netmask})

(文件格式为:{startip}-{endip}-{netmask}

I need check if an IP is included in one of the subnet in the file.

我需要检查 IP 是否包含在文件的子网之一中。

采纳答案by ShellFish

Try this:

尝试这个:

BEGIN {
    FS="."
    ex = "false"
    split(address, ip, ".")
}
{
    split(
$ awk -v address="2.156.0.5" -f checkIP.awk /path/to/ip/ranges/file
true
$ awk -v address="0.0.0.0" -f checkIP.awk /path/to/ip/ranges/file
false
, range, "[-.]") for (i=1; i<5; i++) { if (ip[i] < range[i] || ip[i] > range[i+4]) break; else if ((ip[i] > range[i] && ip[i] < range[i+4]) || i == 4) ex = "true" } } END { print ex }

Invoke this awk script (checkIP.awk) like this:

checkIP.awk像这样调用这个 awk 脚本 ( ):

echo '127.0.0.0-127.255.255.255-255.0.0.0' | awk -F- '
    BEGIN { ip[1] = 127; ip[2] = 0; ip[3] = 0; ip[4] = 1; } 
    { split(, startIp, "."); split(, endIp, ".");
        for(i = 1; i <= 4; i++) {
            if(ip[i] < int(startIp[i]) || ip[i] > int(endIp[i]))
                break;
        }

        if(i == 5)
            print "matching line: ", 
python3 -c 'from ipaddress import ip_address as IP; list(
    map(print, ((startip, endip) for startip, endip, _ in 
            (ip.split("-") for ip in open("tmp/iplist.txt")) 
            if IP(startip) < IP("127.0.0.1") < IP(endip))))'
; }'

回答by myaut

You may use awkfor that:

您可以awk为此使用:

import sys
from ipaddress import ip_address as IP

ip = IP(sys.argv[1])

with open(sys.argv[2]) as f:
    for line in f:
        startIp, endIp, _ = line.split('-')
        if IP(startIp) < ip < IP(endIp):
            print(line)

IP for searching is initially set as array in BEGIN-clause as array. Each line is compared in for-cycle and if each octet laying between startIpand endIp, "matching line" is printed.

用于搜索的 IP 最初在 BEGIN 子句中设置为数组作为数组。每行都在 for-cycle 中进行比较,如果每个八位字节都位于startIp和之间endIp,则会打印“匹配行”。



Some Python 3 gibberish relying on ipaddress module from 3.3 (available for 2.6/2.7:

一些 Python 3 胡言乱语依赖于 3.3 中的 ipaddress 模块(可用于 2.6/2.7:

$ python3 ipcheck.py 127.0.0.1 iplist.txt

Which is actually one-liner version for following script:

这实际上是以下脚本的单行版本:

awk -F- -v arg='2.158.1.2' 'function ipval(arg) {
   split(arg, arr, ".");
   s=0;
   for (i=1; i<=length(arr); i++)
      s += arr[i] * (10**(6-i));
   return s   
}
ipval(arg) >= ipval() && ipval(arg) <= ipval()' file
2.156.0.0-2.159.255.255-255.252.0.0

Which can be used like that:

可以这样使用:

function ipval()
{
  RES=`awk -F- -v arg="" '{
    split(arg, arr, ".");
    s=0;
    for (i=1; i<=length(arr); i++) { s += arr[i] * (256**(4-i)); }
    print s
  }' <<< '' `
  return $RES
}

read -p "IP:" IP
ipval "$IP"
echo "RES=$RES"

回答by anubhava

You can use this awk script:

你可以使用这个 awk 脚本:

##代码##

ipvalconverts given ip address to a numeric value so that it can be compared easily using arithmetical operator.

ipval将给定的 ip 地址转换为数值,以便可以使用算术运算符轻松进行比较。

回答by hvillemoes

I find this variant of the above script more useful:

我发现上述脚本的这个变体更有用:

##代码##