#91919 - 2003-04-17 07:15 PM
COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
Is there a COM or LDAP method into the MS DHCP server? I need to retreive specific MAC addresses from the pool.
i.e. make a function to pass a name or IP and return the MAC address.
Any help?
|
|
Top
|
|
|
|
#91922 - 2003-04-17 08:08 PM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
the problem is, that if the target PC that I wish to get the MAC of, has been off long enough, the arp cache on whatever PC queried, will not have it.
I have found a kludge of a solution..., but there has to be a better way:
code:
$Mac = GetMac('wheelerc_2nd', 'ada06') shell '\\a06\logon\tools\wol.exe ' + $mac
Function GetMac($ip, $dhcpServer) DIM $logs, $found, $file, $arr
$logs=enumdir('\\'+$dhcpServer+'\admin$$\system32\dhcp\','dhcpsrvlog.*',1) $found=0 for each $file in $logs if $found=0 if open(1,$file,2)=0 $line=readline(1) while @error=0 and $found=0 if instr($line,$ip) and (instr($line,'Assign') or instr($line,'renew')) $arr =split($line,',') $GetMac = $arr[ubound($arr)] $found = 1 endif $line=readline(1) loop $=close(1) endif endif next if $GetMac='' exit (1) endif endfunction
|
|
Top
|
|
|
|
#91923 - 2003-04-18 12:07 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
Top
|
|
|
|
#91924 - 2003-04-18 03:02 AM
Re: COM access to MS DHCP server
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
|
|
Top
|
|
|
|
#91925 - 2003-04-18 03:37 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
Sure... just as soon as someone else comes up with it. I don't even know where to begin...
|
|
Top
|
|
|
|
#91927 - 2003-04-18 05:51 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
from what it reads, is that DHCP config is in ADSI, but the lease info isn't...
I guess I'm stuck with what I found in the log files...
|
|
Top
|
|
|
|
#91929 - 2003-04-19 02:13 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
Top
|
|
|
|
#91931 - 2003-04-19 03:06 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
you are the man....
I'll get somthing together Monday
Thanks
|
|
Top
|
|
|
|
#91933 - 2003-04-19 05:19 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I'm working on it... but the damned multi dim array is kicking my butt
|
|
Top
|
|
|
|
#91934 - 2003-04-19 05:59 AM
Re: COM access to MS DHCP server
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Radimus, This would be a perfect application for a HASH or Dictionary object. Each object would be a client with numerous keys that reference the unique data of each client.
|
|
Top
|
|
|
|
#91935 - 2003-04-19 06:09 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
aaarrrggghhhh, you are hurting me.....
|
|
Top
|
|
|
|
#91936 - 2003-04-19 06:30 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
ok.... Here it is, beat it up and see what you think. Suggestions are appreciated. Efficiency is king...
without the optional IP parameter it will return ALL the info for ALL leases. The IP parameter will limit the return to just that IP.
obviously, this requires a MS DHCP server and the dhcpcmd.exe tool from the reskit in @scriptdir... I may change it to so %path% would be sufficient, but that is no big deal http://www.dynawell.com/reskit/microsoft/win2000/dhcpcmd.zip
code:
break on
$return = EnumMAC('192.168.110.2', '192.168.110.0','192.168.110.12') for $x=0 to ubound($return,2) ? 'IP: ' + $return[0,$x] ? 'Host: ' + $return[1,$x] ? 'MAC: ' + $return[2,$x] next
Function EnumMAC($server, $subnet, Optional $ip) DIM $DHCPCMD, $command, $line, $x, $addr, $host, $mac DIM $arrTemp[2,0]
$x = 0 $command = 'cmd /c '+@scriptdir+'\dhcpcmd.exe '+ $server +' enumclients ' + $subnet+' -h' if $ip $command = $command + ' | find "' + $ip + '"' endif $output = WSHPipe($command,1)
For each $line in $output if val(left($line,1)) $arrTemp[0,$x] = trim(substr($line,4,15)) $arrTemp[1,$x] = split(trim(substr($line,21,16)),'.')[0] $arrTemp[2,$x] = trim(substr($line,37,16)) $x = $x +1 redim preserve $arrTemp[2,$x] endif next if $x redim preserve $arrTemp[2,$x-1] $EnumMAC = $arrTemp else exit (1) endif EndFunction ;******************************************************************************************** Function WshPipe($ShellCMD, OPTIONAL $NoEcho) Dim $WshShell, $oExec, $AllOutput, $Exit, $WshExitCode $WshErrorMsg="" $WshShell=CreateObject("WScript.Shell") $oExec=$WshShell.Exec($ShellCMD) While $Exit<>1 Dim $Output Select Case Not $oExec.StdOut.AtEndOfStream $Output=$oExec.StdOut.ReadAll Case Not $oExec.StdErr.AtEndOfStream $Output=$oExec.StdErr.ReadAll $WshErrorMsg = $Output Case 1 $Output=-1 EndSelect If $Output=-1 If $oExec.Status=1 $Exit=1 Endif Else If $NoEcho<>1 ? $Output Endif $AllOutput = $AllOutput + $Output Endif Loop $WshExitCode=$oExec.ExitCode $WshPipe=split($AllOutput,chr(10)) Exit($WshExitCode) EndFunction
I'll eventually post this over to UDF after if gets beaten up a while.
Jens, this might be a nice addon to KSMS [ 19. April 2003, 06:32: Message edited by: Radimus ]
|
|
Top
|
|
|
|
#91937 - 2003-04-19 07:14 AM
Re: COM access to MS DHCP server
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
first change I need to do to this is to remove the subnet portion, and have it automagically enum all the subnets that may be on the server.
It may be possible to enum all the DHCP servers or accept multiple servers as an array, but that shouldn't be a big issue.
And I possibly could detect the local dhcp server, but this has just as much potential for remote servers as local servers, so that isn't very important
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 494 anonymous users online.
|
|
|