CIDR的转换说明在Linux外壳netmask2cdir和cdir2netmask网络掩码 [英] Explanation of convertor of cidr to netmask in linux shell netmask2cdir and cdir2netmask

查看:652
本文介绍了CIDR的转换说明在Linux外壳netmask2cdir和cdir2netmask网络掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现下面的shell函数从该话题

I found the following shell functions from this topic

mask2cdr ()
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x=${1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}
   echo $(( $2 + (${#x}/4) ))
}


cdr2mask ()
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift
   echo ${1-0}.${2-0}.${3-0}.${4-0}
}

您能详细解释如何将这些函数转换为CIDR子网掩码和为CIDR网络掩码?我无法理解code。

Could you explain in details how these functions convert cidr to netmask and the netmask to cidr? I can not understand the code.

推荐答案

要从这样一个点分十进制网络掩码得到CIDR preFIX:

mask2cdr()

To get the CIDR prefix from a dot-decimal netmask like this one:

255.255.192.0

你首先要转换的四个八位二进制再算上最显著位(也就是领头的人的数量):

you first have to convert the four octets to binary and then count the most significant bits (i.e. the number of leading ones):

11111111.11111111.11000000.00000000  # 18 ones = /18 in CIDR

这个功能确实,与其创造性。首先,我们去掉所有领先的 255 八位字节(即是二进制的所有的人的八位位组)和结果存储在变量 X

This function does that rather creatively. First, we strip off all of the leading 255 octets (i.e. the octets that are all ones in binary) and store the results in variable x:

local x=${1##*255.}

此步骤使用参数扩展,这对整个脚本依赖在pretty严重。如果我们将继续与我们的例子 255.255.192.0 的掩码,我们现在有以下值:

This step uses parameter expansion, which the entire script relies on pretty heavily. If we continue with our example netmask of 255.255.192.0, we now have the following values:

$1: 255.255.192.0
$x: 192.0

接下来我们设置了三个变量: $ 1 $ 2 $ 3 。这些被称为位置参数;他们就像普通的命名变量,但是当你将参数传递给脚本或者函数通常设置。我们可以直接使用组设定的值 - ,例如:

Next we set three variables: $1, $2, and $3. These are called positional parameters; they are much like ordinary named variables but are typically set when you pass arguments to a script or function. We can set the values directly using set --, for example:

set -- foo bar  # $1 = foo, $2 = bar

在使用定位参数命名的变量,因为它使得脚本更易于阅读和调试,但最终的结果是一样的我preFER。我们制定 $ 1 来:

0^^^128^192^224^240^248^252^254^

这是真的只是某些十进制值转换为二进制和计数 1 的位数的表。我们会回来这一点。

This is really just a table to convert certain decimal values to binary and count the number of 1 bits. We'll come back to this later.

我们将 $ 2

$(( (${#1} - ${#x})*2 ))

这看起来复杂,但它实际上只是计算我们的第一个命令扒掉 1 的位数。它打破了这样:

This looks complex, but it is really just counting the number of 1 bits we stripped off in the first command. It breaks down to this:

(number of chars in $1 - number of chars in $x) * 2

在我们的例子工程以

which in our case works out to

(13 - 5) * 2 = 16

我们剥去两个字节,所以我们得到16有道理。

We stripped off two octets so we get 16. Makes sense.

我们将 $ 3 来:

${x%%.*}

这是 $ X 与第一次之后的所有值。剥离关闭。在我们的例子,这是 192

which is the value of $x with everything after the first . stripped off. In our case, this is 192.

我们需要将这个数字转换为二进制和计数 1 在其位的数量,让我们回到我们的转换表。我们可以把表成每四个字符等于块:

We need to convert this number to binary and count the number of 1 bits in it, so let's go back to our "conversion table." We can divide the table into equal chunks of four characters each:

0^^^  128^  192^  224^  240^  248^  252^  254^

在二进制文件,上面的数字是:

In binary, the above numbers are:

00000000 10000000 11000000 11100000 11110000 11111000 11111100 11111110
# 0 ones 1 one    2 ones   3 ones   ...

如果我们从左边数,表中的每个四字块对应一个额外的 1 二进制位。我们正试图转换 192 ,所以让我们先砍掉了最右边的桌子,从 192 上,并将其存储在 X

If we count from the left, each four-character block in the table corresponds to an additional 1 bit in binary. We're trying to convert 192, so let's first lop off the rightmost part of the table, from 192 on, and store it in x:

x=${1%%$3*}

$ X 的值是现在

0^^^128^

其中包含二进二出四字块或两个 1 位。

现在我们只需要在我们领先的 255 八位位组(总共16个,存储在添加了 1 位变量 $ 2 ),并从previous步骤(2个) 1 位:

Now we just need to add up the 1 bits from our leading 255 octets (16 total, stored in variable $2) and the 1 bits from the previous step (2 total):

echo $(( $2 + (${#x}/4) ))

其中,

${#x}/4

是字符的 $ X 除以四在 $ X

18

cdr2mask()

让我们保持我们的previous例如,其中有一个CIDR preFIX 18 运行。

我们使用设置 - 到$ 9,设置位置参数$ 1:

We use set -- to set positional parameters $1 through $9:

$1: $(( 5 - ($1 / 8) ))  # 5 - (18 / 8) = 3 [integer math]
$2: 255
$3: 255
$4: 255
$5: 255
$6: $(( (255 << (8 - ($1 % 8))) & 255 ))  # (255 << (8 - (18 % 8))) & 255 = 192
$7: 0
$8: 0
$9: 0

让我们来看看用来设置 $ 1 $ 6个一点点接近的公式。 $ 1 设置为:

Let's examine the formulas used to set $1 and $6 a little closer. $1 is set to:

$(( 5 - ($1 / 8) ))

有一个CIDR preFIX最大和最小可能的值是32,用于网络掩码

The maximum and minimum possible values for a CIDR prefix are 32 for netmask

11111111.11111111.11111111.11111111

和0的网络掩码

00000000.00000000.00000000.00000000

上面的公式使用整数除法,所以可能的结果范围从1到5:

The above formula uses integer division, so the possible results range from 1 to 5:

5 - (32 / 8) = 1
5 - ( 0 / 8) = 5

$ 6个设置为:

$(( (255 << (8 - ($1 % 8))) & 255 ))

让我们打破这对于我们的例子CIDR $ P $的PFIX 18 。首先,我们把模量和做一些减法:

Let's break this down for our example CIDR prefix of 18. First we take the modulus and do some subtraction:

8 - (18 % 8) = 6

接下来,我们按位该值转移255:

Next we bitwise shift 255 by this value:

255 << 6

这是一样的推6 0 位上的二进制255结尾:

This is the same as pushing six 0 bits onto the end of 255 in binary:

11111111000000

最后,我们按位与这个值与255:

Finally, we bitwise AND this value with 255:

11111111000000 &
00000011111111  # 255

这使得

00000011000000

或只是

11000000

看起来很熟悉?这是我们在二进制掩码的第三个八位字节:

Look familiar? This is the third octet in our netmask in binary:

11111111.11111111.11000000.00000000
                  ^------^

在十进制值是192。

接下来我们转向基于价值的位置参数 $ 1

Next we shift the positional parameters based on the value of $1:

[ $1 -gt 1 ] && shift $1 || shift

在我们的例子中, $ 1的值 3,所以我们3移位位置参数左侧。的previous值 $ 4'/ code>成为新的价值 $ 1 的previous值 $ 5 变成 $ 2 的价值,等等:

In our case, the value of $1 is 3, so we shift the positional parameters 3 to the left. The previous value of $4 becomes the new value of $1, the previous value of $5 becomes the value of $2, and so on:

$1: 255
$2: 255
$3: 192
$4: 0
$5: 0
$6: 0

这些值应该很熟悉:他们是从我们的网络掩码十进制八位位组(一对夫妇在年底上涨了额外的零的)。要获得子网掩码,我们简单地打印出前四点与它们之间:

These values should look familiar: they are the decimal octets from our netmask (with a couple of extra zeros tacked on at the end). To get the netmask, we simply print out the first four with dots in between them:

echo ${1-0}.${2-0}.${3-0}.${4-0}

-0 之后的每个参数表示使用 0 为默认值,如果该参数未设置。

The -0 after each parameter says to use 0 as the default value if the parameter is not set.

255.255.192.0

这篇关于CIDR的转换说明在Linux外壳netmask2cdir和cdir2netmask网络掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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