Discussion:
DHCP and VBScripting
(too old to reply)
James Tee Stain
2005-07-13 20:13:32 UTC
Permalink
HiYa,

Anyone got any quick links or coding to access the IP address of a reserved
machine (server, router, printer, RIB card etc) in a Windows 2000 DHCP
server using VB Scripting.

I can only find C++ stuff which is leagues above my head and also banned
(anyway) for security reasons, as the code needs to be transparent to anyone
who wishes to check it.

TIA.

James.
Marty List
2005-07-13 20:44:58 UTC
Permalink
I've used dhcpobjs.dll for this in the past, it was very simple. I think
it's in the Windows 2000 Server Resource Kit tools, but I'm not sure. I
have VB code I could share with you, but not VBScript.

You could also try netsh.exe, I think it will do what you want. You might
need to add the DHCP helper like this:

C:\>netsh.exe
netsh>add helper dhcpmon.dll
Ok.

netsh>dhcp
netsh dhcp>?
list - Lists all the commands available.
dump - Dumps configuration to a text file.
help - Displays help.
? - Displays help.
add - Adds a configuration entry to a table.
delete - Deletes a configuration entry from a table.
show - Displays information.
server [\\servername/ipaddress] - To switch the context to the server
specified.
A Null implies the local machine.


netsh dhcp>server 10.10.10.1
netsh dhcp server>?
list - Lists all the commands available.
dump - Dumps configuration to a text file.
help - Displays help.
? - Displays help.
add - Adds a configuration entry to a table.
delete - Deletes a configuration entry from a table.
export - Exports the configuration to a file.
import - Imports the configuration from a file.
initiate - Initiates an operation.
set - Sets configuration information.
show - Displays information.
scope <scope-ip-address> - To switch to the scope identified by
the IP Address.
mscope <mscope-name> - To switch to the mscope identified by
the MScope name.


netsh dhcp server>list
list - Lists all the commands available.
dump - Dumps configuration to a text file.
help - Displays help.
? - Displays help.

add class - Adds a class to the server.
add mscope - Adds a multicast scope to the server.
add optiondef - Adds a new option to the server.
add scope - Adds a scope to the Server.

delete class - Deletes a specific class from the server.
delete dnscredentials - Sets the credentials to use for DNS
updates
delete mscope - Deletes a multicast scope from the Server.
delete optiondef - Deletes an option from the Server.
delete optionvalue - Deletes an option value from the server.
delete scope - Deletes a scope from the Server.
delete superscope - Deletes a superscope from the Server.

export - Exports configuration of the server.

import - Imports configuraiton of the server.

initiate auth - Initiates retry authorization with the
server.
initiate reconcile - Checks and reconciles the database for all
scopes under the server.

set auditlog - Sets the audit log parameters for the
server.
set databasebackupinterval - Sets the database backup interval of the
current server.
set databasebackuppath - Sets the database backup path for the
Server.
set databasecleanupinterval - Sets the database clean up interval.
set databaseloggingflag - Sets/resets the database logging flag.
set databasename - Sets the name of the server database file.
set databasepath - Sets the path of the server database file.
set databaserestoreflag - Sets/Resets the database restore flag.
set detectconflictretry - Sets the number of conflict detection
attempts
by the DHCP server.
set dnscredentials - Sets the credentials to use for DNS updates
set dnsconfig - Sets the Dynamic DNS configuration for the
Serve
r.
set optionvalue - Sets the global option value for the Server.
set server - Sets the current server in the Server mode.
set userclass - Sets the global user class name for
subsequent operation.
set vendorclass - Sets the global vendor class name for
subsequent operation.

