Uwaga
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Sieć wirtualna (VNet) w usługach infrastruktury platformy Azure połączonych z innymi sieciami musi mieć podsieć bramy. Najlepsze rozwiązania dotyczące definiowania podsieci bramy to:
- Długość prefiksu podsieci bramy może mieć maksymalną długość prefiksu 29 (na przykład 10.119.255.248/29), ale bieżącą rekomendacją jest użycie prefiksu o długości 27 (na przykład 10.119.255.224/27).
- Podczas definiowania przestrzeni adresowej podsieci bramy użyj ostatniej części przestrzeni adresowej sieci wirtualnej.
W przypadku drugiego zalecenia możesz określić przestrzeń adresów podsieci bramy, ustawiając bity używane dla podsieci bramy na 0, a pozostałe bity w przestrzeni adresowej sieci wirtualnej na 1. Aby szybko obliczyć przestrzeń adresową podsieci bramy bez konieczności konwertowania na dane binarne i z powrotem na dziesiętne, możesz użyć aplikacji konsolowej napisanej w języku C# lub Python albo z blokiem poleceń programu PowerShell.
Ten artykuł zawiera bloki kodu C#, Python i PowerShell, które obliczają przestrzeń adresową podsieci bramy na podstawie wartości w.x.y.z/n prefiksu adresu sieci wirtualnej i długości prefiksu podsieci bramy.
Blok kodu w języku C#
Użyj tego bloku kodu, aby utworzyć aplikację konsolową w języku 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();
}
}
}
Blok kodu języka Python
Użyj tego bloku kodu, aby utworzyć aplikację konsolową w języku 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)
Blok poleceń programu PowerShell
Wypełnij wartości i uruchom wynikowy blok poleceń w oknie programu PowerShell lub w zintegrowanym środowisku skryptów programu 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:
$w=
$x=
$y=
$z=
$n=
$g=
# 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
Tematy pokrewne
Zarządzanie platformą Microsoft 365 za pomocą programu PowerShell