Объединение двух сетей с помощью IP Tunnel

Форум для общения пользователей MikroPlant
Ответить
AlexeyU
Сообщения: 13
Зарегистрирован: 14 май 2018, 16:18

01 мар 2019, 16:37

В первой строчке указать путь к модулю. Версия 1.0.0.3

Код: Выделить всё

Import-Module MikrotikPowerShell.dll
$confirm = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$confirm.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$confirm.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))

Write-Host "First device settings:"
$firstHost = Read-Host '  Host (access IP address)'
$firstUsername = Read-Host "  Username (e.g. admin)"

$firstPassword = Read-Host '  Password'
$firstIPTunnelLocalAddress = Read-Host '  External IP address for tunnel'

Write-Host ""
Write-Host "Second device settings:"
$secondHost = Read-Host '  Host (access IP address)'

$decision = $Host.UI.PromptForChoice("", "Are the credentials of the secondary device the same? (Y/N)?", $confirm, 1)

if($decision -eq 0) {
    $secondUsername = $firstUsername
    $secondPassword = $firstPassword
}
else {
    $secondUsername = Read-Host '  Username (e.g. admin)'
    $secondPassword = Read-Host '  Password'
}
$secondIPTunnelLocalAddress = Read-Host '  External IP address for tunnel'

$firstIPTunnelRemoteAddress = $secondIPTunnelLocalAddress
$secondIPTunnelRemoteAddress = $firstIPTunnelLocalAddress

Write-Host ""
Write-Host "Shared settings"
$localNetwork = Read-Host "  Local network (e.g. 10.0.0.0/30)"

$subnetMask = $localNetwork.Split('/')[1]
$localNetworkAddresses = Get-IPs -Subnet $localNetwork -GetFirst 2

$firstIPTunnelNetworkAddress = $localNetworkAddresses[0].ToString() + "/" + $subnetMask
$secondIPTunnelNetworkAddress = $localNetworkAddresses[1].ToString() + "/" + $subnetMask

Write-Host ""
Write-Host "Check settings"
Write-Host "--------------"
Write-Host "First device:"
Write-Host "  IP tunnel local address: " $firstIPTunnelLocalAddress
Write-Host "  IP tunnel remote address: " $firstIPTunnelRemoteAddress
Write-Host "  IP address of new interface: " $firstIPTunnelNetworkAddress

Write-Host ""
Write-Host "Second device:"
Write-Host "  IP tunnel local address: " $secondIPTunnelLocalAddress
Write-Host "  IP tunnel remote address: " $secondIPTunnelRemoteAddress
Write-Host "  IP address of new interface: " $secondIPTunnelNetworkAddress

$decision = $Host.UI.PromptForChoice("", "Check settings, continue? (Y/N)?", $confirm, 1)

if($decision -eq 0) {
    $firstInterface = Get-Mikrotik -Host "$firstHost" -Username "$firstUsername" -Password "$firstPassword" -Command "/interface/ipip/add" -Parameters @{"local-address"="$firstIPTunnelLocalAddress";"remote-address"="$firstIPTunnelRemoteAddress"} | Get-Mikrotik -Command "/interface/ipip/print" -Query @{".id"={$_.'ret'}}
    $firstInterfaceName = $firstInterface."name"
    Get-Mikrotik -Command "/ip/address/add" -Parameters @{"address"="$firstIPTunnelNetworkAddress";"interface"="$firstInterfaceName"}

    $secondInterface = Get-Mikrotik -Host "$secondHost" -Username "$secondUsername" -Password "$secondPassword" -Command "/interface/ipip/add" -Parameters @{"local-address"="$secondIPTunnelLocalAddress";"remote-address"="$secondIPTunnelRemoteAddress"} | Get-Mikrotik -Command "/interface/ipip/print" -Query @{".id"={$_.'ret'}}
    $secondInterfaceName = $secondInterface."name"
    Get-Mikrotik -Command "/ip/address/add" -Parameters @{"address"="$secondIPTunnelNetworkAddress";"interface"="$secondInterfaceName"}
}

Как это работает:

Запрашиваются локальные IP для подключения, логин/пароль, внешние IP для которых создавать туннель, подсеть между созданными интерфейсами. Создаются IPTunnel-интерфейсы, добавляются IP-адреса на эти интерфейсы.


Ответить