dmkirkland Posted March 12, 2018 Posted March 12, 2018 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
Moderators JLogan3o13 Posted March 12, 2018 Moderators Posted March 12, 2018 Moved to the appropriate forum, as the DEV forum very clearly states: Quote Do not create AutoIt-related topics here, use AutoIt General Help and Support "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
dmkirkland Posted March 12, 2018 Author Posted March 12, 2018 Sorry. I clearly did not read the rules for the DEV forum.
dmkirkland Posted March 12, 2018 Author Posted March 12, 2018 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.
xCROv Posted March 12, 2018 Posted March 12, 2018 Are you trying to accomplish just setting the IP or just wanting to understand what you have found that is working for you?
dmkirkland Posted March 12, 2018 Author Posted March 12, 2018 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).
Earthshine Posted March 13, 2018 Posted March 13, 2018 (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. ping a computer if you get a valid ping back, use WMI to get/set the network configuration loop through collection $IPConfigSet and if IP is not "" then do something. Edited March 13, 2018 by Earthshine My resources are limited. You must ask the right questions
Earthshine Posted March 13, 2018 Posted March 13, 2018 you might be interested in this too How to Change Your Computer’s IP Address From the Command Prompt so alternative if needed. My resources are limited. You must ask the right questions
dmkirkland Posted March 13, 2018 Author Posted March 13, 2018 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.
dmkirkland Posted March 13, 2018 Author Posted March 13, 2018 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... :-)
ripdad Posted March 13, 2018 Posted March 13, 2018 (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 March 15, 2018 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
Bilgus Posted March 13, 2018 Posted March 13, 2018 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
dmkirkland Posted March 14, 2018 Author Posted March 14, 2018 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 :-))
dmkirkland Posted March 14, 2018 Author Posted March 14, 2018 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))
Bilgus Posted March 14, 2018 Posted March 14, 2018 no its an array try this msgbox(0,"Current IP","Your IP address is " & $IPConfig.IPAddress(0))
dmkirkland Posted March 14, 2018 Author Posted March 14, 2018 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.
dmkirkland Posted March 14, 2018 Author Posted March 14, 2018 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?
Bilgus Posted March 14, 2018 Posted March 14, 2018 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
Bilgus Posted March 14, 2018 Posted March 14, 2018 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now