show all - Displays all information for the Server.
show auditlog - Displays the Audit log settings for the
server.
show bindings - Displays Binding information for the server.
show class - Displays all available classes for the
Server.
show detectconflictretry - Displays the detect conflict retry settings.
show dnsconfig - Displays the dynamic DNS configuration for
the
server.
show dnscredentials - Displays the currently set DNS credentials.
show mibinfo - Displays MIBInfo for the Server.
show mscope - Displays all multicast scopes for the
Server.
show optiondef - Displays all options for the Server.
show optionvalue - Displays all optionvalues that are set for
the
server.
show scope - Displays all available scopes under the
Server.
show server - Displays the current Server.
show dbproperties - Displays server database configuration
information.
show serverstatus - Displays the current status for the Server.
show userclass - Displays the currently set user class name.
show vendorclass - Displays the currently set vendor class
name.
show version - Displays the current version of the Server.

scope <scope-ip-address> - To switch to the scope identified by
the IP Address.
mscope <mscope-name> - To switch to the mscope identified by
the MScope name.
Post by James Tee Stain
HiYa,
Anyone got any quick links or coding to access the IP address of a reserved
machine (server, router, printer, RIB card etc) in a Windows 2000 DHCP
server using VB Scripting.
I can only find C++ stuff which is leagues above my head and also banned
(anyway) for security reasons, as the code needs to be transparent to anyone
who wishes to check it.
TIA.
James.
James Tee Stain
2005-07-14 15:00:24 UTC
Permalink
Post by Marty List
I've used dhcpobjs.dll for this in the past, it was very simple. I think
it's in the Windows 2000 Server Resource Kit tools, but I'm not sure. I
have VB code I could share with you, but not VBScript.
You could also try netsh.exe, I think it will do what you want. You might
<SNIP>

Thanks, Marty, I'll certainly look into those 2 and see what's what. I've
noticed netsh.exe before on the Microsoft site, but not dhcpobjs.dll.

I'm not sure on the VB code, as we are in a secure enviroment and if I write
that out It'll need to be approved by the technical architecture guys (I'm
sys amin/tech support and I don't like the idea of them laughing at my code)
and any customer, and that'll take a few weeks :-0 and any minor versions
will need to be approved as well. And, no, they don't have time to write
out stuff themselves - even I'll agree they're hard working chaps.

VBScript is ideal, as I'm only needing a read only program, Type in the IP
address and extract the MAC address.

....every little time saving second helps.

Cheers

James
James Whitlow
2005-07-14 17:02:10 UTC
Permalink
Post by James Tee Stain
VBScript is ideal, as I'm only needing a read only program, Type in the IP
address and extract the MAC address.
If you are just looking for a quick way to get a MAC address for a
particular IP address, consider combining PING with ARP:

ping -n 1 -w 250 10.1.1.1>NUL: && arp -a 10.1.1.1

You can wrap a VBScript around this:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim IP, MAC, oExec, RegEx, WSH, x
Set RegEx = New RegExp
Set WSH = CreateObject("WScript.Shell")

If InStr(1, WScript.FullName, "wscript", 1) Then
WSH.Run "cscript.exe " & WScript.ScriptFullName, 0, False
WScript.Quit
End If

IP = InputBox("Type the IP Address")
RegEx.Pattern = "^.*(([0-9a-fA-F]{2}-){5}[0-9a-fA-F]{2}).*$"

If Len(IP) = 0 Then WScript.Quit

Set oExec = WSH.Exec("%comspec% /c ping -n 1 -w 250 " _
& IP & ">NUL:&&arp -a " & IP)

Do While oExec.Status = 0
WScript.Sleep 100
Loop

Do Until oExec.StdOut.AtEndOfStream
x = oExec.StdOut.ReadLine
If RegEx.Test(x) Then MAC = RegEx.Replace(x, "$1")
Loop

If Not RegEx.Test(MAC) Or oExec.ExitCode Then
MAC = "MAC Address not found."
End If

MsgBox MAC
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
James Tee Stain
2005-07-14 19:23:30 UTC
Permalink
Post by James Whitlow
Post by James Tee Stain
VBScript is ideal, as I'm only needing a read only program, Type in the IP
address and extract the MAC address.
If you are just looking for a quick way to get a MAC address for a
ping -n 1 -w 250 10.1.1.1>NUL: && arp -a 10.1.1.1
It has to come straight from the DHCP server - example of problem: A
manager phones up from Deadwood City - can we immediately have an audit of
all printers attached to the their network. Easy enough. However, for a
full audit we need the IP/MAC addresses from the DHCP server as well to
what's on the Print server - usually doesn't match up either!

