Jump to content
Sign in to follow this  
danbridgland

Problem with netsh in IP address changing application

Recommended Posts

danbridgland

Hi,

I have created a script with the assistance of a few posts on this forum (acknowledgments to follow) to easily enable the it challenged users amongst the population to easily switch between Static IP and DHCP address, However I have run into a few issues, Notably, I'm trying to use the "netsh" to switch a PC's ip address between DHCP and a user defined (data stored in a text file on the pc hard disk) static address.

The exact problem is that I need to instruct netsh to change the IP address separately from the DNS instruction. This means that whilst the IP instruction in being executed, the following DNS instruction needs to wait for the first ip instruction to complete, being M$ windows this can take as long a minute. once the ip instruction is complete the dns instruction can begin, which adds a further few seconds. Does anyone know of a way around this using the netsh command or any other reliable embedded windows xp tool?

I have attached the script for anyone to look over, I acknowledge there are other issues with loops, active NIC lookups, and error checking. though if anyone can shed any light of any of these issues also, I would be very grateful.

Thanks for taking the time to read this post.

Dan

;########################################################
;#                      Ip Changer                      #
;#              Created using AutoIt v3                 #
;#                                                      #
;#  Created by Dan Bridgland. Copyright May 2007        #
;#  Acknowledgements (to follow)                        #
;#                                                      #
;#  A simple utility designed primarily to allow laptop #
;#  users a simple yet effective way to switch between  #
;#  static (in office) and DHCP (on the move) ip        #
;#  addresses, with out needing to know anything about  #
;#  networking or IP terminology                        #
;#                                                      #
;########################################################




#include <GUIConstants.au3>
#include <File.au3>

;Create Gui
    GUICreate("GUI menu",400,200)


GuiCtrlCreatePic("1.bmp",10,15, 49,46)
GuiCtrlCreatePic("2.bmp",10,101, 50,45)

    ;Gui Menus
        $filemenu = GuiCtrlCreateMenu ("File")
            $ExportIP = GuiCtrlCreateMenuitem ("Export selected interface settings...",$filemenu)
            $FixedIPFile = GuiCtrlCreateMenuitem ("Edit fixed IP address setting file...",$filemenu)
            $separator1 = GuiCtrlCreateMenuitem ("",$filemenu)
            $exititem = GuiCtrlCreateMenuitem ("Exit",$filemenu)
        $helpmenu = GuiCtrlCreateMenu ("Help")
            $NetCon = GuiCtrlCreateMenuitem ("Open Network Connections Window",$helpmenu)
            $aboutitem = GuiCtrlCreateMenuitem ("About",$helpmenu)

    ;GUI Buttons
        $DHCPbutton = GuiCtrlCreateButton ("DHCP",300,100,70,20)
            $DHCPLabel = GuiCtrlCreateLabel("To switch to a DHCP IP address press DHCP", 70, 103,220)
        $Fixedbutton = GuiCtrlCreateButton ("Static",300,130,70,20)
            $FixedLabel = GuiCtrlCreateLabel("To switch to a Fixed IP address press Fixed", 70, 133,220)

