#165926 - 2006-08-15 11:38 AM
EnumKey() malfunction when operating on remote computers
|
OldFart
Fresh Scripter
Registered: 2005-06-28
Posts: 13
Loc: Marburg, Hessen, Germany
|
Hi.
While implementing a script for remote network adapter check purposes I stumbled over a misbehaviour which looks like a bug in EnumKey() to me. To be concise: EnumKey() DOES enumerate all keys beneath a given key for the registry on the LOCAL machine. It DOES NOT enumerate all keys beneath a given key for the registry on a REMOTE machine. Can be reproduced using KIX 4.50, 4.51, 4.52 and with different local/remote computer combinations available at my site.
The actual code and behaviour (warning: talkative text ahead):
Currently the script emumerates all NICs in the registry of the local or a remote machine and prints them to the console. When I enumerate the NICs (available as distinct registry keys beneath HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards) locally everything works fine. When I enumerate NICs from a remote machine, only one NIC (only one registry key) shows up.
OK. Execute the script locally on a machine named FOO_MACHINE:
-- snip ---
; Here we are C:\>echo %COMPUTERNAME% FOO_MACHINE
; Enumerate local registry keys C:\>C:\KIX32_452.EXE C:\QueryNIC.kix
Delivered Subkey: 8 Delivered Subkey: 9 Delivered Subkey: -- Adapter --- HP NC7782 Gigabit Server Adapter {AF6F0242-0F79-4199-94BC-DFD56B323EBB} -- Adapter --- HP NC7782 Gigabit Server Adapter {FC3E6B8A-00E7-4619-B633-6348CAD01143}
C:\>
-- snap ---
Fine, two NICs found. Thats right. Now move to a different machine (named BAR_MACHINE) execute the script there and aim at our previous, the target machine named FOO_MACHINE:
-- snip ---
; Here we are C:\>echo %COMPUTERNAME% BAR_MACHINE
; Now enumerate remote registry keys C:\>C:\KIX32_452.EXE C:\QueryNIC.kix $MACHINENAME=FOO_MACHINE
Delivered Subkey: 9 Delivered Subkey: -- Adapter --- HP NC7782 Gigabit Server Adapter {FC3E6B8A-00E7-4619-B633-6348CAD01143}
C:\>
-- snap ---
Huh ? Something wrong. Are the keys visible and accessible from here ? Lets verify using REG.EXE
-- snip ---
; Here we are C:\>echo %COMPUTERNAME% BAR_MACHINE
; Query the remote registry C:\>reg query "\\FOO_MACHINE\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards" /s
! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\8 ServiceName REG_SZ {AF6F0242-0F79-4199-94BC-DFD56B323EBB} Description REG_SZ HP NC7782 Gigabit Server Adapter
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\9 ServiceName REG_SZ {FC3E6B8A-00E7-4619-B633-6348CAD01143} Description REG_SZ HP NC7782 Gigabit Server Adapter
C:\>
-- snap ---
All requested keys are there and can be seen from remote. And finally the script 'QueryNIC.kix' that fails to work properly. Definitly too bulky as a simple prove code, but 100% real life script. The significant line ist the only EnumKey()line.
-- snip ---
GLOBAL $NULLSTRING GLOBAL $NONE $NULLSTRING = '' $NONE = 0
if isdeclared( $MACHINENAME ) if len( $MACHINENAME ) > 0 $MACHINENAME = lcase( $MACHINENAME ) else $MACHINENAME = lcase( @wksta ) endif else GLOBAL $MACHINENAME $MACHINENAME = lcase( @wksta ) endif
GLOBAL $ADAPTER[ 99 , 1 ] ; 100 Zeilen (0..99), 2 Spalten (0..1) GLOBAL $ANZAHL_ADAPTER GLOBAL $SP_NAME GLOBAL $SP_DESCR $ANZAHL_ADAPTER = 0 $SP_DESCR = 0 $SP_NAME = 1
GLOBAL $ADAPTER_REGPATH $ADAPTER_REGPATH = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards'
dim $current_subkey dim $index_adapter $current_subkey = $NULLSTRING $index_adapter = 0
do
$current_subkey = enumkey( $ADAPTER_REGPATH , $index_adapter )
; Print for debugging only, reveals failure of 'enumkey' for remote machines ? 'Delivered Subkey: $current_subkey'
if @error = $NONE
$temp = '\\' + $MACHINENAME + '\' + $ADAPTER_REGPATH + '\' + $current_subkey
$ADAPTER[ $index_adapter , $SP_DESCR ] = readvalue( $temp , 'Description' ) $ADAPTER[ $index_adapter , $SP_NAME ] = readvalue( $temp , 'ServiceName' )
$index_adapter = $index_adapter + 1
endif
until @error <> $NONE
$ANZAHL_ADAPTER = $index_adapter
; Some more code to be added in the future here ... for now just print results:
$index_adapter = 0
do
? '-- Adapter ---' ? $ADAPTER[ $index_adapter , $SP_DESCR ] ? $ADAPTER[ $index_adapter , $SP_NAME ]
$index_adapter = $index_adapter + 1
until $ANZAHL_ADAPTER = $index_adapter
-- snap ---
|
|
Top
|
|
|
|
#165928 - 2006-08-15 03:33 PM
Re: EnumKey() malfunction when operating on remote computers
|
OldFart
Fresh Scripter
Registered: 2005-06-28
Posts: 13
Loc: Marburg, Hessen, Germany
|
Thank you for the suggestion. Sadly, this is not sufficient. I face some major infrastructure network changes in our network. So I have to change IP settings (DNS, WINS, etc.) on a quite large scale for lots of computers that are not feed by DHCP. The main goal for this script is to CHANGE settings on target machines. Finding the current NIC settings is only the beginning ... Therefore I need reliable read/write access the remote registries of the target machines. And yes, I know of netsh.exe and reg.exe , but if I decide to use these tools I need some wrapper shell script around them anyway to plumb it all together: netsh for querying the current settings, parsing the result by some means (awk or whatever ...) , refeeding the changes to netsh or reg, and so on. So I prefer doing it ALL unsing KIX.
And if it is a bug, it should be fixed anyway ...
|
|
Top
|
|
|
|
#165929 - 2006-08-15 03:53 PM
Re: EnumKey() malfunction when operating on remote computers
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Code:
And if it is a bug, it should be fixed anyway ...
True.
However if you are going to be making changes you should probably look at using WMI to read/write.
|
|
Top
|
|
|
|
#165931 - 2006-08-15 05:47 PM
Re: EnumKey() malfunction when operating on remote computers
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Quote:
Thank you for the suggestion. Sadly, this is not sufficient. I face some major infrastructure network changes in our network. So I have to change IP settings (DNS, WINS, etc.) on a quite large scale for lots of computers that are not feed by DHCP. The main goal for this script is to CHANGE settings on target machines. Finding the current NIC settings is only the beginning ... Therefore I need reliable read/write access the remote registries of the target machines. And yes, I know of netsh.exe and reg.exe , but if I decide to use these tools I need some wrapper shell script around them anyway to plumb it all together: netsh for querying the current settings, parsing the result by some means (awk or whatever ...) , refeeding the changes to netsh or reg, and so on. So I prefer doing it ALL unsing KIX.
And if it is a bug, it should be fixed anyway ...
Wouln't it be easier to have the loginscript process everything and log it to an ini file, compare the settings to what it should be and then have the loginscript do the settings for you? This way you evade this "bug" and you'd only have to sit back and relax.
Unless they aren't connected to the network anymore due to the IP changes. [edit] Then again you wouln't remotely be able to connect to them either... [/edit]
Edited by apronk (2006-08-15 05:57 PM)
|
|
Top
|
|
|
|
#165933 - 2006-08-16 07:34 AM
Re: EnumKey() malfunction when operating on remote computers
|
OldFart
Fresh Scripter
Registered: 2005-06-28
Posts: 13
Loc: Marburg, Hessen, Germany
|
To Allen & Richard:
Thank you for the workaround suggenstions. Being in a hurry for the given task, I already crufted together an ugly shellscript involving several commandline tools (reg, awk, choice, etc.) by glueing them together with some text parsing to handle piping proper arguments between each other. So far, the current job regarding IP settings is done. But the EnumKey() problem is still waiting ...
|
|
Top
|
|
|
|
#165934 - 2006-08-16 07:45 AM
Re: EnumKey() malfunction when operating on remote computers
|
OldFart
Fresh Scripter
Registered: 2005-06-28
Posts: 13
Loc: Marburg, Hessen, Germany
|
This requires admin rights (local admin at least) for the user loggin in. This isn't true for approx. 2990 out of my approx. 3000 Users. Since I do a lot of system management by a set of really bulky loginscripts (just as you suggested), I stumbled over this specific limitation way to often. I learned to ask "can this be done by ordinary users ?" before I do anything else ... And switching user context to a admin user by means of runas is no real option. You run into several difficulties as how to hide the password, changed %TEMP% due to the different user profile, etc.. Quite insecure and unpleasant.
|
|
Top
|
|
|
|
#165936 - 2006-08-16 07:59 AM
Re: EnumKey() malfunction when operating on remote computers
|
OldFart
Fresh Scripter
Registered: 2005-06-28
Posts: 13
Loc: Marburg, Hessen, Germany
|
Uh, oh. Silly me. You are right. Sorry, sorry.
I accidentially enumerate locally and use the received locally valid subkeys for remote access later on. This is no good ...
Sheer coincidence that the subkey "9" exists on both machines. This lead me to inappropriate interpretation of the results. Hrrrrrgnnnnh. :-(
Thank you so much - especially for reading my code to the very detail. I will fix it.
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|