Sign in to follow this  
Followers 0
logmein

Help with get DNS informations!

3 posts in this topic

This is my code. The problem is my _getDNS () doesn't work properly, it always return old data from the first call. How I can had it "refresh"? Please help me!

Opt ('TrayMenuMode',1)
#include <Array.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <Constants.au3>
#Include <GuiListView.au3>
;===============================================
;DECLARE VARIABLES
Dim $pTitle = 'DNS Changer', $pVersion = '1.0', $pDate = '30-06-2010', $sDNS1, $sDNS2, $sName
;===============================================
TraySetToolTip ($pTitle & ' ' & $pVersion)
;===============================================
;READ DEFAULT DNS
$sString = _getDNS ()
If UBound ($sString) = 1 Then
    $sDefaultDNS1 = $sString[0]
    $sDefaultDNS2 = ''
Else
    $sDefaultDNS1 = $sString[0]
    $sDefaultDNS2 = $sString[1]
EndIf

;$answer1 = StringRegExp($sResult,'((?:\d+)(?:\.\d+){3})',3)

;ConsoleWrite ('Default DNS : ' & $sDefaultDNS1 & '|' & $sDefaultDNS2 & @CRLF)
;===============================================
#Region ### START Koda GUI section ### Form=c:\documents and settings\administrator\my documents\downloads\form1.kxf
$hGUI = GUICreate($pTitle & ' ' & $pVersion, 585, 262, 193, 125)
GUISetFont(10, 400, 0, "Arial")
$Label = GUICtrlCreateLabel("Choose a DNS server :", 8, 8, 136, 20)
$List= GUICtrlCreateListView("Provider|Preferred DNS Server|Alternate DNS Server|Information", 8, 32, 570, 198)
$hList = GUICtrlGetHandle ($List)
GUICtrlSendMsg(-1, 0x101E, 0, 125)
GUICtrlSendMsg(-1, 0x101E, 1, 150)
GUICtrlSendMsg(-1, 0x101E, 2, 150)
GUICtrlSendMsg(-1, 0x101E, 3, 100)
GUICtrlCreateListViewItem ('Default DNS Server|' & $sDefaultDNS1 & '|' & $sDefaultDNS2 & '|',$List)
GUICtrlCreateListViewItem ('Google|8.8.8.8|8.8.4.4|Recommended',$List)
GUICtrlCreateListViewItem ('OpenDNS|208.67.222.222|208.67.220.220|Recommended',$List)
GUICtrlCreateListViewItem ('DNSAdvantage|156.154.70.1|156.154.71.1|',$List)
GUICtrlCreateListViewItem ('Norton|198.153.192.1|198.153.194.1|',$List)
GUICtrlCreateListViewItem ('GTEI DNS|4.2.2.1|4.2.2.2|',$List)
GUICtrlCreateListViewItem ('GTEI DNS|4.2.2.3|4.2.2.4|',$List)
GUICtrlCreateListViewItem ('GTEI DNS|4.2.2.5|4.2.2.6|',$List)
GUICtrlCreateListViewItem ('ScrubIt|67.138.54.100|207.225.209.66|',$List)
GUICtrlCreateListViewItem ('OpenNIC|58.6.115.4|202.83.95.227|Queensland, Australia',$List)
GUICtrlCreateListViewItem ('OpenNIC|69.164.208.50|69.164.211.225|New Jersey, United States',$List)
GUICtrlCreateListViewItem ('Public Name|209.244.0.3|209.244.0.4|',$List)
GUICtrlCreateListViewItem ('Cisco|64.102.255.44|128.107.241.185|',$List)
GUICtrlCreateListViewItem ('Sprintlink|199.2.252.10|204.97.212.10|',$List)
$bSet = GUICtrlCreateButton("&Set", 264, 232, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$bHide = GUICtrlCreateButton("&Hide", 344, 232, 75, 25, 0)
$Label = GUICtrlCreateLabel("DNS Changer 1.0", 8, 232, 105, 20)
GUICtrlSetColor(-1, 0x808080)
$bUpdate = GUICtrlCreateButton("&Update", 424, 232, 75, 25, 0)
$bAbout = GUICtrlCreateButton("&About", 504, 232, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $bSet
            $aSelected = _GUICtrlListView_GetSelectedIndices ($hList,True)
            ConsoleWrite ('Position : ' & $aSelected[0] & @CRLF)
            If $aSelected[0] <> 0 Then
                $sName = _GUICtrlListView_GetItemText($hList,$aSelected[1])
                $sDNS1 = _GUICtrlListView_GetItemText($hList,$aSelected[1],1)
                $sDNS2 = _GUICtrlListView_GetItemText($hList,$aSelected[1],2)
                ConsoleWrite ('DNS 1 : ' & $sDNS1 & @CRLF & 'DNS 2 : ' & $sDNS2 & @CRLF)
            EndIf
            $pSetDNS = RunWait ( @ComSpec & " /c netsh interface ip set dns Local static " & $sDNS1,'',@SW_HIDE)
            Sleep (1000)
            $AddDNS = RunWait ( @ComSpec & " /c netsh interface ip add dns local " & $sDNS2,'',@SW_HIDE)

            If Not @error Then
                $getDNS = _getDNS ()
                ConsoleWrite ('DNS reread : ' & $getDNS[0] & ' ' & $getDNS[1])
                If $getDNS[0] = $sDNS1 And $getDNS[1] = $sDNS2 Then
                    MsgBox (64,$pTitle,'DNS Server Address changed!','',$hGUI)
                Else
                    MsgBox (32,$pTitle,'Error occured! Please try again!','',$hGUI)
                EndIf

            EndIf


    EndSwitch
WEnd
Func _getDNS ()
    Local $sResult, $line
    $hRun = RunWait(@ComSpec & " /c netsh interface ip show dns", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $line = StdoutRead($hRun)
        If @error Then ExitLoop
        $sResult &= $line
    Wend
    ;$answer1 = StringRegExp($sResult,'((?:\d+)(?:\.\d+){3})',3)
    $sString = StringRegExp($sResult,'((?:[1-2]?[0-9]?[0-9]\.){3}(?:[1-2]?[0-9]?[0-9]))',3)
    Return $sString
EndFunc

And this is the console error code :

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\Administrator\Desktop\dnschanger.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams 
+>17:34:43 Starting AutoIt3Wrapper v.2.0.1.24   Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X64 OS:X86)
>Running AU3Check (1.54.19.0)  from:C:\Program Files\AutoIt3
+>17:34:44 AU3Check ended.rc:0
>Running:(3.3.6.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\Administrator\Desktop\dnschanger.au3"    
C:\Documents and Settings\Administrator\Desktop\dnschanger.au3 (21) : ==> Subscript used with non-Array variable.:
$sDefaultDNS1 = $sString[0]
$sDefaultDNS1 = $sString^ ERROR
->17:34:44 AutoIT3.exe ended.rc:1
>Exit code: 1   Time: 2.227

Thanks you first! :-D

Share this post


Link to post
Share on other sites



What is the return value from this line $sString = StringRegExp($sResult,'((?:[1-2]?[0-9]?[0-9]\.){3}(?:[1-2]?[0-9]?[0-9]))',3) ? If the return isn't what you're expecting, an array with the DNS information, then the next check will fail. You need to add in some error checking for that pattern to make sure it's returning what you need/want before you're acting upon what you think is there. Either add an _ArrayDisplay to the _getDNS after that line to see if the array is created, or a consolewrite to tell you if there's an error.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I'm a bit late but using Run instead of RunWait should solve the problem...


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0