Jump to content
Sign in to follow this  
wisem2540

Set Static IP or Return Active Network Adapter Name

Recommended Posts

wisem2540

So this all started because I need a small script to change the IP of my workstation. Basically what I wanted to accomplish was have an input box prompt me. I enter 10.11.12.13 and it adds the IP and replaces ,13 with 254 for the gatweway. Figured it would be rather easy. But its not (at least that I could find.

So, while I was searching, I found an app that was written in autoit that allows you to switch your IP between 4 different configs (Fast IP changer is the name)

So then I wrote a small script that would prompt me for input, and then write the config file of Fast IP Changer before launching the app. That works fine. But, it relies heavily on the name of the network adater. So as long as the adaper is name "Local Area Connection" It works. But what if it was named Local Area Connction 2?" so If I had a script that could return the adapter name, I could add that to my config file wriiting.

Any input is welcomed.

Here is my code for my IP Changer that I started with

#include

#include

#include

#include

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=C:\Documents and Settings\test\Desktop\koda_1.7.3.0\Forms\I.kxf

$Form1 = GUICreate("IP Changer", 214, 167, 200, 124)

GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")

$Input1 = GUICtrlCreateInput("", 45, 58, 25, 20)

$Input2 = GUICtrlCreateInput("", 78, 58, 25, 20)

$Input3 = GUICtrlCreateInput("", 113, 58, 25, 20)

$Input4 = GUICtrlCreateInput("", 148, 58, 25, 20)

$Button1 = GUICtrlCreateButton("Set", 82, 104, 49, 25)

GUICtrlSetOnEvent(-1, "Button1Click")

$Label1 = GUICtrlCreateLabel("Your IP Is Currently " & @IPAddress1, 30, 5, 200, 25)

$Label2 = GUICtrlCreateLabel("Enter Desired IP Below", 45, 20, 200, 20)



$Dot1 = GUICtrlCreateLabel(".", 70, 58, 5, 20)

GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")



$Dot2 = GUICtrlCreateLabel(".", 103, 58, 5, 20)

GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")



$Dot3 = GUICtrlCreateLabel(".", 138, 58, 5, 20)

GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")







GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###



While 1

Sleep(100)

WEnd



Func Button1Click()

$Oct1 = GUICtrlRead(3)

$Oct2 = GUICtrlRead(4)

$Oct3 = GUICtrlRead(5)

$Oct4 = GUICtrlRead(6)

MSgbox(0,"IP","IP " & $Oct1 & "." & $Oct2 & "." & $Oct3 & "." & $oct4 & " Has Been Successfully Created" & @CRLF & "Gateway " & $Oct1 & "." & $Oct2 & "." & $Oct3 & ".254")





EndFunc

Func Form1Close()

exit



EndFunc

Func Form1Minimize()



EndFunc

So that works to the point where I have the variables straightened out.

Edited by wisem2540

Share this post


Link to post
Share on other sites
LurchMan

You could use something like this:

$rtn = _GetAdapterName ()
ConsoleWrite($rtn & @LF)
Func _GetAdapterName ()
Local $sComputer = "."
Local $sNetName = ""
$objWMIService = ObjGet("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" _
  & $sComputer & "\root\cimv2")
If IsObj ($objWMIService) Then
  $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapter Where MacAddress="' & _GetMACFromIP (@IPAddress1) & '"', "WQL", 0x10 + 0x20)
  If IsObj($colItems) Then
   For $objItem In $colItems
    $sNetName = $objitem.NetConnectionID
   Next
  EndIf
EndIf
If $sNetName <> "" Then
  Return $sNetName
Else
  Return -1
EndIf
EndFunc
Func _GetMACFromIP ($sIP)
    Local $MAC,$MACSize
    Local $i,$s,$r,$iIP
;Create the struct
;{
;    char    data[6];
;}MAC
    $MAC        = DllStructCreate("byte[6]")
;Create a pointer to an int
;    int *MACSize;
    $MACSize    = DllStructCreate("int")
;*MACSize = 6;
    DllStructSetData($MACSize,1,6)
;call inet_addr($sIP)
    $r = DllCall ("Ws2_32.dll", "int", "inet_addr", _
                    "str", $sIP)
    $iIP = $r[0]
;Make the DllCall
    $r = DllCall ("iphlpapi.dll", "int", "SendARP", _
                    "int", $iIP, _
                    "int", 0, _
                    "ptr", DllStructGetPtr($MAC), _
                    "ptr", DllStructGetPtr($MACSize))
;Format the MAC address into user readble format: 00:00:00:00:00:00
    $s    = ""
    For $i = 0 To 5
        If $i Then $s = $s & ":"
        $s = $s & Hex(DllStructGetData($MAC,1,$i+1),2)
    Next
;Return the user readble MAC address
    Return $s
EndFunc

Credit for the IP Address to MAC Address function goes to LazyCat


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites
UEZ

Here a GUI to enter the needed IP information.

;fast hack by UEZ 2013
#include <Array.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>

Global Const $oMyError = ObjEvent("AutoIt.Error", "oMyError") ; Install a custom error handler

$hGUI = GUICreate("IP Test", 423, 123)
GUISetFont(9, 400, 0, "Arial")
TrayTip("Information", "Reading network setting. Please wait...", 10, 1)
$aNetworkAdapters = _WMI_GetNetworkAdapterNames()
TrayTip("", "",0)
$iComboBox = GUICtrlCreateCombo("", 220, 15, 180, 20, $CBS_DROPDOWN + $CBS_AUTOHSCROLL)
Global $sNetworkAdapters, $z
For $z = 0 To UBound($aNetworkAdapters) - 1
    $sNetworkAdapters &= $aNetworkAdapters[$z][0] & "|"
Next
GUICtrlSetData($iComboBox, StringTrimRight($sNetworkAdapters, 1), $aNetworkAdapters[0][0])
$sCurrentIP = GUICtrlCreateLabel("Current IP:", 16, 18, 62, 19, $SS_RIGHT)
$hCurrenIP = _GUICtrlIpAddress_Create($hGUI, 84, 16, 130, 21)
_GUICtrlIpAddress_Set($hCurrenIP,  $aNetworkAdapters[0][1])
$sNewIP = GUICtrlCreateLabel("New IP:", 16, 53, 62, 19, $SS_RIGHT)
$hNewIP = _GUICtrlIpAddress_Create($hGUI, 84, 51, 130, 21)
_GUICtrlIpAddress_Set($hNewIP, "0.0.0.0")
$sDefaultGateway = GUICtrlCreateLabel("Gateway:", 16, 85, 62, 19, $SS_RIGHT)
$hDefaultGateway = _GUICtrlIpAddress_Create($hGUI, 84, 83, 130, 21)
_GUICtrlIpAddress_Set($hDefaultGateway, "0.0.0.254")
$iBtnOK = GUICtrlCreateButton("OK", 232, 80, 75, 25)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIRegisterMsg($WM_NOTIFY, "")
            GUIDelete()
            Exit
        Case $iBtnOK
            MsgBox(0, "Test",   "Interface Name: " & $aNetworkAdapters[_ArraySearch($aNetworkAdapters, GUICtrlRead($iComboBox))][0] & @CRLF & _
                                                    "New IP: " & _GUICtrlIpAddress_Get($hNewIP) & @CRLF & _
                                                    "Default Gateway: " & _GUICtrlIpAddress_Get($hDefaultGateway))
        Case $iComboBox
            _GUICtrlIpAddress_Set($hCurrenIP,  $aNetworkAdapters[_ArraySearch($aNetworkAdapters, GUICtrlRead($iComboBox))][1])
    EndSwitch
WEnd

Func _WMI_GetNetworkAdapterNames($sHost = ".")
    If $sHost = "." Then $sHost = @ComputerName
    Local $objWMI= ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sHost & "\root\cimv2")
    Local  $oItems1 = $objWMI.InstancesOf("Win32_NetworkAdapter WHERE NetEnabled=TRUE")
    Local $oItems2, $oTokens1, $oTokens2, $aIP, $sIPAddresses, $aResult[100][2], $i = 0
    For $oTokens1 In $oItems1
        $aResult[$i][0] = $oTokens1.NetConnectionID
        $oItems2 =  $objWMI.InstancesOf('Win32_NetworkAdapterConfiguration WHERE Description="' &  $oTokens1.Name & '"')
        For $oTokens2 In $oItems2
            $aIP = $oTokens2.IPAddress
            For $j = 0 To UBound($aIP) - 1
                If StringRegExp($aIP[$j], "(\d+\.\d+\.\d+\.\d+)", 0) Then $aResult[$i][1] = $aIP[$j]
            Next
            $i += 1
        Next
    Next
    If $i > 0 Then 
        ReDim $aResult[$i][2]
        Return $aResult
    EndIf
    Return SetError(1, 0, 0)
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR
    Local $tInfo

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hNewIP
            Switch $iCode
                Case $IPN_FIELDCHANGED
                    Local $aIP = _GUICtrlIpAddress_GetArray($hNewIP)
                    $aIP[3] = "254"
                    _GUICtrlIpAddress_SetArray($hDefaultGateway, $aIP)
            EndSwitch
        Case $hDefaultGateway
            Switch $iCode
                Case $IPN_FIELDCHANGED
                    Local $aIP = _GUICtrlIpAddress_GetArray($hDefaultGateway)
                    $aIP[3] = "254"
                    _GUICtrlIpAddress_SetArray($hDefaultGateway, $aIP)
            EndSwitch
    EndSwitch
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>WM_NOTIFY