This used to work fine, but we have been get very clumsy Techs, lately, who
forget to delete reservations when printers are replaced, change IPs, put
wrong MACs in...

Microsoft DHCP server is not the greatest of programs for any type of
auditing. We have hundreds of offices on each of the 4 different DHCP
servers for redundency.

A VB script where we can put an IP address in and come out with a MAC
address would be nice :-)

James
ItsMillerTime4u
2005-07-14 18:46:18 UTC
Permalink
I did this using the DHCP objects. Look for


Dhcpobjs.exe - DHCP Objects DHCP Objects is an automation
object-model library for the Microsoft® DHCP server. It allows
administrators and developers to write applications and scripts to
administer a DHCP server remotely. It provides enhanced capabilities
over Dhcpcmd.exe (a tool included in the Microsoft® Windows® 2000
Support Tools), such as the ability to remove a DHCP lease.



I believe it was included in the Windows® 2000 Support Tools, but you
still have to install it before it register the DHCP COM object. Once
installed you can connect to any DHCP's server subnet and view its
reservations and their details. The is a pretty detailed .chm bundled
with the COM object.
James Tee Stain
2005-07-14 19:28:45 UTC
Permalink
"ItsMillerTime4u" <***@yahoo.com> wrote in message news:***@g49g2000cwa.googlegroups.com...
I did this using the DHCP objects. Look for

<SNIP>

Cheers, I don't have the Win 2000 support tools at home, but I'll view them
at work on Monday.

James
ItsMillerTime4u
2005-07-14 19:43:59 UTC
Permalink
I just tried to run my script on a mchine after installing the DHCP COm
object, and it didn't work it looks like you have to register the dll
after the install, fo some reason it doesn't do this as part of the
install. Anyways here's the code I use in my script. Please note I
have multiple DHCP servers, because of this I created a group in AD for
them and placed them all in there. The script goes to this group and
ping each of the members, if they're online then it connects to their
DHCP database asking them for their info. You could also put the DHCP
servers in an Array if you want instead, or id you have only one DHCP
then this is not necessary. Code below:


Set rootDSE = GetObject("LDAP://RootDSE")
domainContainer = rootDSE.Get("defaultNamingContext")
Set DhcpMgr = CreateObject("Dhcp.Manager")
Set objShell = CreateObject("Wscript.Shell")
Set objGroup = GetObject("LDAP://cn=DHCP Servers,ou=Server Groups," &
domainContainer)
Set mList = objGroup.members
For Each Member In mList
DHCPServer = Left(Member.sAMAccountName, 10)
Set objExec = objShell.Exec("cmd /c ping " & DHCPServer)
strText = objExec.StdOut.ReadLine()
strText = objExec.StdOut.ReadLine()
strText = objExec.StdOut.ReadLine()
strText = objExec.StdOut.ReadLine()
strText = objExec.StdOut.ReadLine()
strText = objExec.StdOut.ReadLine()
strText = objExec.StdOut.ReadLine()
If InStr(strText, "Reply") > 0 Then
strCount = InStr(strText, ":")
If strCount = "23" Then
DHCPSrvrIP = Mid(strText, 12, 11)
ElseIf strCount = "24" Then
DHCPSrvrIP = Mid(strText, 12, 12)
ElseIf strCount = "25" Then
DHCPSrvrIP = Mid(strText, 12, 13)
End If
WScript.Echo "Starting - " & DHCPSrvrIP & " - " & DHCPServer
Set DhcpSrvr = DhcpMgr.Servers.Connect(DHCPSrvrIP)
With DhcpMgr.Servers.Connect(DHCPSrvrIP).Scopes
For a = 1 To .Count
With DhcpSrvr.Scopes(.Item(a).Address).Reservations
For b = 1 To .Count
WScript.Echo .Item(b).Name
wscript.Echo .Item(b).Address
wscript.Echo Trim(.Item(b).UniqueID)
Next
End With
Next
End With
End If
WScript.echo ""
Next



