Get external IP address of Router

  • Thread starter Thread starter Lorne Smith
  • Start date Start date
L

Lorne Smith

Hi,

I'm running a small network and need to get the external IP address of my
router so that I can update my DNS forwarding service should my IP change
(it's a dynamic one)... I can get the IP addresses of all the machines in
the network, and can get the internal IP address of the default gateway (the
router), but I need to get the external IP address...

Any pointers?

Thanks

Lorne
 
OK, I've solved my own problem... For anyone interested, here's what you
need to do...

First, add a reference to the NATUPnP 1.0 Type Library. Then paste the
following code into a class. When done, instantiate the class as you
normally would and call the GetExternalIP function. This works find and you
can expand on this functionality to iterate through each of the configured
ports on your router. The only limitation I've found with that though it
that Microsofts implementation of the uPnP library cannot handle port ranges
so any setup on your router like this will NOT be returned in the
StaticPortMappingCollection object... If anyone knows a workaround for
this, please post!!!

<Code Follows>

Imports NATUPNPLib
Public Class RouterInfo
Dim cRouter As NATUPNPLib.UPnPNAT

Public Sub New()
cRouter = New NATUPNPLib.UPnPNAT
End Sub

Public Function GetExternalIP() As String
Dim lMapper = cRouter.StaticPortMappingCollection
Dim lMappedPort As NATUPNPLib.IStaticPortMapping
Dim lExtIP As String

If Not lMapper Is Nothing Then
For Each lMappedPort In lMapper
lExtIP = lMappedPort.ExternalIPAddress().ToString
Exit For
Next
GetExternalIP = lExtIP
Else
GetExternalIP = "<Unable to resolve external IP>"
End If
End Function

End Class
 
Back
Top