Func oMyError()
  ConsoleWrite( @CRLF  & @CRLF & _
                "We intercepted a COM Error !"      & @CRLF  & @CRLF & _
                "err.description is: "    & @TAB & $oMyError.Description    & @CRLF & _
                "err.windescription:"     & @TAB & $oMyError.WinDescription & @CRLF & _
                "err.number is: "         & @TAB & Hex($oMyError.Number, 8) & @CRLF & _
                "err.lastdllerror is: "   & @TAB & $oMyError.LastDllError   & @CRLF & _
                "err.scriptline is: "     & @TAB & $oMyError.Scriptline     & @CRLF & _
                "err.source is: "         & @TAB & $oMyError.Source         & @CRLF & _
                "err.helpfile is: "       & @TAB & $oMyError.Helpfile       & @CRLF & _
                "err.helpcontext is: "    & @TAB & $oMyError.HelpContext & @CRLF  & @CRLF)
EndFunc

To do: apply settings / save it to a ini file.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
wisem2540

I guess I am not cut out for this sort of thing. None of this makes any sense at all to me. Lurchman, your code did absolutely nothing when I ran it. Or maybe that was its intention?

Uez, I can almost grasp what is happening in your script. It appeared at first that $aNetworkAdapters would return what I am looking for. But that doesnt seem to work. Im going to look it over again and see what I missed. Uez, perhaps you could give me an idea as to how I may become more able to understand coding. Everything i know from autoit has been from these forums and self-taught. So when it comes to reading scripts like yours, it all is very overwhelming with variable after variable. If I were going to invest in a programming course, what language do you think would benefit me the most?

