# VBForums CodeBank > CodeBank - Visual Basic .NET >  VB.Net Simple function to get external IP-adress

## BlindSniper

This is The Code


```
Imports System.Net
Imports System.Text.RegularExpressions

 Function GetExternalIP() As IPAddress
        Dim lol As WebClient = New WebClient()
        Dim str As String = lol.DownloadString("http://www.ip-adress.com/")
        Dim pattern As String = "<h2>My IP address is: (.+)</h2>"
        Dim matches1 As MatchCollection = Regex.Matches(str, pattern)
        Dim ip As String = matches1(0).ToString
        ip = ip.Remove(0, 21)
        ip = ip.Replace("</h2>", "")
        ip = ip.Replace(" ", "")
        Return IPAddress.Parse(ip)
    End Function
```

This is How to use it



```
 Dim MyIP As IPAddress = GetExternalIP()
```

This code is an easy method to get your Ip-address. I've read that sites disclaimer and all the fine print and nothing said I'm not allowed to parse it's html code, although I'm not sure if their is a daily limit or not

This is my first code bank submission and I hope that it will help someone out
I did a search and didn't find something similar

----------


## J-Deezy

A similar way but utilizing Regular Expressions built in features:


vb Code:
Private Function GetMyIP() As IPAddress        Dim outputIP As IPAddress        Using wClient As New WebClient            Dim myIP As String = Regex.Match(wClient.DownloadString("http://www.ip-adress.com/"), "(?<=<h2>My IP address is: )[0-9.]*?(?=</h2>)", RegexOptions.Compiled).Value            outputIP = IPAddress.Parse(myIP)        End Using        Return outputIP    End Function

Or if you want to get ugly


vb Code:
Private Function GetMyIP() As IPAddress        Using wClient As New WebClient            Return IPAddress.Parse(Regex.Match(wClient.DownloadString("http://www.ip-adress.com/"), "(?<=<h2>My IP address is: )[0-9.]*?(?=</h2>)", RegexOptions.Compiled).Value)        End Using    End Function

----------


## J-Deezy

Sorry for the double post but I felt I should share this, as it's a heck of a lot easier. Change your Imports to just System.Text and try the following.


vb Code:
Private Function GetMyIP() As Net.IPAddress        Using wc As New Net.WebClient            Return Net.IPAddress.Parse(Encoding.ASCII.GetString(wc.DownloadData("http://whatismyip.com/automation/n09230945.asp")))        End Using    End Function

That returns your ip with no string formatting necessary.

----------


## Lightning

Since the last code doens't work anymore and people might still want to use it, I have an alternative but simular solution:


```
Private Function GetMyIP() As Net.IPAddress
	Using wc As New Net.WebClient
	Return Net.IPAddress.Parse(Encoding.ASCII.GetString(wc.DownloadData("http://tools.feron.it/php/ip.php")))
	End Using
End Function
```

----------


## a_ahmed

This is very simple, clever and cool, thanks for sharing!

----------


## Lightning

You're welcome, feron.it is my own domain and I'll keep this service running forever for free. It is just 2 line PHP  :Smilie:

----------


## a_ahmed

Could you write this in VBScript I'm not sure how to do this in VBScript. How to download and parse using VBScript

----------


## Nightwalker83

> Could you write this in VBScript I'm not sure how to do this in VBScript. How to download and parse using VBScript


You have posted in the wrong section for the language you want to use! VBscript is a different language to VB.NET. Please ask the question in a new thread in the ASP/VB Script section of the forum.

----------


## a_ahmed

Of course I know, but was hoping he would share a VBScript version of his shared code too  :Smilie:

----------


## Nightwalker83

> Of course I know, but was hoping he would share a VBScript version of his shared code too


BlindSniper might not know VBScript though.

----------


## a_ahmed

I know I asked this before and I found a solution for vbscript**:


vb Code:
Function MyIP_WinHTTP( )    Dim lngStatus, objHTTP, objMatch, objRE, strText, strURL    MyIP_WinHTTP = "0.0.0.0"    strURL = "http://tools.feron.it/php/ip.php"    Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )    objHTTP.Open "GET", strURL    objHTTP.Send    If objHTTP.Status = 200 Then MyIP_WinHTTP = objHTTP.ResponseText    Set objHTTP = NothingEnd Function

then just call myIP_WinHTTP() and put in a variable or echo it

----------


## eliaspizarro

This can help



```

    Public Structure DatosIPExterna
        Dim IP As String
        Dim CountryCode As String
        Dim CountryName As String
        Dim RegionCode As String
        Dim RegionName As String
        Dim City As String
        Dim TimeZone As String
        Dim Latitude As Double
        Dim Longitude As Double
    End Structure

Public Function GeoIP(Optional IP As String = Nothing) As DatosIPExterna
        Dim DatosIP As New DatosIPExterna

        Using client = New WebClient()
            Try
                Dim strFile As String
                If IsNothing(IP) Then
                    strFile = client.DownloadString("http://freegeoip.net/xml")
                Else
                    strFile = client.DownloadString(String.Format("http://freegeoip.net/xml/{0}", IP))
                End If

                Dim response As XElement = XElement.Parse(strFile)

                With DatosIP
                    .IP = response.Element("IP").Value
                    .CountryCode = response.Element("CountryCode").Value
                    .CountryName = response.Element("CountryName").Value
                    .RegionCode = response.Element("RegionCode").Value
                    .RegionName = response.Element("RegionName").Value
                    .City = response.Element("City").Value
                    .TimeZone = response.Element("TimeZone").Value
                    .Latitude = CType(response.Element("Latitude").Value, Long)
                    .Longitude = CType(response.Element("Longitude").Value, Long)
                End With

            Catch ex As Exception
                Return Nothing
            End Try
        End Using

        Return DatosIP

    End Function
```

----------


## danielmaat

> You're welcome, feron.it is my own domain and I'll keep this service running forever for free. It is just 2 line PHP


@Lightning Cool page! I wouldn't mind hosting this as well, you mind sharing the ip.php code?

----------


## SubRoutine

first time caller, long time listener...




> @Lightning Cool page! I wouldn't mind hosting this as well, you mind sharing the ip.php code?


simple... in Perl:



> #!/usr/bin/perl
> 
> $number = $ENV{REMOTE_ADDR};
> print "Content-type: text/plain\n\n$number";
> 1;
> #example url: https://www.serp2tab.com/ip.cgi


In php:




> <?php  
> echo 'User IP Address - '.$_SERVER['REMOTE_ADDR'];  
> ?>  
> #example url: https://www.serp2tab.com/ip.php

----------

