Page 1 of 1 1
Topic Options
#165926 - 2006-08-15 11:38 AM EnumKey() malfunction when operating on remote computers
OldFart Offline
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
#165927 - 2006-08-15 02:41 PM Re: EnumKey() malfunction when operating on remote computers
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
Does this have anything to do with it?

Code:
 ; Now enumerate remote registry keys
C:\>C:\KIX32_452.EXE C:\QueryNIC.kix $MACHINENAME="FOO_MACHINE"



Also, there are a couple of UDFs that will get the nic info for you. You might see if these have the same problem.
_________________________
(... better days ahead)

Top
#165928 - 2006-08-15 03:33 PM Re: EnumKey() malfunction when operating on remote computers
OldFart Offline
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 Offline
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
#165930 - 2006-08-15 04:14 PM Re: EnumKey() malfunction when operating on remote computers
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Shouldn't you be adding $MACHINENAME here too:

$current_subkey = enumkey( '\\' + $MACHINENAME + '\' + $ADAPTER_REGPATH , $index_adapter )

Top
#165931 - 2006-08-15 05:47 PM Re: EnumKey() malfunction when operating on remote computers
Arend_ Moderator Offline
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
#165932 - 2006-08-15 06:42 PM Re: EnumKey() malfunction when operating on remote computers
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
You might take a look... the wrapper as you put it... is built...(around WMI)

EnumNetworkConnections()
GetIPOptions()
SetIPOptions()

{Edit: Shameless plug of my UDFs }
_________________________
(... better days ahead)

Top
#165933 - 2006-08-16 07:34 AM Re: EnumKey() malfunction when operating on remote computers
OldFart Offline
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 Offline
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
#165935 - 2006-08-16 07:50 AM Re: EnumKey() malfunction when operating on remote computers
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
may I jump in and ask.
is this now a malfunction or not?
the current discussion about possible ways to accomplish some tasks is not related to the topic.

Top
#165936 - 2006-08-16 07:59 AM Re: EnumKey() malfunction when operating on remote computers
OldFart Offline
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
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.075 seconds in which 0.038 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org