You may have to adjust this part in your script according to you ip
configuration

strCount = InStr(strText, ":")
If strCount = "23" Then
DHCPSrvrIP = Mid(strText, 12, 11)
ElseIf strCount = "24" Then
DHCPSrvrIP = Mid(strText, 12, 12)
ElseIf strCount = "25" Then
DHCPSrvrIP = Mid(strText, 12, 13)
End If


Also please note that this always seems to throw a cscript program
error once the script completes. Error is Microsoft Visual C++ Runtime
Library Error R6025 -pure virtual function call. I don't worry about
this because by the time this error is thrown all the output is
complete, and I haven't figured out a way to avoid this problem from
happening. Let me know if you have any questions.
James Tee Stain
2005-07-14 22:25:58 UTC
Permalink
Post by ItsMillerTime4u
I just tried to run my script on a mchine after installing the DHCP COm
object, and it didn't work it looks like you have to register the dll
after the install, fo some reason it doesn't do this as part of the
install. Anyways here's the code I use in my script. Please note I
have multiple DHCP servers, because of this I created a group in AD for
them and placed them all in there. The script goes to this group and
ping each of the members, if they're online then it connects to their
DHCP database asking them for their info. You could also put the DHCP
servers in an Array if you want instead, or id you have only one DHCP
<snip>

Cheers, will study it over the weekend and try it on Monday at work.

Thanks again to yourself and Marty List, now I know there's a dll available
(which I didn't know about) it's given me something to focus on.

James
g***@gmail.com
2005-07-15 20:55:12 UTC
Permalink
If it has to come from the DHCP server, why don't you just export a
list from the DHCP MMC? It can give you the following information. (I
have X'd out personal information) - I have chosen to export as a csv.
But you can chose tab delimited as well.


Client IP Address,Name,Lease Expiration,Type,Unique ID,Description
xxx.yyy.43.100,dkpmkf99.sample.domain.com,7/23/2005 4:14:12
PM,DHCP,004096a9xxxx,
xxx.yyy.43.101,jeff.sample.domain.com,7/23/2005 4:22:42
PM,DHCP,000b7d16xxxx,
xxx.yyy.43.102,lhf7zd11.sample.domain.com,7/23/2005 4:16:39
PM,DHCP,004096a9xxxx
xxx.yyy.43.103,ldzjyd11.sample.domain.com,7/23/2005 4:25:19
PM,DHCP,004096a9xxxx,
xxx.yyy.43.104,lj26h871.sample.domain.com,7/23/2005 4:08:48
PM,DHCP,004096a9xxxx,
xxx.yyy.43.105,ld46h871.sample.domain.com,7/23/2005 4:09:18
PM,DHCP,004096a9xxxx,
xxx.yyy.43.106,d7blb00b.sample.domain.com,7/22/2005 1:04:31
AM,DHCP,00b0d043xxxx,
xxx.yyy.43.107,dh9lt001.sample.domain.com,7/23/2005 4:15:09
PM,DHCP,004096a9xxxx,
xxx.yyy.43.108,l5qln351.sample.domain.com,7/23/2005 8:02:33
AM,DHCP,0011f522xxxx,
xxx.yyy.43.109,dkpmkg83.sample.domain.com,7/23/2005 4:06:32
PM,DHCP,004096a9xxxx,
xxx.yyy.43.111,d6y5w571.sample.domain.com,7/23/2005 4:14:56
PM,DHCP,004096a9xxxx,
xxx.yyy.43.112,d9dz3801.sample.domain.com,7/23/2005 4:11:35
PM,DHCP,004096a9xxxx,
xxx.yyy.43.113,d6b3y921.sample.domain.com,7/23/2005 4:08:17
PM,DHCP,004096a9xxxx,


Eddie

Loading...