thanks

Share this post


Link to post
Share on other sites
UEZ

@wisem2540: why is $aNetworkAdapters not what you are looking for? Do you expect a different name? What the WMI is returning is the device name not the name of the network interface. Do you need the network interface name which can be set manually?

I cannot say whether my code is complicated or not easy to understand but what I learnt about AutoIt is everything from this forum - no other languages. To understand the windows api is an advantage but not easy to learn.

The more you get in touch with AutoIt the more easy it will be for you.

Don't give up and if you have any question feel free to post it here!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
DicatoroftheUSA

I guess I am not cut out for this sort of thing. None of this makes any sense at all to me.

I am sure you are capable, if you can learn english, you can learn another language. If you were trying to learn spanish (insert langauge x), would you give up just because you can't read the first book you pick up?

As far as training, it depends on your wants/needs, If you don't have a clue I would suggest looking for a job that you would like, and see what their employment requirements are. Anyhow I would start at one of the following. I wouldn't recomend paying someone to train you unless you are already in a related career, and need to bring your game up a level. I wouldn't recomend getting into something where you can't (or won't) pull yourself up on your bootstraps, otherwise you may not have the inclination to enjoy it anyways.

http://www.autoitscript.com/wiki/Tutorials

https://www.khanacademy.org/cs/tutorials/programming-basics

http://noexcuselist.com/everything

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites
wisem2540

@Uez, let me clarify. I did a ConsoleWrite($aNetworkAdapters) to see if it would return the name I was looking for. It did nothing. What I am looking for is the connection name. "Local Area Connection" "Local Area Connection 2" Like that. Specifically, i need the one that is active with an IP, So I know which one to set a static to

@dictator, I may have been exaggerating when I said I did not have a clue. I feel I understand alot about autoit, but I do get pretty confused when I see alot of variables in an example script. I work in IT for a healthcare org and I use autoit to automate many of our tasks, and I have came a long way with autoit. Programming has always been a weakness for me, so I get flustered.

But yes, I am in a related career, so I could really benefit from a huge boost in my programming / autoit knowledge

Edited by wisem2540

Share this post


Link to post
Share on other sites
UEZ

@wisem2540: I updated the code from post#3. Does it fit now? ConsoleWrite($aNetworkAdapters) will show you nothing because it is an array!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
wisem2540

It doesnt like line 66 for some reason. Looking into it now...

Share this post


Link to post
Share on other sites
UEZ

There is no error checking and it seems to be that there is a problem with the WMI part on your system.

I added an error object handler where you can see any issues with WMI objects.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
wisem2540

We intercepted a COM Error !

err.description is:

err.windescription:

err.number is: 80041010

err.lastdllerror is: 0

err.scriptline is: 56

err.source is:

err.helpfile is:

err.helpcontext is: 0

IP.au3 (21) : ==> Subscript used with non-Array variable.:

GUICtrlSetData($iComboBox, StringTrimRight($sNetworkAdapters, 1), $aNetworkAdapters[0][0])

GUICtrlSetData($iComboBox, StringTrimRight($sNetworkAdapters, 1), $aNetworkAdapters^ ERROR

>Exit code: 1 Time: 0.761

Share this post


Link to post
Share on other sites
UEZ

0x80041010 means Invalid Class. Which os is installed on the affected system?

I tested it on my notebook running Win7 x64 and it works properly.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
wisem2540

Windows 7 will be the target OS 32bit.... but i ran it on an XP station

Share this post


Link to post
Share on other sites
wisem2540

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\test\Desktop\koda_1.7.3.0\Forms\I.kxf
$Form1 = GUICreate("IP Changer", 214, 167, 200, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
$Input1 = GUICtrlCreateInput("", 45, 58, 25, 20)
$Input2 = GUICtrlCreateInput("", 78, 58, 25, 20)
$Input3 = GUICtrlCreateInput("", 113, 58, 25, 20)
$Input4 = GUICtrlCreateInput("", 148, 58, 25, 20)
$Button1 = GUICtrlCreateButton("Set", 82, 104, 49, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Label1 = GUICtrlCreateLabel("Your IP Is Currently " & @IPAddress1, 30, 5, 200, 25)
$Label2 = GUICtrlCreateLabel("Enter Desired IP Below", 45, 20, 200, 20)
$Label3 = GUICtrlCreateLabel("Note: 0.0.0.0 = DHCP", 45, 140, 200, 20)
$Dot1 = GUICtrlCreateLabel(".", 70, 58, 5, 20)
GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")
$Dot2 = GUICtrlCreateLabel(".", 103, 58, 5, 20)
GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")
$Dot3 = GUICtrlCreateLabel(".", 138, 58, 5, 20)
GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
Sleep(100)
WEnd

Func Form1Close()
exit
EndFunc
Func Form1Minimize()
EndFunc
Func Button1Click()
$Oct1 = GUICtrlRead(3)
$Oct2 = GUICtrlRead(4)
$Oct3 = GUICtrlRead(5)
$Oct4 = GUICtrlRead(6)
$IP = $Oct1 & "." & $Oct2 & "." & $Oct3 & "." & $oct4
$Subnet = "255.255.255.0"
$Gateway = ($Oct1 & "." & $Oct2 & "." & $Oct3 & ".254")

FileWrite("test.vbs"," Option Explicit" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","On Error Resume Next" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","Dim objWMIService" & @CRLF)
FileWrite("test.vbs","Dim objNetAdapter" & @CRLF)
FileWrite("test.vbs","Dim strComputer" & @CRLF)
FileWrite("test.vbs","Dim arrIPAddress" & @CRLF)
FileWrite("test.vbs","Dim arrSubnetMask" & @CRLF)
FileWrite("test.vbs","Dim arrGateway" & @CRLF)
FileWrite("test.vbs","Dim colNetAdapters" & @CRLF)
FileWrite("test.vbs","Dim errEnableStatic" & @CRLF)
FileWrite("test.vbs","Dim errGateways" & @CRLF)
FileWrite("test.vbs","Dim strInput" & @CRLF)
FileWrite("test.vbs","Dim errFailed" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","errFailed = 0" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs",'strInput = "' & $IP & '"' & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs",'If strInput = "0.0.0.0" Then' & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs",'strComputer = "."' & @CRLF)
FileWrite("test.vbs",'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")' & @CRLF)
FileWrite("test.vbs",'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")' & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","For Each objNetAdapter in colNetAdapters" & @CRLF)
     FileWrite("test.vbs","errEnable = objNetAdapter.EnableDHCP()" & @CRLF)
     FileWrite("test.vbs","If Not errEnable = 0 Then" & @CRLF)
     FileWrite("test.vbs",'WScript.Echo "Setting DHCP Failed."' & @CRLF)
FileWrite("test.vbs","errFailed = 1" & @CRLF)
     FileWrite("test.vbs","End If" & @CRLF)
FileWrite("test.vbs","Next" & @CRLF)
FileWrite("test.vbs","Else" & @CRLF)
FileWrite("test.vbs",'strComputer = "."' & @CRLF)
FileWrite("test.vbs","arrIPAddress = Array(strInput)" & @CRLF)
FileWrite("test.vbs",'arrSubnetMask = Array("' & $Subnet & '")' & @CRLF)
FileWrite("test.vbs",'arrGateway = Array("' & $Gateway & '")' & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs",'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")' & @CRLF)
FileWrite("test.vbs",'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")' & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","For Each objNetAdapter in colNetAdapters" & @CRLF)
     FileWrite("test.vbs","errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)" & @CRLF)
     FileWrite("test.vbs","If Not errEnableStatic = 0 Then" & @CRLF)
     FileWrite("test.vbs",'WScript.Echo "Failure assigning IP/Subnet."' & @CRLF)
FileWrite("test.vbs","errFailed = 1" & @CRLF)
     FileWrite("test.vbs","End If" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
     FileWrite("test.vbs",'errGateways = objNetAdapter.SetGateways(arrGateway)' & @CRLF)
     FileWrite("test.vbs","If Not errGateways = 0 Then" & @CRLF)
     FileWrite("test.vbs",'WScript.Echo "Failure assigning Gateway."' & @CRLF)
FileWrite("test.vbs","errFailed = 1" & @CRLF)
     FileWrite("test.vbs","End If" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","Next" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","End If" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","If errFailed = 0 Then" & @CRLF)
FileWrite("test.vbs","")
FileWrite("test.vbs",'WScript.Echo "IP Settings Successfully Modified."' & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","End If" & @CRLF)
FileWrite("test.vbs","" & @CRLF)
FileWrite("test.vbs","WScript.Quit" & @CRLF)
EndFunc
Uez,I was able to tie my script together with a VBS file I found on the net. I do not know (yet) how to tie to VBS commands into autoit. so I created a function to write the VBS file. While this script does not actually change the IP, it outputs a VBS that does. I know this is not the best way to do this obviously, but it does work. Have a look.

EDIT. I made some quick changes. Entering an IP of 0.0.0.0 will set it to DHCP - also added a label in the GUI so the user knows about DHCP

To Do List:

1. Figure out how to incorporate DNS into this (that will happen after some more sleep)

2. Hopefully eliminate the VBS script and let autoit handle it all

3. Test on more OS Versions than XP

Edited by wisem2540

Share this post


Link to post
Share on other sites
DicatoroftheUSA

Check out the following, I found it to not be perfect (especially with microsofts massive eg slmgr.vbs)

But it can give you an idea of how to start

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites
wisem2540

#include

#include

#include

#include

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=C:\Documents and Settings\test\Desktop\koda_1.7.3.0\Forms\I.kxf

$Form1 = GUICreate("IP Changer", 214, 167, 200, 124)

GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")

$Input1 = GUICtrlCreateInput("", 45, 58, 25, 20)

$Input2 = GUICtrlCreateInput("", 78, 58, 25, 20)

$Input3 = GUICtrlCreateInput("", 113, 58, 25, 20)

$Input4 = GUICtrlCreateInput("", 148, 58, 25, 20)

$Button1 = GUICtrlCreateButton("Set", 82, 104, 49, 25)

GUICtrlSetOnEvent(-1, "Button1Click")

$Label1 = GUICtrlCreateLabel("Your IP Is Currently " & @IPAddress1, 30, 5, 200, 25)

$Label2 = GUICtrlCreateLabel("Enter Desired IP Below", 45, 20, 200, 20)

$Label3 = GUICtrlCreateLabel("Note: 0.0.0.0 = DHCP", 45, 140, 200, 20)



$Dot1 = GUICtrlCreateLabel(".", 70, 58, 5, 20)

GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")



$Dot2 = GUICtrlCreateLabel(".", 103, 58, 5, 20)

GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")



$Dot3 = GUICtrlCreateLabel(".", 138, 58, 5, 20)

GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")







GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###



While 1

Sleep(100)

WEnd





Func Form1Close()

exit



EndFunc

Func Form1Minimize()

EndFunc



Func Button1Click()

$Oct1 = GUICtrlRead(3)

$Oct2 = GUICtrlRead(4)

$Oct3 = GUICtrlRead(5)

$Oct4 = GUICtrlRead(6)

$IP = $Oct1 & "." & $Oct2 & "." & $Oct3 & "." & $oct4

$Subnet = "255.255.255.0"

$Gateway = ($Oct1 & "." & $Oct2 & "." & $Oct3 & ".254")





FileWrite("test.vbs"," Option Explicit" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","On Error Resume Next" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","Dim objWMIService" & @CRLF)

FileWrite("test.vbs","Dim objNetAdapter" & @CRLF)

FileWrite("test.vbs","Dim strComputer" & @CRLF)

FileWrite("test.vbs","Dim arrIPAddress" & @CRLF)

FileWrite("test.vbs","Dim arrSubnetMask" & @CRLF)

FileWrite("test.vbs","Dim arrGateway" & @CRLF)

FileWrite("test.vbs","Dim arrDNSServers" & @CRLF)

FileWrite("test.vbs","Dim colNetAdapters" & @CRLF)

FileWrite("test.vbs","Dim errEnableStatic" & @CRLF)

FileWrite("test.vbs","Dim errGateways" & @CRLF)

FileWrite("test.vbs","Dim strInput" & @CRLF)

FileWrite("test.vbs","Dim errFailed" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","errFailed = 0" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs",'strInput = "' & $IP & '"' & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs",'If strInput = "0.0.0.0" Then' & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs",'strComputer = "."' & @CRLF)

FileWrite("test.vbs",'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")' & @CRLF)

FileWrite("test.vbs",'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")' & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","For Each objNetAdapter in colNetAdapters" & @CRLF)

FileWrite("test.vbs","errEnable = objNetAdapter.EnableDHCP()" & @CRLF)

FileWrite("test.vbs","If Not errEnable = 0 Then" & @CRLF)

FileWrite("test.vbs",'WScript.Echo "Setting DHCP Failed."' & @CRLF)

FileWrite("test.vbs","errFailed = 1" & @CRLF)

FileWrite("test.vbs","End If" & @CRLF)

FileWrite("test.vbs","Next" & @CRLF)

FileWrite("test.vbs","Else" & @CRLF)

FileWrite("test.vbs",'strComputer = "."' & @CRLF)

FileWrite("test.vbs","arrIPAddress = Array(strInput)" & @CRLF)

FileWrite("test.vbs",'arrSubnetMask = Array("' & $Subnet & '")' & @CRLF)

FileWrite("test.vbs",'arrGateway = Array("' & $Gateway & '")' & @CRLF)

FileWrite("test.vbs",'arrDNSServers = Array("4.1.1.1", "4.2.2.2")' & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs",'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")' & @CRLF)

FileWrite("test.vbs",'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")' & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","For Each objNetAdapter in colNetAdapters" & @CRLF)

FileWrite("test.vbs","errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)" & @CRLF)

FileWrite("test.vbs","If Not errEnableStatic = 0 Then" & @CRLF)

FileWrite("test.vbs",'WScript.Echo "Failure assigning IP/Subnet."' & @CRLF)

FileWrite("test.vbs","errFailed = 1" & @CRLF)

FileWrite("test.vbs","End If" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs",'errGateways = objNetAdapter.SetGateways(arrGateway)' & @CRLF)

FileWrite("test.vbs","If Not errGateways = 0 Then" & @CRLF)

FileWrite("test.vbs",'WScript.Echo "Failure assigning Gateway."' & @CRLF)

FileWrite("test.vbs","errFailed = 1" & @CRLF)

FileWrite("test.vbs","End If" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","errDNSServers = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","Next" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","End If" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","If errFailed = 0 Then" & @CRLF)

FileWrite("test.vbs","")

FileWrite("test.vbs",'WScript.Echo "IP Settings Successfully Modified."' & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","End If" & @CRLF)

FileWrite("test.vbs","" & @CRLF)

FileWrite("test.vbs","WScript.Quit" & @CRLF)





MsgBox(0, "File", "File Generated")





;FileDelete("Test.vbs")

;MsgBox(0, "File", "File Deleted")



Exit





EndFunc

Thanks Dictator.

I have been doing some research with VBS and I have successfully gotten DNS to work. So that knocks one off the list.

Here is the code so far. I will look into the converter now.

Edited by wisem2540

Share this post


Link to post
Share on other sites
wisem2540

Dictator.

The converter does work. I do get a console error that line 67 is missomg a declaration of a variable. But, Its not causing an issue. So im happy. Only thing left is to test on other OS versions.

THANKS!

Edited by wisem2540

Share this post


Link to post
Share on other sites
BrewManNH

Here's a way of doing it just in AutoIt without resorting to using another script.

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\test\Desktop\koda_1.7.3.0\Forms\I.kxf
$Form1 = GUICreate("IP Changer", 214, 167, 200, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
$Input1 = GUICtrlCreateInput("", 45, 58, 25, 20)
$Input2 = GUICtrlCreateInput("", 78, 58, 25, 20)
$Input3 = GUICtrlCreateInput("", 113, 58, 25, 20)
$Input4 = GUICtrlCreateInput("", 148, 58, 25, 20)
$Button1 = GUICtrlCreateButton("Set", 82, 104, 49, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Label1 = GUICtrlCreateLabel("Your IP Is Currently " & @IPAddress1, 30, 5, 200, 25)
$Label2 = GUICtrlCreateLabel("Enter Desired IP Below", 45, 20, 200, 20)
$Label3 = GUICtrlCreateLabel("Note: 0.0.0.0 = DHCP", 45, 140, 200, 20)
$Dot1 = GUICtrlCreateLabel(".", 70, 58, 5, 20)
GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")
$Dot2 = GUICtrlCreateLabel(".", 103, 58, 5, 20)
GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")
$Dot3 = GUICtrlCreateLabel(".", 138, 58, 5, 20)
GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
    Sleep(100)
WEnd
Func Button1Click()
    Local $hFile = FileOpen(@ScriptDir & "\test.vbs", 2)
    $Oct1 = GUICtrlRead(3)
    $Oct2 = GUICtrlRead(4)
    $Oct3 = GUICtrlRead(5)
    $Oct4 = GUICtrlRead(6)
    $IP = $Oct1 & "." & $Oct2 & "." & $Oct3 & "." & $Oct4
    $Subnet = "255.255.255.0"
    $Gateway = ($Oct1 & "." & $Oct2 & "." & $Oct3 & ".254")
    $errFailed = 0
    $strInput = $IP
    If $strInput = "0.0.0.0" Then
        $strComputer = "."
        Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
        Local $colNetAdapters = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        For $objNetAdapter In $colNetAdapters
            $errEnable = $objNetAdapter.EnableDHCP()
            If Not $errEnable = 0 Then
                MsgBox($MB_SYSTEMMODAL, "Error", "Setting DHCP Failed.")
                $errFailed = 1
            EndIf
        Next
    Else
        $strComputer = "."
        $sIPAddress = $strInput
        $sSubnetMask = "255.255.255.0"
        $sGateway = "10.41.152.254"
        Local $arrDNSServers[2] = ["4.1.1.1", "4.2.2.2"]
        Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
        Local $colNetAdapters = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        For $objNetAdapter In $colNetAdapters
            $errEnableStatic = $objNetAdapter.EnableStatic($sIPAddress, $sSubnetMask)
            If Not $errEnableStatic = 0 Then
                MsgBox($MB_SYSTEMMODAL, "Error", "Failure assigning IP/Subnet.")
                $errFailed = 1
            EndIf
            $errGateways = $objNetAdapter.SetGateways($sGateway)
            If Not $errGateways = 0 Then
                MsgBox($MB_SYSTEMMODAL, "Error", "Failure assigning Gateway.")
                $errFailed = 1
            EndIf
            $errDNSServers = $objNetAdapter.SetDNSServerSearchOrder($arrDNSServers)
        Next
    EndIf
    If $errFailed = 0 Then
        MsgBox($MB_SYSTEMMODAL, "Error", "IP Settings Successfully Modified.")
    EndIf


;~  FileWrite($hFile, " Option Explicit" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "On Error Resume Next" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "Dim objWMIService" & @CRLF)
;~  FileWrite($hFile, "Dim objNetAdapter" & @CRLF)
;~  FileWrite($hFile, "Dim strComputer" & @CRLF)
;~  FileWrite($hFile, "Dim arrIPAddress" & @CRLF)
;~  FileWrite($hFile, "Dim arrSubnetMask" & @CRLF)
;~  FileWrite($hFile, "Dim arrGateway" & @CRLF)
;~  FileWrite($hFile, "Dim arrDNSServers" & @CRLF)
;~  FileWrite($hFile, "Dim colNetAdapters" & @CRLF)
;~  FileWrite($hFile, "Dim errEnableStatic" & @CRLF)
;~  FileWrite($hFile, "Dim errGateways" & @CRLF)
;~  FileWrite($hFile, "Dim strInput" & @CRLF)
;~  FileWrite($hFile, "Dim errFailed" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "errFailed = 0" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, 'strInput = "' & $IP & '"' & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, 'If strInput = "0.0.0.0" Then' & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, 'strComputer = "."' & @CRLF)
;~  FileWrite($hFile, 'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")' & @CRLF)
;~  FileWrite($hFile, 'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")' & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "For Each objNetAdapter in colNetAdapters" & @CRLF)
;~  FileWrite($hFile, "errEnable = objNetAdapter.EnableDHCP()" & @CRLF)
;~  FileWrite($hFile, "If Not errEnable = 0 Then" & @CRLF)
;~  FileWrite($hFile, 'WScript.Echo "Setting DHCP Failed."' & @CRLF)
;~  FileWrite($hFile, "errFailed = 1" & @CRLF)
;~  FileWrite($hFile, "End If" & @CRLF)
;~  FileWrite($hFile, "Next" & @CRLF)
;~  FileWrite($hFile, "Else" & @CRLF)
;~  FileWrite($hFile, 'strComputer = "."' & @CRLF)
;~  FileWrite($hFile, "arrIPAddress = Array(strInput)" & @CRLF)
;~  FileWrite($hFile, 'arrSubnetMask = Array("' & $Subnet & '")' & @CRLF)
;~  FileWrite($hFile, 'arrGateway = Array("' & $Gateway & '")' & @CRLF)
;~  FileWrite($hFile, 'arrDNSServers = Array("4.1.1.1", "4.2.2.2")' & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, 'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")' & @CRLF)
;~  FileWrite($hFile, 'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")' & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "For Each objNetAdapter in colNetAdapters" & @CRLF)
;~  FileWrite($hFile, "errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)" & @CRLF)
;~  FileWrite($hFile, "If Not errEnableStatic = 0 Then" & @CRLF)
;~  FileWrite($hFile, 'WScript.Echo "Failure assigning IP/Subnet."' & @CRLF)
;~  FileWrite($hFile, "errFailed = 1" & @CRLF)
;~  FileWrite($hFile, "End If" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, 'errGateways = objNetAdapter.SetGateways(arrGateway)' & @CRLF)
;~  FileWrite($hFile, "If Not errGateways = 0 Then" & @CRLF)
;~  FileWrite($hFile, 'WScript.Echo "Failure assigning Gateway."' & @CRLF)
;~  FileWrite($hFile, "errFailed = 1" & @CRLF)
;~  FileWrite($hFile, "End If" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "errDNSServers = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "Next" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "End If" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "If errFailed = 0 Then" & @CRLF)
;~  FileWrite($hFile, "")
;~  FileWrite($hFile, 'WScript.Echo "IP Settings Successfully Modified."' & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "End If" & @CRLF)
;~  FileWrite($hFile, "" & @CRLF)
;~  FileWrite($hFile, "WScript.Quit" & @CRLF)
;~  MsgBox(0, "File", "File Generated")
;~  ;FileDelete($hFile)
    ;MsgBox(0, "File", "File Deleted")
    Exit
EndFunc   ;==>Button1Click
Func Form1Close()
    Exit
EndFunc   ;==>Form1Close
Func Form1Minimize()
EndFunc   ;==>Form1Minimize

I'm not 100% sure the vbscript has been translated correctly to AutoIt, as I'm unsure of what you were doing with the commands, as I haven't ever used a script to set an IP address before.


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

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  

×