Notice: This post does not contain any pictures. They were all lost during the import from my old Gonjer.com site. I do apologize for that but I hope that the post will still help.
I needed a tool, preferably a Powershell script, which could show my WAN IP address using alternate outgoing TCP ports, other then default http/s (80 & 443) when using websites like whatismyip.com.
The reason for this is that I use multiple WAN/VPN connections based on protocol for different purposes like web surfing uses one WAN/VPN connection and all other protocols uses the default WAN gateway.
I use pFsense Firewall in my home lab and create firewall rules with an advanced settings and choose a Gateway other than the default like the example below:
Picture: pFsense Firewall Advanced Rules
For the tool, script or function I needed a site or service which responds to all different protocols. The one i found that were free, simple and used most ports in the TCP range, 1-65535, was portquiz.net.
The decision wasn’t hard, create a Powershell function that leverage the cmdlet Invoke-WebRequest to parse the content of the site and match against keywords using regular expressions.
This is the result:
DISCLAIMER: This script is published on “As Is” basis. I will not take any responsibility for any damage this script might do to your production or test server environment. Please ensure that you test this properly in a non-production environment before running or scheduling the script in a production environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
function Get-WANAddress { <# .SYNOPSIS Get the current WAN address with a corresponding TCP port. .DESCRIPTION Get the current WAN address with a corresponding TCP port, default 80. The Port parameter takes an array or from pipeline as long as the input is a [int[]]. .EXAMPLE Get-WANAddress WAN Address Port Protocol ----------- ---- -------- 43.217.27.156 80 http .EXAMPLE Get-WANAddress -Port 23,80,465,27015 WAN Address Port Protocol ----------- ------ -------- 91.259.127.72 23 telnet 43.217.27.156 80 http 91.259.127.72 465 ssmtp 91.259.127.72 27015 unknown .EXAMPLE 23,80,465,27015,21,22,443,445 | Get-WANAddress WAN Address Port Protocol ----------- ------ -------- 91.259.127.72 23 telnet 46.227.67.156 80 http 91.259.127.72 465 ssmtp 91.259.127.72 27015 unknown WARNING: The query site 'Portquiz.net' does not support port '21' when invoking web requests. WARNING: The query site 'Portquiz.net' does not support port '22' when invoking web requests. WARNING: The query site 'Portquiz.net' does not support port '443' when invoking web requests. WARNING: The query site 'Portquiz.net' does not support port '445' when invoking web requests. .NOTES Created on: 2016-09-01 17:54 Created by: Philip Haglund Organization: Omnicit AB Filename: Get-WANAddress.ps1 Version: 1.0 Requirements: Powershell 3.0 Changelog: 2016-09-01 17:54 - Creation of script. .LINK https://omnicit.se http://www.portquiz.net #> [CmdletBinding()] param ( # Enter a TCP port you want to use. [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0 )] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [ValidateRange(1, 65535)] [int[]]$Port = 80 ) begin { Write-Verbose -Message "Starting cmdlet $($MyInvocation.MyCommand.Name)" if (Get-Command -Name 'Invoke-WebRequest' -ErrorAction SilentlyContinue) { Write-Warning -Message "Unable to find command 'Invoke-WebRequest'." return } # Regular expressions to match the web request. [regex]$PortRegex = '(?:Network service:\s)(?<PortName>.*)(?:\<)' [regex]$AddressRegex = '(?:Your outgoing IP:\s)(?<Address>.*)(?:\<)' [regex]$PassiveRegex = '(?:.*\n)' } process { foreach ($InlinePort in $Port) { # Ports on 'Portquiz.net' that are blocked or not usable with web requests. if ($InlinePort -match '21|22|443|445') { Write-Warning -Message "The query site 'Portquiz.net' does not support port '$($InlinePort)' when invoking web requests." continue } try { $web = Invoke-WebRequest -Uri "http://portquiz.net:$($InlinePort)" -ErrorAction Stop } catch { Write-Warning -Message "Error with web request for port $InlinePort - $($_.Exception.Message)" continue } if ($web -match "$($PortRegex)$($PassiveRegex)$($AddressRegex)") { [PSCustomObject]@{ 'WAN Address' = [string]"$([string]$Matches['Address'])" 'Port ' = [string]$($InlinePort) 'Protocol' = [string]"$([string]$Matches['PortName'])" } } else { Write-Warning -Message 'The regex query against the web request is not matching the pattern. Update the regular expression in the script.' } } } end { Write-Verbose -Message "Finished running cmdlet $($MyInvocation.MyCommand.Name)" } } |