.Net DynDNS client updater class

The following code is a .Net class that can be used to update a dyndns host alias. It is pretty simple to use. All you have to do is instantiate the class passing the username/password/host to the constructor.

This is the class:

' .Net DynDNS client class by David Gouveia - me[_@_]davidgouveia.net
' Feel free to use it as long as you don't remove the credits :o)
 
Imports System.Net
Imports System.IO
Public Class DynDnsUpdater
    Private _username, _password, _host, _ip As String
 
    Public Enum UpdateStatus
        SUCESS
        FAIL
        NOCHANGE
    End Enum
 
    Private Function checkip() As String
        Dim ipRegex As New System.Text.RegularExpressions.Regex("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", System.Text.RegularExpressions.RegexOptions.Singleline)
 
        Try
            Dim UpdateClient As System.Net.HttpWebRequest = WebRequest.Create( _
            New Uri("http://checkip.dyndns.org"))
            UpdateClient.Timeout = 15000
            Dim response As WebResponse = UpdateClient.GetResponse()
            Dim content As Stream = response.GetResponseStream()
            Dim readstream As New StreamReader(content, System.Text.Encoding.Default)
            Dim IpAddress As String = readstream.ReadToEnd
            readstream.Close()
            content.Close()
            If ipRegex.IsMatch(IpAddress) Then
                Return ipRegex.Match(IpAddress).Groups(0).Value
            Else
                Return Nothing
            End If
        Catch
            Return Nothing
        End Try
    End Function
 
    Public Sub New(ByVal username As String, ByVal password As String, ByVal host As String)
        _username = username
        _password = password
        _host = host
    End Sub
 
    Public Function update() As UpdateStatus
        _ip = checkip()
 
        Dim UpdateClient As System.Net.HttpWebRequest = WebRequest.Create( _
        New Uri("http://members.dyndns.org/nic/update?hostname=" + _host + "&myip=" + _ip))
        UpdateClient.Credentials = New NetworkCredential(_username, _password)
        UpdateClient.PreAuthenticate = True
        UpdateClient.UserAgent = ".Net DynDNS Updater Client"
        UpdateClient.Timeout = 15000
 
        Dim response As WebResponse = UpdateClient.GetResponse()
        Dim content As Stream = response.GetResponseStream()
        Dim readstream As New StreamReader(content, System.Text.Encoding.Default)
        Dim DynDnsResponse As String = readstream.ReadToEnd
        readstream.Close()
        content.Close()
 
        If DynDnsResponse.Contains("good " + _ip) Then
            Return UpdateStatus.SUCESS
        ElseIf DynDnsResponse.Contains("nochg " + _ip) Then
            Return UpdateStatus.NOCHANGE
        Else
            Return UpdateStatus.FAIL
        End If
 
    End Function
End Class

And this is how you can use it:

Dim updater As New DynDnsUpdater("dyndns username", "dyndns password", "hostalias")
        If updatert.update() <> DynDnsUpdater.UpdateStatus.FAIL Then
            MsgBox("SUCCESS!")
        Else
            MsgBox("ERROR!")
        End If

have fun :-)

Leave a Reply

Your email address will not be published.