;Get Network Connection Names
Dim $i, $base, $key, $name, $lan, $combolist, $CurrentIPSetting, $CurrentIPSettingSetting
$base = "HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
While 1
    $i += 1
    $key = RegEnumKey($base, $i)
    If @error <> 0 Then ExitLoop

    $name = RegRead($base & "\" & $key & "\Connection", "Name")
    $type = RegRead($base & "\" & $key & "\Connection", "PnpInstanceID")
    If StringLeft($key, 1) = "{" Then
        If StringLeft($type, 4) = "PCI\" Or StringLeft($type, 8) = "ROOT\NET" Then
        $NIC = $key
        $lan &=  $name & "|"
        $IsDhcp = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NIC ,'EnableDHCP')
            If $IsDhcp = 1 Then 
                    $CurrentIPSettingSetting = "DHCP"
                ElseIf $IsDhcp = 0 Then 
                    $CurrentIPSettingSetting = "Static"
            EndIf
        EndIf
    EndIf
WEnd
$lan = StringSplit(StringTrimRight($lan, 1), '|')


;MsgBox(1,"title",$NIC)

;MsgBox(1,"dhcp?",$IsDhcp)




;Combolist
    For $x = 1 To $lan[0]
    $combolist = $combolist & $lan [$x] & "|"
    Next    

    $ComboLabel = GuiCtrlCreateLabel("Please Select the network interface" & @LF & "you which to modify", 70, 10, 300)
    $ComboLabelsetting = GuiCtrlCreateLabel("Currently set to..", 280, 24, 300)
    $CurrentIPSetting = GUICtrlCreateInput ("", 280,  40, 100, 20,$ES_READONLY)
    GUICtrlSetData($CurrentIPSetting,$CurrentIPSettingSetting)
    $combo = GUICtrlCreateCombo("",70,40,200,120, $CBS_DROPDOWNLIST)
    GUICtrlSetData($combo,$combolist,$lan[1])



GuiSetState()

While 1
    $ComboSelect   = GUICtrlRead($combo, 1); return the text of the menu item
    $msg = GUIGetMsg()
    
    Select
        Case $msg = $GUI_EVENT_CLOSE 
            ExitLoop
        
        Case $msg = $ExportIP
            run(@comspec & ' /c netsh -c interface dump > c:\location1.txt',"")
                WinWaitActive("C:\WINDOWS\system32\cmd.exe")
                    ProgressOn("Exporting " &  $ComboSelect, "Please Wait", "0 percent")
                        For $i = 1 to 100 step 1
                            sleep(100)
                            ProgressSet( $i, $i & " percent")
                            If not WinExists("C:\WINDOWS\system32\cmd.exe") Then ExitLoop
                        Next
                        ProgressSet(100 , "Done", "Complete")
                    ProgressOff()
            Msgbox(0, "Network Interface", 'Export of ' & $ComboSelect & ' settings complete')
        Case $msg = $exititem
            ExitLoop
        
        Case $msg = $FixedIPFile
            RunWait("Notepad c:\ipaddress_settings.txt")
        
        
        Case $msg = $DHCPbutton
                $msg = GUIGetMsg()
                    run(@comspec & ' /c netsh interface ip set address "' & $ComboSelect & '" dhcp',"")
                        WinWaitActive("C:\WINDOWS\system32\cmd.exe")
                            ProgressOn( $ComboSelect, "Switching to DHCP Please Wait", "0 percent")
                                For $i = 1 to 100 step 1
                                    sleep(100)
                                    ProgressSet( $i, $i & " percent")
                        If not WinExists("C:\WINDOWS\system32\cmd.exe") Then run(@comspec & ' /c netsh interface ip set dns name="' & $ComboSelect & '" source=dhcp',"")
                            WinWaitActive("C:\WINDOWS\system32\cmd.exe")
                        If not WinExists("C:\WINDOWS\system32\cmd.exe") Then ExitLoop
                            Next
                            ProgressSet(100 , "Done", "Complete")
                            ProgressOff()
                Msgbox(0, "Network Interface", $ComboSelect & ' is now set to DHCP IP address')
        Case $msg = $NetCon
            run("control.exe /NCpa.cpl")

        Case $msg = $aboutitem
            Msgbox(0,"About","IP Address Changer" & @LF & "Version 1.0" & @LF & @LF & "This application is primarially designed for laptop users to easily enable/disable" & @lf & "DHCP and Static Ip address's for their network interface card" & @lf & @lf & "If you have any questions please contact a member of IT")

        Case $msg = $Fixedbutton
            $msg = GUIGetMsg()
;               $file = FileOpen("c:\ipaddress_settings.txt", 0)





$ipline1 = FileReadLine("c:\ipaddress_settings.txt",1)
$ipline2 = FileReadLine("c:\ipaddress_settings.txt",2)
$ipline3 = FileReadLine("c:\ipaddress_settings.txt",3)
$ipline4 = FileReadLine("c:\ipaddress_settings.txt",4)

;Msgbox(0,"About",$ipline1 & @LF & $ipline2 & @LF & $ipline3 & @LF & $ipline4 )

;$instruction = 'netsh interface ip set address name="Local Area Connection" source=static addr=' & $ipline1 & ' mask=' & $ipline2 & ' gateway=' & $ipline3 & ' gwmetric=1'

Run(@ComSpec & ' /c netsh interface ip set address name="' & $ComboSelect & '" source=static addr=' & $ipline1 & ' mask=' & $ipline2 & ' gateway=' & $ipline3 & ' gwmetric=1',"")
                        WinWaitActive("C:\WINDOWS\system32\cmd.exe")
                            ProgressOn( $ComboSelect, "Switching to DHCP Please Wait", "0 percent")
                                For $i = 1 to 100 step 1
                                    sleep(500)
                                    ProgressSet( $i, $i & " percent")
                                    If not WinExists("C:\WINDOWS\system32\cmd.exe") Then run(@comspec & ' /c Netsh interface ip set dns name="' & $ComboSelect & '" source=static addr=' & $ipline4 & ' register=none',"")
                                    WinWaitActive("C:\WINDOWS\system32\cmd.exe")
                                    If not WinExists("C:\WINDOWS\system32\cmd.exe") Then ExitLoop
                                Next
                                ProgressSet(100 , "Done", "Complete")
                            ProgressOff()
                Msgbox(0, "Network Interface", $ComboSelect & ' is now set to Fixed IP address')
    
;FileClose($file)   

EndSelect
WEnd

GUIDelete()

Share this post


Link to post
Share on other sites
PsaltyDS

The exact problem is that I need to instruct netsh to change the IP address separately from the DNS instruction. This means that whilst the IP instruction in being executed, the following DNS instruction needs to wait for the first ip instruction to complete, being M$ windows this can take as long a minute. once the ip instruction is complete the dns instruction can begin, which adds a further few seconds. Does anyone know of a way around this using the netsh command or any other reliable embedded windows xp tool?

Have you tried just using RunWait() instead of Run() for that particular netsh command?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
danbridgland

Have you tried just using RunWait() instead of Run() for that particular netsh command?

:)

Hi PsaltyDS,

Thanks for your reply, I have considered RunWait() but as I see it, its only going to produce the same problems. or do you have a better idea of how RunWait() might work?

Regards

Dan

Share this post


Link to post
Share on other sites
PsaltyDS

Maybe I don't really understand the question:

This means that whilst the IP instruction in being executed, the following DNS instruction needs to wait for the first ip instruction to complete...

If the "IP instruction" uses RunWait(), the script won't continue till it's done. Then you Run() the "DNS instruction". That doesn't fix the problem?

Another possibility is to Run() with $STDOUT_CHILD and use a StdOutRead() loop to tell you when it's done, but the RunWait() is easier...

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×