连接到其他网络的 Azure 基础结构服务中 (VNet) 的虚拟网络必须具有网关子网。 定义网关子网的最佳做法是:
网关子网的前缀长度最大可以为 29 (例如 10.119.255.248/29) ,但当前建议使用前缀长度 27 (例如 10.119.255.224/27) 。
定义网关子网的地址空间时,请使用 VNet 地址空间的最后一部分。
对于第二个建议,可以通过将网关子网使用的位设置为 0,将 VNet 地址空间中的剩余位设置为 1 来确定网关子网的地址空间。 若要快速计算网关子网地址空间,而无需转换为二进制并重新转换为十进制,可以使用用 C# 或 Python 编写的控制台应用程序,或者使用 PowerShell 命令块。
本文包含 C#、Python 和 PowerShell 代码块,这些代码块根据 VNet 地址前缀和网关子网前缀长度的 w.x.y.z/n 值计算网关子网地址空间。
C# 代码块
使用此代码块在 C# 中创建控制台应用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
class Program
static void Main(string[] args)
// Declare variables.
const long wOctet = 16777216;
const long xOctet = 65536;
const long yOctet = 256;
double D;
int w, x, y, z, vnetPrefLen, gwPrefLen;
long d, w2, x2, y2, z2;
// Get the five values needed from the keyboard.
Console.WriteLine("**************************************************************************");
Console.WriteLine("*** Gateway subnet address space calculator for Azure virtual networks ***");
Console.WriteLine("**************************************************************************");
Console.WriteLine();
Console.WriteLine("Please supply your virtual network address space in the form of w.x.y.z/n.");
Console.WriteLine();
Console.WriteLine("w=");
w = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("x=");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("y=");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("z=");
z = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("n=");
vnetPrefLen = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("Now supply the prefix length of your gateway subnet:");
gwPrefLen = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// Perform the calculation.
D = w * wOctet + x * xOctet + y * yOctet + z;
for (int i = vnetPrefLen + 1; i < gwPrefLen + 1; i++)
D = D + Math.Pow(2, 32 - i);
d = Convert.ToInt64(D);
w2 = d / wOctet;
x2 = (d - w2 * wOctet) / xOctet;
y2 = (d - w2 * wOctet - x2 * xOctet) / yOctet;
z2 = d - w2 * wOctet - x2 * xOctet - y2 * yOctet;
// Display the result.
Console.WriteLine("**************************************************************************");
Console.WriteLine("For the Azure virtual address space {0}.{1}.{2}.{3}/{4}", w, x, y, z, vnetPrefLen);
Console.WriteLine("and gateway prefix length of {0}, the gateway subnet address space is:", gwPrefLen);
Console.WriteLine();
Console.WriteLine("{0}.{1}.{2}.{3}/{4}", w2, x2, y2, z2, gwPrefLen);
Console.WriteLine();
Console.WriteLine("Press ENTER to quit.");
Console.ReadLine();
Python 代码块
使用此代码块在 Python 中创建控制台应用。
import math
# Collect the values of w.x.y.z/n for your VNet address space and g, the prefix length of your gateway subnet
print("**************************************************************************")
print("*** Gateway subnet address space calculator for Azure virtual networks ***")
print("**************************************************************************\n")
print("Please supply your virtual network address space in the form of w.x.y.z/n.");
w=int(input("w = "))
x=int(input("x = "))
y=int(input("y = "))
z=int(input("z = "))
n=int(input("n = "))
print("")
g=int(input("Now supply the prefix length of your gateway subnet: "))
print("")
# Calculate
wOctet = 16777216
xOctet = 65536
yOctet = 256
D = w * wOctet + x * xOctet + y * yOctet + z
for index in range(n + 1,g + 1):
D += 2**(32 - index)
w2 = math.floor(D / wOctet)
x2 = math.floor((D - w2 * wOctet) / xOctet)
y2 = math.floor((D - w2 * wOctet - x2 * xOctet) / yOctet)
z2 = D - w2 * wOctet - x2 * xOctet - y2 * yOctet
# Display the result
gwAddrPref= "Your gateway address prefix is: " + str(w2) + "." + str(x2) + "." + str(y2) + "." + str(z2) + "/" + str(g)
print(gwAddrPref)
PowerShell 命令块
填写值,并在 PowerShell 窗口或 PowerShell 集成脚本环境 (ISE) 中运行生成的命令块。
# Specify the values of w.x.y.z/n for your VNet address space and g, the prefix length of your gateway subnet:
# Calculate
$wOctet = 16777216
$xOctet = 65536
$yOctet = 256
[long]$D = $w * $wOctet + $x * $xOctet + $y * $yOctet + $z;
for ($i = $n + 1; $i -lt $g + 1; $i++)
$D = $D + [math]::pow(2, 32 - $i)
$w2 = [math]::floor($D / $wOctet)
$x2 = [math]::floor( ($D - $w2 * $wOctet) / $xOctet )
$y2 = [math]::floor( ($D - $w2 * $wOctet - $x2 * $xOctet) / $yOctet )
$z2 = $D - $w2 * $wOctet - $x2 * $xOctet - $y2 * $yOctet
# Display the result
$dx= [string]$w2 + "." + [string]$x2 + "." + [string]$y2 + "." + [string]$z2 + "/" + [string]$g
Write-Host "Your gateway address prefix is: " $dx
使用 PowerShell 管理 Microsoft 365