Jump to content
dmkirkland

Newbie...Sorry

Recommended Posts

dmkirkland

I have a script I downloaded from the forums here that works as advertised. It displays the IP address of a connected interface by "polling" (?) WMI. I'm relatively new to using AutoIT but have been able to adapt many scripts I have found here to do exactly what I have wanted to do in the past. Most notably, I have determined how to utilize CMD in Windows to send a netsh command to set the IP address. Not very pretty, but effective. The disadvantage (as I'm sure many/most/all of you know is that you have to know the interface name in order to utilize this command. Not very easy to use to provide a simple script for a novice user. I have learned WMI seems much more effective and user friendly but, although I can find a script or 2 to display IP address and have found a couple to set one (not entirely the way I prefer but works), what I really want is to understand, step-by-step, how/why the script works so I can further my knowledge and keep from bugging everyone here on the forums as much as possible (I'm a big proponent of "teaching a man to fish"). I just can't figure it out entirely. Below is the script. And I thoroughly apologize for the novice request. I'm sure you guys have better problems to solve but I'm at my whit's end with learning this.

 

Local $IPConfig
$strComputer = "localhost"
$ping = Ping($strComputer, 500)
If $ping Then
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$IPConfigSet = $objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ")

For $IPConfig in $IPConfigSet
If $IPConfig.IPAddress <> "" Then
    For $x = 0 to UBound($IPConfig.IPAddress) - 1
        msgbox(0,"Current IP","Your IP address is " & $IPConfig.IPAddress($x))
    Next
EndIf
Next
EndIf

Share this post


Link to post
Share on other sites
dmkirkland

Sorry. I clearly did not read the rules for the DEV forum.

Share this post


Link to post
Share on other sites
dmkirkland

On a side note, my mission is to accomplish a very simple script that sets a specific ("hard-coded") IP address along with a subnet mask (only these 2 items will need to be set-no gateway, no DNS). No request for input will be collected.

Share this post


Link to post
Share on other sites
xCROv

Are you trying to accomplish just setting the IP or just wanting to understand what you have found that is working for you?

Share this post


Link to post
Share on other sites
dmkirkland

Actually both. Most importantly, I would really like to understand the script I pasted before...in complete detail. I feel like if I understand it line for line I will be better suited to helping myself with the other part of this project. I'm basically going to be putting together a small GUI with multiple buttons. The first will set one IP on the PC, the second, another IP and so on (essentially 25 different configs). But I will only be setting IP and Subnet Mask. My only worry is that if I understand how the "Get IP" script pasted above works, it may not offer me enough insight into creating the "Set IP" script (but I plan to cross that bridge if I come to it).

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

so, write out a description, line by line as you see it and post here for help or so we can verify you understand it. it's not bad. basically it does the following.

  1. ping a computer
  2. if you get a valid ping back, use WMI to get/set the network configuration
  3. loop through collection $IPConfigSet and if IP is not "" then do something.
Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
dmkirkland

I've used the command prompt method in the past. It works but unfortunately requires knowledge of the adapter name for the interface you wish to change. That's why I wanted to understand better what was happening in the script in hopes to reverse engineer it, learn more options and apply what I've learned to a script to set the IP in the same/semi-reverse fashion. I will analyze it and see if I can post tonight what my dissection of it would be. Thanks.

Share this post


Link to post
Share on other sites
dmkirkland

I guess one question I can ask now is why would there be need to assign variable to variable to variable? It seems this is frequently done with most scripts I've downloaded and tried to analyze. It seems redundant and overly complicated in some cases. The beginning of the script for this one assigns a variable to the computer you want to ping, then assigns a variable to the results of the ping on the variable previously assigned....seems overly complicated to me (again, to me) but I can't wrap my head around why it would be necessary or helpful to compound variables in that way. Tiny brain...lots of information... :-)

Share this post


Link to post
Share on other sites
ripdad
Posted (edited)

Everyone has different coding habits. Some good, some bad.

So, if you see something in someones code that doesn't set well with you --
no one will stop you from modifying or tweaking it. Like this...

I noticed there wasn't any error checking in the code you posted.
So, I adjusted it a little bit.
 

#include 'Array.au3'
Opt('MustDeclareVars', 1)

