Skip to content

Commit b0f4bde

Browse files
added nat routes
1 parent d440e93 commit b0f4bde

9 files changed

+310
-153
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ configs.example.ps1
77
/.vscode/*
88
!/.ssh/
99
/.ssh/*
10+
hyperv.ps1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ There are few options when building the Site2Site VPN lab:
115115

116116
<span style="background-color:Yellow;">**NOTE: If connected to Azure, hit tab for the virtualNetwork and Resourcegroup values to iterate through existing Azure resources. </span>
117117
```powershell
118-
& '.\Step 3C. Attach Azure Basic S2S to Existing.ps1' -Prefix contoso -ResourceGroup mecmcb-arm-rg -VirtualNetwork contoso-vnet
118+
& '.\Step 3C. Attach Azure Basic S2S to Existing.ps1' -Prefix contoso -ResourceGroup mecmcb-arm-rg -VirtualNetwork contoso-vnet -DNSIP 10.0.0.4 -RemovePublicIps -Force
119119
```
120120

121121
<span style="background-color:Red;">**IMPORTANT**: All scripts list above can be ran multiple times! If ran a second time, it will check all configurations and attempt to repair and issues. this can be useful when public IP has changed on home network</span>

Step 2. Setup Vyos Router in Lab.ps1

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#>
1919
#Requires -RunAsAdministrator
2020

21+
Param(
22+
[switch]$SkipInitialSetup
23+
)
24+
2125
#https://systemspecialist.net/2014/11/26/create-mini-router-with-hyper-v-for-vm-labs/
2226
#region Grab Configurations
2327
If($PSScriptRoot.ToString().length -eq 0)
@@ -89,27 +93,28 @@ Else{
8993
}
9094
#endregion
9195

92-
#Trunk HyperV Network for internal networks; determine if VLAN needs to be used.
93-
#https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vmnetworkadaptervlan?view=windowsserver2019-ps
94-
If($HyperVConfig.ConfigureForVLAN)
95-
{
96-
Get-VMNetworkAdapter -VMName $VyOSConfig.VMName | Where-Object {$_.SwitchName -ne $VmSwitchExternal} |
97-
Set-VMNetworkAdapterVlan -Trunk -NativeVlanId $HyperVConfig.VLANID -AllowedVlanIdList $VyOSConfig.AllowedvLanIdRange
98-
}
99-
Else{
100-
Get-VMNetworkAdapter -VMName $VyOSConfig.VMName | Where-Object {$_.SwitchName -ne $VmSwitchExternal} |
101-
Set-VMNetworkAdapterVlan -Untagged
102-
}
96+
If(!$SkipInitialSetup){
97+
#Trunk HyperV Network for internal networks; determine if VLAN needs to be used.
98+
#https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vmnetworkadaptervlan?view=windowsserver2019-ps
99+
If($HyperVConfig.ConfigureForVLAN)
100+
{
101+
Get-VMNetworkAdapter -VMName $VyOSConfig.VMName | Where-Object {$_.SwitchName -ne $VmSwitchExternal} |
102+
Set-VMNetworkAdapterVlan -Trunk -NativeVlanId $HyperVConfig.VLANID -AllowedVlanIdList $VyOSConfig.AllowedvLanIdRange
103+
}
104+
Else{
105+
Get-VMNetworkAdapter -VMName $VyOSConfig.VMName | Where-Object {$_.SwitchName -ne $VmSwitchExternal} |
106+
Set-VMNetworkAdapterVlan -Untagged
107+
}
103108

104109

105-
#start VM
106-
Write-Host "Configuring router for initial settings..." -ForegroundColor Yellow
107-
If($VM.State -ne "Running"){Start-VM -Name $VyOSConfig.VMName -ErrorAction Stop
108-
Start-Sleep 45
109-
}
110+
#start VM
111+
Write-Host "Configuring router for initial settings..." -ForegroundColor Yellow
112+
If($VM.State -ne "Running"){Start-VM -Name $VyOSConfig.VMName -ErrorAction Stop
113+
Start-Sleep 45
114+
}
110115

111-
#region INSTALL VyOS
112-
$VyOSSteps = @"
116+
#region INSTALL VyOS
117+
$VyOSSteps = @"
113118
`n
114119
Installing an image onto the virtual router
115120
Connect to router and answer the questions below:
@@ -129,22 +134,22 @@ Connect to router and answer the questions below:
129134
Which drive should GRUB modify the boot partition on? [sda]: [Enter]
130135
"@
131136

132-
do {
133-
#cls
134-
Write-Host $VyOSSteps -ForegroundColor Gray
135-
Write-Host "`nNOTE: To get out of console, hit [CTRL+ALT+LEFT ARROW]" -ForegroundColor Yellow
136-
$response1 = Read-host "Did you complete the steps above? [Y or N]"
137-
} until ($response1 -eq 'Y')
138-
139-
Write-Host "`nConfiguring router for next configurations..." -ForegroundColor Yellow
140-
Stop-VM $VyOSConfig.VMName -ErrorAction SilentlyContinue
141-
Get-VMDvdDrive -VMName $VyOSConfig.VMName | Remove-VMDvdDrive
142-
Start-VM -Name $VyOSConfig.VMName -ErrorAction SilentlyContinue
143-
Start-Sleep 45
144-
#endregion
137+
do {
138+
#cls
139+
Write-Host $VyOSSteps -ForegroundColor Gray
140+
Write-Host "`nNOTE: To get out of console, hit [CTRL+ALT+LEFT ARROW]" -ForegroundColor Yellow
141+
$response1 = Read-host "Did you complete the steps above? [Y or N]"
142+
} until ($response1 -eq 'Y')
143+
144+
Write-Host "`nConfiguring router for next configurations..." -ForegroundColor Yellow
145+
Stop-VM $VyOSConfig.VMName -ErrorAction SilentlyContinue
146+
Get-VMDvdDrive -VMName $VyOSConfig.VMName | Remove-VMDvdDrive
147+
Start-VM -Name $VyOSConfig.VMName -ErrorAction SilentlyContinue
148+
Start-Sleep 45
149+
#endregion
145150

146-
#region Setup VyOS SSH
147-
$VyOSSteps = @"
151+
#region Setup VyOS SSH
152+
$VyOSSteps = @"
148153
`n
149154
Enabling network and SSH on the virtual router
150155
Connect to router and answer the questions below:
@@ -160,17 +165,22 @@ Connect to router and answer the questions below:
160165
vyos@vyos:~$ show int
161166
"@
162167

163-
do {
164-
#cls
165-
Write-Host $VyOSSteps -ForegroundColor Gray
166-
Write-Host "`nMake sure there is an IP address for interface eth0" -ForegroundColor Yellow
167-
Write-Host "TAKE NOTE OF IP" -BackgroundColor Yellow -ForegroundColor Black
168-
$response1 = Read-host "Did you complete the steps above? [Y or N]"
169-
} until ($response1 -eq 'Y')
170-
Write-Host "If steps completed successfully, You can now ssh into the router instead of connecting VM console" -ForegroundColor Yellow
171-
172-
#endregion
168+
do {
169+
#cls
170+
Write-Host $VyOSSteps -ForegroundColor Gray
171+
Write-Host "`nMake sure there is an IP address for interface eth0" -ForegroundColor Yellow
172+
Write-Host "TAKE NOTE OF IP" -BackgroundColor Yellow -ForegroundColor Black
173+
$response1 = Read-host "Did you complete the steps above? [Y or N]"
174+
} until ($response1 -eq 'Y')
175+
Write-Host "If steps completed successfully, You can now ssh into the router instead of connecting VM console" -ForegroundColor White
173176

177+
#endregion
178+
}
179+
Else{
180+
Write-Host "Check IP by running command in router [" -ForegroundColor Red -NoNewline
181+
Write-Host "show int" -ForegroundColor Yellow -NoNewline
182+
Write-Host "]" -ForegroundColor Red
183+
}
174184
#region Prompt for external interface for router
175185
do {
176186
If(Test-Path "$env:temp\VyOSextip.txt"){
@@ -404,15 +414,17 @@ Else{
404414

405415
If($RunManualSteps){
406416
#region Copy Paste Mode
407-
Write-Host "`nOpen ssh session for $($VyOSConfig.VMName):`n" -ForegroundColor Yellow
408-
Write-Host "Copy script below line or from $PSScriptRoot\Logs\$ScriptName" -ForegroundColor Yellow
409417
Write-Host "--------------------------------------------------------" -ForegroundColor Yellow
410-
Write-Host $VyOSLanCmd -ForegroundColor Gray
418+
Write-Host $VyOSLanCmd -ForegroundColor Gray
411419
Write-Host "--------------------------------------------------------" -ForegroundColor Yellow
412-
Write-Host "Stop copying above line this and paste in ssh session" -ForegroundColor Yellow
420+
Write-Host "`nOpen ssh session for $($VyOSConfig.VMName) by running command [" -ForegroundColor White -NoNewline
421+
Write-Host ("ssh vyos@{0}" -f $VyOSExternalIP) -ForegroundColor Yellow -NoNewline
422+
Write-Host "]" -ForegroundColor White
423+
Write-Host "Then copy the script between the lines or `n from $PSScriptRoot\Logs\$ScriptName" -ForegroundColor White
413424
Write-Host "`nA reboot may be required on $($VyOSConfig.VMName) for updates to take effect" -ForegroundColor Red
414-
Write-Host "Run this command last in ssh session: " -ForegroundColor Gray -NoNewline
415-
Write-Host "reboot now" -ForegroundColor Yellow
425+
Write-Host "In router's ssh session, run command [" -ForegroundColor Gray -NoNewline
426+
Write-Host "reboot now" -ForegroundColor Yellow -NoNewline
427+
Write-Host "] to reboot" -ForegroundColor Gray
416428
#endregion
417429
}
418430

Step 3A. Build Azure Basic S2S.ps1

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ $LogfileName = "$RegionName-AzureSimpleS2S-$(Get-Date -Format 'yyyy-MM-dd_Thh-mm
2121
Try{Start-transcript "$PSScriptRoot\Logs\$LogfileName" -ErrorAction Stop}catch{Start-Transcript "$PSScriptRoot\$LogfileName"}
2222
#endregion
2323

24+
#Make it a global variable so it used for the entire session
25+
#TEST $Global:BasicPssKey='bB8u6Tj60uJL2RKYR0OCyiGMdds9gaEUs9Q2d3bRTTVRKJ516CCc1LeSMChAI0rc'
26+
If(!$Global:BasicPssKey){$Global:BasicPssKey = New-SharedPSKey}
27+
2428
#grab external interface for VyOS router
2529
If($null -ne $VyOSConfig.ExternalInterfaceIP){
2630
$VyOSExternalIP = $VyOSConfig.ExternalInterfaceIP
@@ -255,7 +259,7 @@ Elseif( $null -eq $currentGwConnection)
255259
#Create the connection
256260
New-AzVirtualNetworkGatewayConnection -Name $AzureSimpleConfig.ConnectionName -ResourceGroupName $AzureSimpleConfig.ResourceGroupName `
257261
-Location $AzureSimpleConfig.LocationName -VirtualNetworkGateway1 $gateway1 -LocalNetworkGateway2 $Local `
258-
-ConnectionType IPsec -RoutingWeight 10 -SharedKey $sharedPSKKey -Force | Out-Null
262+
-ConnectionType IPsec -RoutingWeight 10 -SharedKey $Global:BasicPssKey -Force | Out-Null
259263
Write-Host "Done" -ForegroundColor Green
260264
}
261265
Catch{
@@ -274,7 +278,7 @@ Else{
274278
If( ($response1 -eq 'Y') -or ($VyOSConfig['ResetVPNConfigs'] -eq $true) )
275279
{
276280
Write-Host ("Attempting to update vyos router vpn configurations to use Azure's public IP [{0}]..." -f $azpip.IpAddress) -ForegroundColor Yellow
277-
$Global:sharedPSKKey = Get-AzVirtualNetworkGatewayConnectionSharedKey -Name $AzureSimpleConfig.ConnectionName -ResourceGroupName $AzureSimpleConfig.ResourceGroupName
281+
$Global:BasicPssKey = Get-AzVirtualNetworkGatewayConnectionSharedKey -Name $AzureSimpleConfig.ConnectionName -ResourceGroupName $AzureSimpleConfig.ResourceGroupName
278282
$VyOSConfig['ResetVPNConfigs'] = $true
279283
}
280284
Else{
@@ -324,7 +328,7 @@ set vpn ipsec ike-group azure-ike proposal 1 hash 'sha1'
324328
set vpn ipsec ipsec-interfaces interface 'eth0'
325329
set vpn ipsec nat-traversal 'enable'
326330
set vpn ipsec site-to-site peer $($azpip.IpAddress) authentication mode 'pre-shared-secret'
327-
set vpn ipsec site-to-site peer $($azpip.IpAddress) authentication pre-shared-secret '$Global:sharedPSKKey'
331+
set vpn ipsec site-to-site peer $($azpip.IpAddress) authentication pre-shared-secret '$($Global:BasicPssKey)'
328332
set vpn ipsec site-to-site peer $($azpip.IpAddress) connection-type 'initiate'
329333
set vpn ipsec site-to-site peer $($azpip.IpAddress) default-esp-group 'azure'
330334
set vpn ipsec site-to-site peer $($azpip.IpAddress) description '$($AzureSimpleConfig.TunnelDescription)'
@@ -340,20 +344,32 @@ set vpn ipsec site-to-site peer $($azpip.IpAddress) tunnel 1 remote prefix '$($A
340344
set protocols static route 0.0.0.0/0 next-hop '$($VyOSConfig.NextHopSubnet)'
341345
"@
342346

343-
If($VyOSConfig.ResetVPNConfigs){
347+
foreach ($SubnetCIDR in $VyOSConfig.LocalSubnetPrefix.GetEnumerator() | Sort Name){
344348
$VyOSFinal += @"
345349
`n
346-
reset vpn ipsec-peer $($azpip.IpAddress) tunnel 1
350+
set protocols static route '$($SubnetCIDR.Name)' next-hop '$($azpip.IpAddress)'
347351
"@
348352
}
349353

350-
foreach ($SubnetCIDR in $VyOSConfig.LocalSubnetPrefix.GetEnumerator() | Sort Name){
354+
foreach ($vNetPrefix in $vNet.AddressSpace.AddressPrefixes){
355+
#use the last octet of network id as the rule id (keeps it unique)
356+
$RuleID = ((Get-NetworkDetails -CidrAddress $vNetPrefix).NetworkID -replace '\.0','').split('.')[-1]
357+
If( ($RuleID -eq 10) -or ($RuleID -eq 100) ){$RuleID++}
351358
$VyOSFinal += @"
352359
`n
353-
set protocols static route '$($SubnetCIDR.Name)' next-hop '$($azpip.IpAddress)'
360+
set nat source rule $($RuleID) destination address '$($vNetPrefix)'
361+
set nat source rule $($RuleID) exclude
362+
set nat source rule $($RuleID) outbound-interface 'eth0'
363+
set nat source rule $($RuleID) source address '$($VyOSConfig.LocalCIDRPrefix)'
354364
"@
355365
}
356366

367+
If($VyOSConfig.ResetVPNConfigs){
368+
$VyOSFinal += @"
369+
`n
370+
reset vpn ipsec-peer $($azpip.IpAddress) tunnel 1
371+
"@
372+
}
357373

358374
$VyOSFinal += @"
359375
@@ -453,7 +469,7 @@ public class TrustAllCertsPolicy : ICertificatePolicy {
453469
$response2 = Read-host "Would you like to attempt to reset the VPN connection? [Y or N]"
454470
If($response2 -eq 'Y'){
455471
Set-AzVirtualNetworkGatewayConnectionSharedKey -Name $AzureSimpleConfig.ConnectionName `
456-
-ResourceGroupName $AzureSimpleConfig.ResourceGroupName -Value $Global:sharedPSKKey -Force | Out-Null
472+
-ResourceGroupName $AzureSimpleConfig.ResourceGroupName -Value $Global:BasicPssKey -Force | Out-Null
457473

458474
Reset-AzVirtualNetworkGatewayConnection -Name $AzureSimpleConfig.ConnectionName `
459475
-ResourceGroupName $AzureSimpleConfig.ResourceGroupName -Force | Out-Null
@@ -485,22 +501,23 @@ If($RunManualSteps)
485501
Write-Host ("Azure Location: {0}" -f $AzureSimpleConfig.LocationName)
486502
Write-Host ("Azure Public IP: {0}" -f $azpip.IpAddress)
487503
Write-Host ("Azure Subnet Prefix: {0}" -f $AzureSimpleConfig.VnetSubnetPrefix)
488-
Write-host ("Shared Key (PSK): {0}" -f $Global:sharedPSKKey)
504+
Write-host ("Shared Key (PSK): {0}" -f $Global:BasicPssKey)
489505
Write-host ("Home Public IP: {0}" -f $HomePublicIP)
490506
Write-Host ("Router CIDR Prefix: {0}" -f $VyOSConfig.LocalCIDRPrefix)
491-
Write-Host "Be sure to follow a the configuration file: '$PSScriptRoot\Logs\$ScriptName'`n" -ForegroundColor Yellow
492507

493508
#region Copy Paste Mode
494-
Write-Host "`nOpen ssh session for $($VyOSConfig.VMName):`n" -ForegroundColor Yellow
495-
Write-Host "Copy script below line or from $PSScriptRoot\Logs\$ScriptName" -ForegroundColor Yellow
496509
Write-Host "--------------------------------------------------------" -ForegroundColor Yellow
497510
Write-Host $VyOSFinal -ForegroundColor Gray
498511
Write-Host "--------------------------------------------------------" -ForegroundColor Yellow
499-
Write-Host "Stop copying above line this and paste in ssh session" -ForegroundColor Yellow
512+
Write-Host "`nOpen ssh session for $($VyOSConfig.VMName) by running command [" -ForegroundColor White -NoNewline
513+
Write-Host ("ssh vyos@{0}" -f $VyOSExternalIP) -ForegroundColor Yellow -NoNewline
514+
Write-Host "]" -ForegroundColor White
515+
Write-Host "Then copy the script between the lines or `n from $PSScriptRoot\Logs\$ScriptName" -ForegroundColor White
500516
Write-Host "`nA reboot may be required on $($VyOSConfig.VMName) for updates to take effect" -ForegroundColor Red
501-
Write-Host "Log into router and run [" -ForegroundColor Gray -NoNewline
517+
Write-Host "In router's ssh session, run command [" -ForegroundColor Gray -NoNewline
502518
Write-Host "reboot now" -ForegroundColor Yellow -NoNewline
503-
Write-Host "]" -ForegroundColor Gray
519+
Write-Host "] to reboot" -ForegroundColor Gray
520+
#endregion
504521
}
505522

506523
Stop-Transcript

0 commit comments

Comments
 (0)