Last active
October 18, 2019 14:14
-
-
Save zatarra/5491278 to your computer and use it in GitHub Desktop.
Simple DynDns Updater Class written in VB.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' .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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi dear friend , Is this code work well ?