Local $_objError = ObjEvent('AutoIt.Error', '_ObjErrorHandler')

MsgBox(0, '', Example())

Func Example($strComputer = 'localhost')
    If Ping($strComputer, 500) < 1 Then Return -1
    Local $objWMI = ObjGet('winmgmts:\\' & $strComputer & '\root\cimv2')
    Local $objSet = $objWMI.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration')
    If @error Or IsObj($objSet) = 0 Or $objSet.Count = 0 Then Return -2
    ;
    For $objItem In $objSet
        Local $aIPAddress = $objItem.IPAddress
        If IsArray($aIPAddress) And $aIPAddress[0] <> '' Then
            Return $aIPAddress[0]
        EndIf
    Next
    Return -3
EndFunc
;
Func _ObjErrorHandler($_objError)
    $_objError.Clear
    Return SetError(1)
EndFunc
;

 

You might also be interested in this, which is great for working with WMI...

https://www.autoitscript.com/forum/topic/139323-wmi-query-v104/
 

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Bilgus

You typically assign a variable to another variable if you need to keep the old value or if the variable you are grabbing is a really long name and you use it often

That makes the code a bit cleaner to read

So Earthshine Got you on the rough outline of this snippet 

Local $IPConfig
$strComputer = "localhost"
$ping = Ping($strComputer, 500) ;1. ping a computer


If $ping Then ; 2. if you get a valid ping back, use WMI to get/set the network configuration
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$IPConfigSet = $objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ") ; See Here https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx

For $IPConfig in $IPConfigSet ; 3. loop through collection $IPConfigSet 
If $IPConfig.IPAddress <> "" Then ;and if IP is not "" then do something.
    For $x = 0 to UBound($IPConfig.IPAddress) - 1 ; Data type: string array; so we need to go through the array to retrieve all the strings 
        ;NOTE the -1 above this is because the arrays are 0 based
        msgbox(0,"Current IP","Your IP address is " & $IPConfig.IPAddress($x))
    Next
EndIf
Next
EndIf

 

Share this post


Link to post
Share on other sites
dmkirkland

Ok, so then my next question is, why the need for going through the array to retrieve all the strings? Shouldn't there be only one response generated by the previous command? I'm referencing the 

On 3/13/2018 at 6:46 PM, Bilgus said:

For $x = 0 to UBound($IPConfig.IPAddress) - 1 ; Data type: string array; so we need to go through the array to retrieve all the strings

(Not trying to be confrontational-just trying to understand :-))

Share this post


Link to post
Share on other sites
dmkirkland

Oh, and what is $IPConfig.IPAddress? Is ".IPAddress" a function of some sort?

On 3/13/2018 at 6:46 PM, Bilgus said:

        msgbox(0,"Current IP","Your IP address is " & $IPConfig.IPAddress($x))

 

Share this post


Link to post
Share on other sites
Bilgus

no its an array try this 

msgbox(0,"Current IP","Your IP address is " & $IPConfig.IPAddress(0))

Share this post


Link to post
Share on other sites
dmkirkland

I guess a better question for me, might be what is an array, in layman's terms? I'm not quite sure I understand an array fully.

Share this post


Link to post
Share on other sites
dmkirkland

Well, except on a basic level that an array contains a group of items. Just wondering what that "group" is in this case and maybe where those "items" are derived?

Share this post


Link to post
Share on other sites
Bilgus

The Interface exposed to autoit is a function but its just a wrapper around the elements

Func IPConfig_IpAddress($iElem)

    Local Const $IPaddress[20] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]

    Return $IPaddress[$iElem]

EndFunc

 

put that function below the current function and place this in place of your other messagebox

msgbox(0,"Current IP","Your IP address is " & IPConfig_IPAddress($x))

and array is basically a group of values

0 is the first value 1 is the second 100 is the 99th value that is if it goes that high

Now in some languages 1 is the first item but in C and Autoit 0 is generally the first item

 

Share this post


Link to post
Share on other sites
Bilgus

WMI returns an array of all the IP addresses on the system arrays can be groups of strings too

$aTest[2] = ["STRING1", "STRING2"]

now behind the scenes its a different story but you need not worry about that

 

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

×