DHCP Reservation Tool


Reservation Manager – A tool to manage reservations

Reservation Manager:
Reservations are a critical feature in the DHCP server. Reservations’ are generally used for centralized static IP address assignment for critical machines. This way the IP address of the critical machines which serves critical functions in the network will not have dynamically assigned IP address which can change over time. The reservation manager is a tool which will help solve typical reservation-related problems which the network admins might face. More often than not this tool tries to automate problems to recurring customer problems. It tries to solve the following reservation-related problems:
1) Migrating reservations from one DHCP Server to another on specific subnets.
As a general best-practice, reservations must be duplicated across all DHCP servers serving the same subnet. This particular functionality addresses this best-practice. Without this the admin has to manually dump reservations from one server to another. This command takes 3+ arguments. The first argument tells the destination DHCP server IP address to which the reservations must be copied to. The second argument identifies the IP Address of the source DHCP server. The last argument is either ‘all’ or identifies the first scope address on which this operation has to be performed. The third argument can be followed by n number of arguments which identify the different subnet’s to which this operation has to be performed.
Example:
cscript rmanager.vbs Rmanager –migrate 10.0.0.1 11.0.0.1 all
This command will copy all reservations in all scopes from the server 10.0.0.1 to the server 11.0.0.1.
cscript rmanager.vbs Rmanager –migrate 10.0.0.1 11.0.0.1 12.0.0.0 10.0.0.0 11.0.0.0
This command will copy reservations from the scopes 12.0.0.0 10.0.0.0 and 11.0.0.0 from the server 10.0.0.1 to 11.0.0.1.
For this command to work properly the specified scope/scopes should be present in both the servers. An error message will be printed for those scopes for which this condition is not met. Also for those reservations which cannot be added successfully for whatever reason, an error is printed. Also for every successfully migrated reservation, it can be printed.
In addition to this, this command can also take an additional preview switch which will just print the action which will be taken and not actually take the action. This is especially useful when the admin wants to review the action before doing it.
Example:
cscript rmanager.vbs Rmanager –migrate 10.0.0.1 11.0.0.1 all –preview out.txt
cscript rmanager.vbs Rmanager –migrate 10.0.0.1 11.0.0.1 12.0.0.0 10.0.0.0 11.0.0.0 –preview out.txt

2) Synchronizing reservations on 2 DHCP servers on specific subnets.
As already mentioned, it is a general best-practice for reservations must be duplicated across all DHCP servers serving the same subnet. This particular functionality addresses this best-practice in a more subter way. Instead of copying reservations from one server to another, this option lets the admin to synchronize the reservation lists between two servers. The arguments for this switch is exactly similar to that of the migrate switch. This command also has an optional preview switch.
Example:
cscript rmanager.vbs Rmanager –sync 10.0.0.1 11.0.0.1 all
This command will sync all reservations in all scopes from the server 11.0.0.1 to the server 10.0.0.1.
cscript rmanager.vbs Rmanager –sync 10.0.0.1 11.0.0.1 12.0.0.0 10.0.0.0 11.0.0.0
For this command to work properly the specified scope/scopes should be present in both the servers. An error message will be printed for those scopes for which this condition is not met. Also for those reservations which cannot be added successfully for whatever reason, an error is printed. Also for every successfully migrated reservation, it can be printed.
In addition to this, this command can also take an additional preview switch which will just print the action which will be taken and not actually take the action. This is especially useful when the admin wants to review the action before doing it.
Example:
cscript rmanager.vbs Rmanager –sync 10.0.0.1 11.0.0.1 all –preview out.txt
cscript rmanager.vbs Rmanager –sync 10.0.0.1 11.0.0.1 12.0.0.0 10.0.0.0 11.0.0.0 –preview out.txt

3) Dumping leases on specific subnets.
This command dumps the leases for a specific scope and server into a human-editable text file. The file will contains several lines each of which corresponds to a single lease. A lease is represented by IP address and MAC address. The exact utility of this command will be exposed once you see the next section.
Example:
cscript rmanager.vbs Rmanager -dumplease out.txt 120.0.0.1 all
The above command dumps all leases in all scopes in the server 120.0.0.1 to a text file called “out.txt”
cscript rmanager.vbs Rmanager -dumplease out.txt 120.0.0.1 10.0.0.0 11.0.0.0
The above command dumps all leases in scopes 10.0.0.0 and 11.0.0.0 in the server 120.0.0.1 to a text file called “out.txt”

4) MakeReservation switch.
This switch tries to create reservations on a given DHCP server from the information present in a given input file. The input file contains several lines of info. Each line corresponds to a single reservation. Each reservation is represented by IP address and MAC address pair. The format of this input file is exactly the same as the output file of the -dumplease switch.
Example:
cscript rmanager.vbs Rmanager -makereservation 120.0.0.1 out.txt
This command will create reservations out of information present in the file out.txt in the server 120.0.0.1.

Based on the -dumplease and -makereservation switch, some interesting possibilities arise. What is possible is the following scenario: An admin has an active DHCP server which has a couple of scopes. Initially there are no reservations in any of these scopes. Thus all the subnets corresponding to these scopes have dynamic addresses. In the environment any machine which is plugged into the these subnets will get a dynamic IP address from the DHCP server. Now to restrict the access of the network to those machines that are already connected to the network, the admin can do the following.
cscript rmanager.vbs Rmanager -dumplease out.txt 120.0.0.1 all
cscript rmanager.vbs Rmanager -makereservation 120.0.0.1 out.txt
These two commands intends to convert every active lease in a given DHCP server identified by an IP address in a given set of scopes identified by a list of scope addresses into reservations. Once this is done, the admin also has to make sure that the whole address space in the given scope in the DHCP server are excluded. This operation is thus particularly relevant to those admins who might want to move from a pure Dynamic IP address assignment to a more controlled purely reservation oriented address assignment. This way the admin can keep track of who is getting IP addresses and can limit the network access to those clients who already have an IP address.
The first command Rmanager -dumplease <filename> <Scope Identifiers> will dump all the active leases in the specified server and scope to a text file. This text file will have pair of IP address and MAC addresses. The second command Rmanager –makereservation <ServerIdentifier> <filename> will take the file which is generated by the first command and create reservations corresponding to them. The idea behind separating these two actions is to make sure that the admin gets a chance to edit the file generated by the first command before proceeding to the second. Also we want to support scenarios where the admin can just create the file and use it to run the second command directly.

In the attachment (See the link at the end of this post), Find the RManager.vbs .You need to extract the contents of the zip file into a separate folder and run it from there. The zip file should contain two files: rmanager.vbs and help.txt.

 If you are not able to get it, Cut-copy-paste the following into a file named rmanager.vbs. Also copy the contents of the documentation help.txt

Run it by typing “cscript rmanager.vbs”

X———————–Begin Paste———————X

call start()
dim scopes
dim synccount
dim argerror
dim iparray
dim macarray
dim finaliparray
dim finalmacarray
dim m
dim args

sub start()
 
 args=0 
 synccount=0
 argerror=0
 if(wscript.arguments.count=0) then
 wscript.echo “enter the command”
 exit sub
 end if

 for i=0 to wscript.arguments.count-1
 if(i=0) then
 str=wscript.arguments.item(i)
 else
 str=str&” “&wscript.arguments.item(i)
 end if 
 next
 
 str1=split(str)

 ‘show error if nothing is entered————

 if (not isarray(str1)) then
 wscript.echo “Error: unrecongnized or incomplete command line.”
 call helpfile()
 exit sub
 end if


 ‘checking if the command is Rmanager or not

 if(strcomp(“Rmanager”,str1(0))<>0) then
 wscript.echo “Error: unrecongnized or incomplete command line.”
 call helpfile()
 exit sub
 end if
  
 ‘checking for different cases after Rmanager
 if(ubound(str1)=0) then
 wscript.echo “Error: unrecongnized or incomplete command line.”
 call helpfile()
 exit sub
 end if
 select case str1(1)

 case “-migrate”

  call migrate(str1)
  
 case “-sync”
  
  call migrate(str1)
  if(argerror=1) then
  exit sub
  end if
  synccount=1
  a=str1(2)
  str1(2)=str1(3)
  str1(3)=a
  call migrate(str1) 

 case “-dumplease”
  
  call dump(str1)
  
 case “-makereservation”

  call makereservation(str1)
 
 case “/?”
 
  call helpfile()
  
 case else
  wscript.echo “Error: unrecongnized or incomplete command line.”
  call helpfile()
 
  end select
 
‘delete the scopefile that is created during the program 

set fso = createobject(“Scripting.FileSystemObject”)
if(fso.fileexists(“scopefile.txt”))then
fso.deletefile(“scopefile.txt”),true
end if


end sub


‘ function for dumplease

sub dump(str1)

‘if improper arguments are entered show error

if ubound(str1)<4 then       
wscript.echo “Error: unrecongnized or incomplete command line.”
call helpfile()
exit sub
end if

filename = str1(2)
set fso = createobject(“Scripting.FileSystemObject”)
 Set outfile = fso.CreateTextFile(filename)
 outfile.close

p=checkserver(str1(3))

if p=0 then
wscript.echo “server “&str1(3)&” not present or is entered wrongly”
exit sub
end if

serverarray = makescopearray(scopes)

if not isarray(serverarray) then
wscript.echo “no scopes in server “&str1(3)
exit sub
end if

if (strcomp(str1(4),”all”)=0) then

 if(ubound(str1)=4) then
 scope = serverarray
 else
 wscript.echo “improper arguments”
 end if

else

for i = 4 to ubound(str1)
if(isscope(str1(i),serverarray)) then
redim preserve scope(i-4)
scope(i-4)=str1(i)
else
wscript.echo “scope “& str1(i) &” is not present in server “&str1(3)
exit sub
end if
next
end if
‘scope contains all the scopes entered
if not isarray(scope) then
wscript.echo “no scopes in the server “&str1(3)
else
if scope(0)=”” then
wscript.echo “no scopes in the server “&str1(3)
else
 set wshell = CreateObject(“WScript.Shell”)
 
 for i = 0 to ubound(scope)
 set obj = wshell.exec(“netsh dhcp server “&str1(3)&” scope “&scope(i)&” show clients”)
 clients = obj.stdout.readall()
 if(instrrev(clients,”Command completed successfully.”)=0) then
 exit sub
 end if 
 call makeclients(clients,str1(2)) 
 next
end if
end if
if args = 0 then
else
wscript.echo “the ipaddress and macaddress pair are in file “&filename
end if
end sub

‘the following procedure outputs the ip and corresponding mac addresses(active leases) into the file – ‘filename’…it takes the output string clients that we get after executing the command shoe clients

sub makeclients(clients,filename)

 set fso = createobject(“Scripting.FileSystemObject”)
 Set scopefile = fso.CreateTextFile(“scopefile.txt”)
 scopefile.writeline(clients)
 scopefile.close
 
 
 Set scopefile = fso.OpenTextFile(“scopefile.txt”)
   do While not scopefile.AtEndOfStream   ‘reading line by line and checking for the string “Total No. of Scopes”
    
    line = scopefile.ReadLine
   
   if(instr(line,”No of Clients”)=1) then  
   str = split(line)
   
   t=str(4)        ‘taking no. of clients into t
   exit do
   
   end if

   loop   

   scopefile.close
   
   t=cint(t)    

   if t=0 then
   wscript.echo “no clients in the scope “&str(ubound(str))
   
   exit sub
   end if
args=args+1
Set scopefile = fso.OpenTextFile(“scopefile.txt”)
for i= 1 to 8
scopefile.skipline
next     

set outfile = fso.opentextfile(filename,8)

for i=0 to t-1
line=scopefile.readline

str= split(line)

if (instr(line,”INACTIVE”)<>0) then
d=0
 for l=0 to ubound(str)
 if(str(l)=””) then
 else
 if d=3 then
 exit for
 else
 d=d+1 
 end if
 end if
 next
 
outfile.write(str(0))
for size = 1 to 18-(len(str(0)))
outfile.write(” “)
next

outfile.writeline(trim(replace(str(l),”-“,””)))

else
d=0
 for l=0 to ubound(str)
 if(str(l)=””) then
 else
 if d=4 then
 exit for
 else
 d=d+1 
 end if
 end if
 next
outfile.write(str(0))
for size = 1 to 18-(len(str(0)))
outfile.write(” “)
next

outfile.writeline(trim(replace(str(l),”-“,””)))


end if
next
outfile.close
end sub

sub makereservation(str1)

if ubound(str1)<>3 then
wscript.echo “Error: unrecongnized or incomplete command line.”
call helpfile()
exit sub
end if

server = str1(2)
filename = str1(3)

set fso = createobject(“Scripting.FileSystemObject”)
if(not fso.fileexists(filename))then
wscript.echo “file “&filename&” not present”
exit sub
end if

k = checkserver(server)
if k=0 then
wscript.echo “server “&server&” is not present or may have been entered wrongly”
exit sub
end if

scopearray=makescopearray(scopes)       ‘scopes is the output string obtained after executing show scope command for given server

set handle = fso.opentextfile(filename)

Dim ipaddress
Dim macaddress
t=0
do while not handle.atendofstream

 line=trim(handle.readline)
 line=replace(line,” “,””) 
 if line=”” then
 
 else
  strarray=split(line)

  p=0

  for k=0 to ubound(strarray)
   if strarray(k)=”” then
      
   else

    if p=0 then 
    ipaddress=trim(strarray(k))
    else if p=1 then
    macaddress = trim(strarray(k))
    end if
    end if
    p=p+1
    ‘wscript.echo Replace(ipaddress, ” “, “”)
    ‘wscript.echo macaddress
   end if
  next
  
  if p<>2 then
  wscript.echo “improper format of file”
  exit sub
  end if
 
 redim preserve iparray3(t)
 redim preserve macarray3(t)
 iparray3(t)=ipaddress
 macarray3(t)=macaddress
 t=t+1
 end if
 

loop
handle.close

if not isarray(iparray3) then
wscript.echo “there is nothing in the file”
exit sub
end if

dim arr
redim arr(t-1)
 
for i = 0 to ubound(scopearray)
 
set iprange = fso.createtextfile(“iprangefile.txt”)
iprange.close
arraysize = 0
dim iparray1
redim iparray1(0)
redim macarray1(0)

  set wshell = CreateObject(“WScript.Shell”)
 set obj = wshell.exec(“netsh dhcp server “&server&” scope “&scopearray(i)&” show iprange”)
 range = obj.stdout.readall()
 
 set iprange = fso.opentextfile(“iprangefile.txt”,8)
 iprange.writeline(range)
 iprange.close
 set iprange = fso.opentextfile(“iprangefile.txt”)
 
 for p=0 to 5
 iprange.skipline
 next
 line1=iprange.readline
 line2=split(line1)
 
 count=0
 
 for j=0 to t-1
 
 k = checkiprange(line2(3),line2(13),iparray3(j))
 
 if k = 1 then
 
 redim preserve iparray1(arraysize)
 redim preserve macarray1(arraysize)
 iparray1(arraysize) = iparray3(j)
 macarray1(arraysize) = macarray3(j) 
 arraysize = arraysize + 1
 arr(j)=1
 else if k=2 then
 wscript.echo “the ip address “&iparray3(j)&” is not valid” 
 fso.deletefile(“iprangefile.txt”),true
 exit sub
 end if 
 end if
 next 

iprange.close


if isarray(iparray1) and not iparray1(0)=”” then

call addreservation(server,scopearray(i),iparray1,macarray1) 

if (finaliparray(0)=””) then

else
for l=0 to ubound(finaliparray)
set obj = wshell.exec(“netsh dhcp server “&server&” scope ” &scopearray(i)& ” add reservedip “&finaliparray(l)&” “&finalmacarray(l) )
outp =  obj.stdout.readall()
  if(instr(outp,”Command completed successfully.”)<>0) then
  wscript.echo “added reservation “&finaliparray(l)& ” “&finalmacarray(l)&” to server “&server&” in scope “&scopearray(i)
  else
  
  wscript.echo “The specified IP address “&finaliparray(l)& ” or hardware address “& finalmacarray(l)& ” is either not proper or is being used by another client.”
  end if
next
end if

end if
redim iparray1(0)
redim macarray1(0)

next

set handle = fso.opentextfile(filename)
for i=0 to ubound(arr)
str=trim(handle.readline)

if(arr(i)=0) then
line=split(str)

wscript.echo “The ipaddress “&iparray3(i)&” is not present in any scope”
end if
next
handle.close
fso.deletefile(“iprangefile.txt”),true
end sub

sub addreservation(server,scopestr,iparraysource,macarraysource)

 redim finaliparray(0)
 redim finalmacarray(0)
 set wshell = CreateObject(“WScript.Shell”)
 set obj = wshell.exec(“netsh dhcp server “&server&” scope ” &scopestr& ” show reservedip” )
 outpdest = obj.stdout.readall()

 call makeipandmacarray(outpdest)
 iparraydest = iparray
 macarraydest = macarray   
 
 ‘ remove common ip and mac pair between the two servers for the given scope
 
 if(not isarray(iparraydest)) then
  finaliparray=iparraysource 
  finalmacarray=macarraysource 
 
 else
  k=0
  
  for i = 0 to ubound(iparraysource)
  flag=0
   for j=0 to ubound(iparraydest)
    if(iparraysource(i)=iparraydest(j) and macarraysource(i)=macarraydest(j)) then
    flag=1
    exit for
    end if
   next
  
  if flag=1 then
  
   if synccount = 0 and m=0 then
  
  wscript.echo “reservation “&iparraysource(i)&” “&macarraysource(i)&” is already present on server “&server
   end if
  else
  
  redim preserve finaliparray(k) 
  redim preserve finalmacarray(k)
  finaliparray(k)=iparraysource(i)
  finalmacarray(k)=macarraysource(i) 
  k=k+1   
  end if
  next
 end if

end sub

function checkiprange(ip1,ip2,ip3)

str2=split(ip1,”.”)
str3=split(ip2,”.”)
str4=split(ip3,”.”)
if ubound(str4)<>3 then
checkiprange=2
exit function
end if

dim string

if cint(str2(0))<=cint(str4(0)) and cint(str4(0))<=cint(str3(0)) then
 if cint(str2(1))<=cint(str4(1)) and cint(str4(1))<=cint(str3(1)) then
  for i=2 to 3
if(cint(str2(i))>=0 and cint(str2(i))<10) then
string1=string1&”00″&str2(i)
else if(cint(str2(i))>=10 and cint(str2(i))<100) then
string1=string1&”0″&str2(i)
end if
end if
next
for i=2 to 3
if(cint(str3(i))>=0 and cint(str3(i))<10) then
string2=string2&”00″&str3(i)
else if(cint(str3(i))>=10 and cint(str3(i))<100) then
string2=string2&”0″&str3(i)
end if
end if
next


for i=2 to 3
if(cint(str4(i))>=0 and cint(str4(i))<10) then
string3=string3&”00″&str4(i)
else if(cint(str4(i))>=10 and cint(str4(i))<100) then
string3=string3&”0″&str4(i)
end if
end if
next

if(cint(string1)<=cint(string3) and cint(string3)<=cint(string2)) then
checkiprange=1
else
checkiprange=0
end if

 else
 checkiprange=0
 end if
else
checkiprange=0
end if
end function

sub helpfile()

set fso = createobject(“Scripting.FileSystemObject”)
set help = fso.OpenTextFile(“help.txt”)

do while not help.atendofstream

wscript.echo (help.readline)

loop

end sub

‘ this function will return 1 if the scope(scopestr) is present in the given server(serverarray….this is an array of server scopes)(be it source or destination)…else 0

function isscope(scopestr,serverarray)

l=0
for i=0 to ubound(serverarray)
if(strcomp(scopestr,serverarray(i))=0) then
l=1
exit for
end if
next

isscope=l

end function

‘this function will check if the server entered as argument is present or not(or rather entered correctly or not)

function checkserver(str1)
dim wshell

dim pos1

 set wshell = CreateObject(“WScript.Shell”)
 set obj = wshell.exec(“netsh dhcp server “&str1&” show scope”)
    
 scopes = obj.stdout.readall()
 
 pos1=instr(scopes,”Command completed successfully.”)      ‘if the string is present, the command has executed succesfully
 
 if(pos1<>0) then
  checkserver=1
 else
  checkserver=0
 end if
  
end function


  


‘this function takes as argument the output string that we get after the command show scope is executed and returns
‘the array of all scopes present in the server

function makescopearray(scopes)

   set fso = createobject(“Scripting.FileSystemObject”)
   Set scopefile = fso.CreateTextFile(“scopefile.txt”)
   scopefile.writeline(scopes)           ‘writing output string to a file
   scopefile.close
       
   Set scopefile = fso.OpenTextFile(“scopefile.txt”)
   do While not scopefile.AtEndOfStream   ‘reading line by line and checking for the string “Total No. of Scopes”
    
    line = scopefile.ReadLine
   
   if(instr(line,”Total No. of Scopes”)=2) then  
   str = split(line)
   t=str(ubound(str)-1)        ‘taking no. of scopes into t
   exit do
   end if

   loop   

   scopefile.close
   
   t=cint(t)      ‘converting string to integer
    
   if t=0 then      ‘if no scopes are present
   
   makescopearray=””     
   else
      
   Set scopefile = fso.OpenTextFile(“scopefile.txt”)    
   for i=1 to 5
   scopefile.skipline     ‘skipping first five lines
   next    
   for i=0 to t-1
   str3=trim(scopefile.readline)
   
   str3 = split(str3,” “)
       
   redim preserve serverscope(i)
   serverscope(i)=str3(0)     ‘adding scopes to array serverscope
   
   next
   makescopearray = serverscope    ‘returning the array to calling function
   scopefile.close
   end if
end function

 

‘function to take reservations from source server and add to destination server for a given scope

sub reservedip(sourceadd,destadd,scopestr,filename,command)

 dim outp
 set wshell = CreateObject(“WScript.Shell”)
 set obj = wshell.exec(“netsh dhcp server “&sourceadd&” scope ” &scopestr& ” show reservedip” )
 outpsource = obj.stdout.readall()

 call makeipandmacarray(outpsource)
 iparraysource = iparray
 macarraysource = macarray
 
 if(not isarray(iparraysource)) then
 wscript.echo “no reservations in the scope “&scopestr&” on the server “&sourceadd
  exit sub
 end if
 

 if(m=1 or m=2) then

 set fso = createobject(“Scripting.FileSystemObject”)
 Set outfile = fso.openTextFile(filename,8)

 for i=0 to ubound(iparraysource)  
 outfile.writeline(“netsh dhcp server “&destadd& ” scope ” &scopestr& ” add reservedip ” &iparraysource(i)& ” ” & macarraysource(i))
 next
 outfile.close
 exit sub
 end if


call addreservation(destadd,scopestr,iparraysource,macarraysource)

‘now finaliparray and finalmacarray contain only the unique ip and mac addresses

if (not isarray(finaliparray) or finaliparray(0)=””) then
if ((m=1 or m=2)) then
  if (strcomp(command,”-migrate”)=0 or (strcomp(command,”-sync”)=0 and synccount=1)) then
  wscript.echo”output is in file “&filename
  end if 
 end if

 exit sub
 end if

t=ubound(finaliparray)
  
  set wshell = CreateObject(“WScript.Shell”)
  for i=0 to t
  set obj = wshell.exec(“netsh dhcp server “&destadd&” scope ” &scopestr& ” add reservedip ” &finaliparray(i)& ” ” & finalmacarray(i))
  outp =  obj.stdout.readall()
  if(instr(outp,”Command completed successfully.”)) then
  wscript.echo “added reservation “&finaliparray(i)& ” “&finalmacarray(i)&” to server “&destadd&” in scope “&trim(scopestr)
  else
  
  wscript.echo “The specified IP address “&finaliparray(i)& ” or hardware address “& finalmacarray(i)& ” is being used by another client.”
  end if
  
  next
 
 if ((m=1 or m=2)) then
  if (strcomp(command,”-migrate”)=0 or (strcomp(command,”-sync”)=0 and synccount=1)) then
  wscript.echo”output is in file “&filename
  end if 
 end if
end sub

sub makeipandmacarray(outp)


 redim iparray(0)
 redim macarray(0)

 set fso = createobject(“Scripting.FileSystemObject”)
 Set scopefile = fso.CreateTextFile(“scopefile.txt”)
 scopefile.writeline(outp)
 scopefile.close
  
 Set scopefile = fso.openTextFile(“scopefile.txt”)
  
 
 do While not scopefile.AtEndOfStream
 line = scopefile.ReadLine
 if(instr(line,”No of ReservedIPs”)=1) then
 str = split(line)
 t=str(4)         ‘taking no. of reservations into t
 exit do
 end if
 loop   
 
 scopefile.close
 
 if t=0 then
 iparray = “”
 macarray = “”
 exit sub
 
 else
 Set scopefile = fso.openTextFile(“scopefile.txt”)
 
 for i=1 to 7
 scopefile.skipline
 next
 
 for i=1 to t
 redim preserve iparray(i-1)
 redim preserve macarray(i-1)
 string1=split(trim(scopefile.readline))
 
 iparray(i-1)=string1(0)
 macarray(i-1)=replace(string1(ubound(string1)),”-“,””)
 next
 end if
 scopefile.close
 
end sub

sub migrate(str1)

  if ubound(str1)<4 then        ‘ ensures that the command entered has required no. of arguments 
         wscript.echo “Error: unrecongnized or incomplete command line.”
  call helpfile()
  argerror=1
  exit sub
  end if
  dim k     

  k = checkserver(str1(2))&nbsp


Comments (31)

  1. teamdhcp says:

    DHCP server IP address does not seem to be right, please check this.

    Can you also try doing the following on command prompt and share the results.

    "netsh dhcp server 192.168.116.0 show scope"

    Regards,

    Subhash

  2. teamdhcp says:

    Hello Eric,

    I dont know why you get errors, the best I can do is debug your issue if you can give me your dhcp server information about scopes and the leases + your out.txt file. Please forward all these information to msnetworkteam@live.com

    Thanks,

    Subhash Badri

  3. Anonymous says:

    Reservations are a critical feature in the DHCP server. Reservations are generally used for centralized

  4. Anonymous says:

    Any sugguestions why when I do a

    -dumplease

    -makereservation

    I get the message "improper format"

    I am getting a scopefile.txt

    ==============================================================================

    Scope Address  – Subnet Mask    – State        – Scope Name          –  Comment  

    ==============================================================================

    172.22.0.0     – 255.255.0.0    -Active        -CRN-Domian           –              

    Total No. of Scopes = 1

    Command completed successfully.

    Thanks for your attention to this matter,

    Greg Porter

  5. Anonymous says:

    Line 393 needs to be changed to

    k = checkiprange(line2(3),line2(14),iparray3(j))

    line2(13) is an empty location in the array

    A line needs to be added after line 529

    Else string1=string1&str2(i)

    The original script did not append any values higher than 99 to the string, leading to false range failures.

    The same change needs to be added after the other strings

    after Line 537

    Else string2=string2&str3(i)

    after line 547

    Else string3=string3&str4(i)end if

    Good tool after I fixed it though. 🙂

  6. teamdhcp says:

    hello Eric,

    We have not recieved any mail from you to msnetworkteam@live.com account. Can you please re-send.

    Thanks,

    Subhash

  7. teamdhcp says:

    Hello,

    Replacing cint with cLng should solve your problem.

    Anyways the parameters passed to checkiprange will not passed correct for varying ips eg. line2(13) may not be correct for the range with ips as 123.123.123.200 – 123.123.123.201, in such a scenario line2(13) may not exist at all.

    To make it more accurate I have added another for loop which skips the white space. Any VB script expert can comment on this

    Do the following:

    1. replace the makereservation with the added sub routine.

    2. replace the checkiprange with the added funtion.

    ———- makereservation ————

    sub makereservation(str1)

           if ubound(str1)<>3 then

               wscript.echo "Error: unrecongnized or incomplete command line."

               call helpfile()

               exit sub

           end if

           server = str1(2)

           filename = str1(3)

           set fso = createobject("Scripting.FileSystemObject")

           if(not fso.fileexists(filename))then

               wscript.echo "file "&filename&" not present"

               exit sub

           end if

           k = checkserver(server)

           if k=0 then

               wscript.echo "server "&server&" is not present or may have been entered wrongly"

               exit sub

           end if

           scopearray=makescopearray(scopes)       ‘scopes is the output string obtained after executing show scope command for given server

           set handle = fso.opentextfile(filename)

           Dim ipaddress

           Dim macaddress

           t=0

           do while not handle.atendofstream

               line=trim(handle.readline)

               if line="" then

               else

                   strarray=split(line)

                   p=0

                   for k=0 to ubound(strarray)

                       if strarray(k)="" then

                       else

                           if p=0 then

                               ipaddress=trim(strarray(k))

                           else

                               if p=1 then

                                   macaddress = trim(strarray(k))

                               end if

                           end if

                           p=p+1

                       end if

                   next

                   if p<>2 then

                       wscript.echo "p = "

                       wscript.echo p

                       wscript.echo "improper format of file"

                       exit sub

                   end if

                   redim preserve iparray3(t)

                   redim preserve macarray3(t)

                   iparray3(t)=ipaddress

                   macarray3(t)=macaddress

                   t=t+1

               end if

           loop

           handle.close

           if not isarray(iparray3) then

               wscript.echo "there is nothing in the file"

               exit sub

           end if

           dim arr

           redim arr(t-1)

           for i = 0 to ubound(scopearray)

               set iprange = fso.createtextfile("iprangefile.txt")

               iprange.close

               arraysize = 0

               dim iparray1

               redim iparray1(0)

               redim macarray1(0)

               redim IP(6)

               dim x

               set wshell = CreateObject("WScript.Shell")

               set obj = wshell.exec("netsh dhcp server "&server&" scope "&scopearray(i)&" show iprange")

               range = obj.stdout.readall()

               set iprange = fso.opentextfile("iprangefile.txt",8)

               iprange.writeline(range)

               iprange.close

               set iprange = fso.opentextfile("iprangefile.txt")

               for p=0 to 5

                   iprange.skipline

               next

               line1=iprange.readline

               line2=split(line1)

               count=0

               for j=0 to t-1

                   x=1

                   for n=1 to ubound(line2)

                       if line2(n) = "" then

                       else

                           IP(x) = line2(n)

                           x=x+1

                       end if

                   next

                   k = checkiprange(IP(1),IP(3),iparray3(j))

                   if k = 1 then

                       redim preserve iparray1(arraysize)

                       redim preserve macarray1(arraysize)

                       iparray1(arraysize) = iparray3(j)

                       macarray1(arraysize) = macarray3(j)

                       arraysize = arraysize + 1

                       arr(j)=1

                   else if k=2 then

                       wscript.echo "the ip address "&iparray3(j)&" is not valid"

                       fso.deletefile("iprangefile.txt"),true

                       exit sub

                   end if

                   end if

               next

               iprange.close

               if isarray(iparray1) and not iparray1(0)="" then

                   call addreservation(server,scopearray(i),iparray1,macarray1)

                   if (finaliparray(0)="") then

                   else

                       for l=0 to ubound(finaliparray)

                           set obj = wshell.exec("netsh dhcp server "&server&" scope " &scopearray(i)& " add reservedip "&finaliparray(l)&" "&finalmacarray(l) )

                           outp =  obj.stdout.readall()

                           if(instr(outp,"Command completed successfully.")<>0) then

                               wscript.echo "added reservation "&finaliparray(l)& " "&finalmacarray(l)&" to server "&server&" in scope "&scopearray(i)

                           else

                               wscript.echo "The specified IP address "&finaliparray(l)& " or hardware address "& finalmacarray(l)& " is either not proper or is being used by another client."

                           end if

                       next

                   end if

               end if

               redim iparray1(0)

               redim macarray1(0)

           next

           set handle = fso.opentextfile(filename)

           for i=0 to ubound(arr)

               str=trim(handle.readline)

               if(arr(i)=0) then

                   line=split(str)

                   wscript.echo "The ipaddress "&iparray3(i)&" is not present in any scope"

               end if

           next

           handle.close

           fso.deletefile("iprangefile.txt"),true

       end sub

    —————————————

    ———- CheckIPRange —————

       function checkiprange(ip1,ip2,ip3)

           str2=split(ip1,".")

           str3=split(ip2,".")

           str4=split(ip3,".")

           if ubound(str4)<>3 then

               checkiprange=2

               exit function

           end if

           dim string

           if cint(str2(0))<=cint(str4(0)) and cint(str4(0))<=cint(str3(0)) then

               if cint(str2(1))<=cint(str4(1)) and cint(str4(1))<=cint(str3(1)) then

                   for i=2 to 3

                       if(cint(str2(i))>=0 and cint(str2(i))<10) then

                           string1=string1&"00"&str2(i)

                       else if(cint(str2(i))>=10 and cint(str2(i))<100) then

                               string1=string1&"0"&str2(i)

                           else string1=string1&str2(i)

                           end if

                       end if

                   next

                   for i=2 to 3

                       if(cint(str3(i))>=0 and cint(str3(i))<10) then

                           string2=string2&"00"&str3(i)

                       else if(cint(str3(i))>=10 and cint(str3(i))<100) then

                               string2=string2&"0"&str3(i)

                           else string2=string2&str3(i)

                           end if

                       end if

                   next

                   for i=2 to 3

                       if(cint(str4(i))>=0 and cint(str4(i))<10) then

                           string3=string3&"00"&str4(i)

                       else if(cint(str4(i))>=10 and cint(str4(i))<100) then

                               string3=string3&"0"&str4(i)

                           else string3=string3&str4(i)

                           end if

                       end if

                   next

                   if(cLng(string1)<=cLng(string3) and cLng(string3)<=cLng(string2)) then

                       checkiprange=1

                   else

                       checkiprange=0

                   end if

               else

                   checkiprange=0

               end if

           else

               checkiprange=0

           end if

       end function

    —————————————

    mail me if you have any more issues & queries.

    Thanks,

    Subhash Badri

  8. Anonymous says:

    Hey… tool worked great at syncing all reservations made between two DHCP servers, same subnet.  Issue is, the reservations didn’t keep their names and descriptions… they are there, but blank (just IP and MAC).  Is there a way to sync and keep this info intact?  Should I just run a ‘migration’ instead of a ‘sync’?

  9. teamdhcp says:

    Your query seems to have been already answered by "ronsdavis".

    Please check it, if it resolves your issue and get back if it does not.

    Regards,

    SB

  10. teamdhcp says:

    I have modified the tool with some prints for debugging and made the tool working. I am even pasting the out.txt just to make sure you are writing the out.txt in the proper format.

    call start()

    dim scopes

    dim synccount

    dim argerror

    dim iparray

    dim macarray

    dim finaliparray

    dim finalmacarray

    dim m

    dim args

    sub start()

    args=0

    synccount=0

    argerror=0

    if(wscript.arguments.count=0) then

    wscript.echo "enter the command"

    exit sub

    end if

    for i=0 to wscript.arguments.count-1

    if(i=0) then

    str=wscript.arguments.item(i)

    else

    str=str&" "&wscript.arguments.item(i)

    end if

    next

    str1=split(str)

    ‘show error if nothing is entered————

    if (not isarray(str1)) then

    wscript.echo "Error: unrecongnized or incomplete command line."

    call helpfile()

    exit sub

    end if

    ‘checking if the command is Rmanager or not

    if(strcomp("Rmanager",str1(0))<>0) then

    wscript.echo "Error: unrecongnized or incomplete command line."

    call helpfile()

    exit sub

    end if

    ‘checking for different cases after Rmanager

    if(ubound(str1)=0) then

    wscript.echo "Error: unrecongnized or incomplete command line."

    call helpfile()

    exit sub

    end if

    select case str1(1)

    case "-migrate"

     call migrate(str1)

    case "-sync"

     call migrate(str1)

     if(argerror=1) then

     exit sub

     end if

     synccount=1

     a=str1(2)

     str1(2)=str1(3)

     str1(3)=a

     call migrate(str1)

    case "-dumplease"

     call dump(str1)

    case "-makereservation"

     call makereservation(str1)

    case "/?"

     call helpfile()

    case else

     wscript.echo "Error: unrecongnized or incomplete command line."

     call helpfile()

     end select

    ‘delete the scopefile that is created during the program

    set fso = createobject("Scripting.FileSystemObject")

    if(fso.fileexists("scopefile.txt"))then

    fso.deletefile("scopefile.txt"),true

    end if

    end sub

    ‘ function for dumplease

    sub dump(str1)

    ‘if improper arguments are entered show error

    if ubound(str1)<4 then      

    wscript.echo "Error: unrecongnized or incomplete command line."

    call helpfile()

    exit sub

    end if

    filename = str1(2)

    set fso = createobject("Scripting.FileSystemObject")

    Set outfile = fso.CreateTextFile(filename)

    outfile.close

    p=checkserver(str1(3))

    if p=0 then

    wscript.echo "server "&str1(3)&" not present or is entered wrongly"

    exit sub

    end if

    serverarray = makescopearray(scopes)

    if not isarray(serverarray) then

    wscript.echo "no scopes in server "&str1(3)

    exit sub

    end if

    if (strcomp(str1(4),"all")=0) then

    if(ubound(str1)=4) then

    scope = serverarray

    else

    wscript.echo "improper arguments"

    end if

    else

    for i = 4 to ubound(str1)

    if(isscope(str1(i),serverarray)) then

    redim preserve scope(i-4)

    scope(i-4)=str1(i)

    else

    wscript.echo "scope "& str1(i) &" is not present in server "&str1(3)

    exit sub

    end if

    next

    end if

    ‘scope contains all the scopes entered

    if not isarray(scope) then

    wscript.echo "no scopes in the server "&str1(3)

    else

    if scope(0)="" then

    wscript.echo "no scopes in the server "&str1(3)

    else

    set wshell = CreateObject("WScript.Shell")

    for i = 0 to ubound(scope)

    set obj = wshell.exec("netsh dhcp server "&str1(3)&" scope "&scope(i)&" show clients")

    clients = obj.stdout.readall()

    if(instrrev(clients,"Command completed successfully.")=0) then

    exit sub

    end if

    call makeclients(clients,str1(2))

    next

    end if

    end if

    if args = 0 then

    else

    wscript.echo "the ipaddress and macaddress pair are in file "&filename

    end if

    end sub

    ‘the following procedure outputs the ip and corresponding mac addresses(active leases) into the file – ‘filename’…it takes the output string clients that we get after executing the command shoe clients

    sub makeclients(clients,filename)

    set fso = createobject("Scripting.FileSystemObject")

    Set scopefile = fso.CreateTextFile("scopefile.txt")

    scopefile.writeline(clients)

    scopefile.close

    Set scopefile = fso.OpenTextFile("scopefile.txt")

      do While not scopefile.AtEndOfStream   ‘reading line by line and checking for the string "Total No. of Scopes"

       line = scopefile.ReadLine

      if(instr(line,"No of Clients")=1) then  

      str = split(line)

      t=str(4)        ‘taking no. of clients into t

      exit do

      end if

      loop  

      scopefile.close

      t=cint(t)    

      if t=0 then

      wscript.echo "no clients in the scope "&str(ubound(str))

      exit sub

      end if

    args=args+1

    Set scopefile = fso.OpenTextFile("scopefile.txt")

    for i= 1 to 8

    scopefile.skipline

    next    

    set outfile = fso.opentextfile(filename,8)

    for i=0 to t-1

    line=scopefile.readline

    str= split(line)

    if (instr(line,"INACTIVE")<>0) then

    d=0

    for l=0 to ubound(str)

    if(str(l)="") then

    else

    if d=3 then

    exit for

    else

    d=d+1

    end if

    end if

    next

    outfile.write(str(0))

    for size = 1 to 18-(len(str(0)))

    outfile.write(" ")

    next

    outfile.writeline(trim(replace(str(l),"-","")))

    else

    d=0

    for l=0 to ubound(str)

    if(str(l)="") then

    else

    if d=4 then

    exit for

    else

    d=d+1

    end if

    end if

    next

    outfile.write(str(0))

    for size = 1 to 18-(len(str(0)))

    outfile.write(" ")

    next

    outfile.writeline(trim(replace(str(l),"-","")))

    end if

    next

    outfile.close

    end sub

    sub makereservation(str1)

    if ubound(str1)<>3 then

    wscript.echo "Error: unrecongnized or incomplete command line."

    call helpfile()

    exit sub

    end if

    server = str1(2)

    filename = str1(3)

    set fso = createobject("Scripting.FileSystemObject")

    if(not fso.fileexists(filename))then

    wscript.echo "file "&filename&" not present"

    exit sub

    end if

    k = checkserver(server)

    if k=0 then

    wscript.echo "server "&server&" is not present or may have been entered wrongly"

    exit sub

    end if

    scopearray=makescopearray(scopes)       ‘scopes is the output string obtained after executing show scope command for given server

    set handle = fso.opentextfile(filename)

    Dim ipaddress

    Dim macaddress

    t=0

    do while not handle.atendofstream

    line=trim(handle.readline)

    ‘line=replace(line," ","")

    if line="" then

    else

    strarray=split(line)

    p=0

    wscript.echo "upper bound"

    wscript.echo ubound(strarray)

    for k=0 to ubound(strarray)

    if strarray(k)="" then

    else

    wscript.echo "strarray(k)"

    wscript.echo strarray(k)

    if p=0 then

    ipaddress=trim(strarray(k))

    else

    if p=1 then

    macaddress = trim(strarray(k))

    end if

    end if

    p=p+1

    ‘wscript.echo Replace(ipaddress, " ", "")

    ‘wscript.echo macaddress

    end if

    next

    if p<>2 then

    wscript.echo "p = "

    wscript.echo p

    wscript.echo "improper format of file"

    exit sub

    end if

    redim preserve iparray3(t)

    redim preserve macarray3(t)

    iparray3(t)=ipaddress

    macarray3(t)=macaddress

    t=t+1

    end if

    loop

    handle.close

    if not isarray(iparray3) then

    wscript.echo "there is nothing in the file"

    exit sub

    end if

    dim arr

    redim arr(t-1)

    for i = 0 to ubound(scopearray)

    set iprange = fso.createtextfile("iprangefile.txt")

    iprange.close

    arraysize = 0

    dim iparray1

    redim iparray1(0)

    redim macarray1(0)

     set wshell = CreateObject("WScript.Shell")

    set obj = wshell.exec("netsh dhcp server "&server&" scope "&scopearray(i)&" show iprange")

    range = obj.stdout.readall()

    set iprange = fso.opentextfile("iprangefile.txt",8)

    iprange.writeline(range)

    iprange.close

    set iprange = fso.opentextfile("iprangefile.txt")

    for p=0 to 5

    iprange.skipline

    next

    line1=iprange.readline

    line2=split(line1)

    count=0

    for j=0 to t-1

    wscript.echo "IPS —————-"

    wscript.echo line2(3)

    wscript.echo line2(12)

    wscript.echo "IPS —————-"

    k = checkiprange(line2(3),line2(12),iparray3(j))

    if k = 1 then

    redim preserve iparray1(arraysize)

    redim preserve macarray1(arraysize)

    iparray1(arraysize) = iparray3(j)

    macarray1(arraysize) = macarray3(j)

    arraysize = arraysize + 1

    arr(j)=1

    else if k=2 then

    wscript.echo "the ip address "&iparray3(j)&" is not valid"

    fso.deletefile("iprangefile.txt"),true

    exit sub

    end if

    end if

    next

    iprange.close

    if isarray(iparray1) and not iparray1(0)="" then

    call addreservation(server,scopearray(i),iparray1,macarray1)

    if (finaliparray(0)="") then

    else

    for l=0 to ubound(finaliparray)

    set obj = wshell.exec("netsh dhcp server "&server&" scope " &scopearray(i)& " add reservedip "&finaliparray(l)&" "&finalmacarray(l) )

    outp =  obj.stdout.readall()

     if(instr(outp,"Command completed successfully.")<>0) then

     wscript.echo "added reservation "&finaliparray(l)& " "&finalmacarray(l)&" to server "&server&" in scope "&scopearray(i)

     else

     wscript.echo "The specified IP address "&finaliparray(l)& " or hardware address "& finalmacarray(l)& " is either not proper or is being used by another client."

     end if

    next

    end if

    end if

    redim iparray1(0)

    redim macarray1(0)

    next

    set handle = fso.opentextfile(filename)

    for i=0 to ubound(arr)

    str=trim(handle.readline)

    if(arr(i)=0) then

    line=split(str)

    wscript.echo "The ipaddress "&iparray3(i)&" is not present in any scope"

    end if

    next

    handle.close

    fso.deletefile("iprangefile.txt"),true

    end sub

    sub addreservation(server,scopestr,iparraysource,macarraysource)

    redim finaliparray(0)

    redim finalmacarray(0)

    set wshell = CreateObject("WScript.Shell")

    set obj = wshell.exec("netsh dhcp server "&server&" scope " &scopestr& " show reservedip" )

    outpdest = obj.stdout.readall()

    call makeipandmacarray(outpdest)

    iparraydest = iparray

    macarraydest = macarray  

    ‘ remove common ip and mac pair between the two servers for the given scope

    if(not isarray(iparraydest)) then

     finaliparray=iparraysource

     finalmacarray=macarraysource

    else

     k=0

     for i = 0 to ubound(iparraysource)

     flag=0

      for j=0 to ubound(iparraydest)

       if(iparraysource(i)=iparraydest(j) and macarraysource(i)=macarraydest(j)) then

       flag=1

       exit for

       end if

      next

     if flag=1 then

      if synccount = 0 and m=0 then

     wscript.echo "reservation "&iparraysource(i)&" "&macarraysource(i)&" is already present on server "&server

      end if

     else

     redim preserve finaliparray(k)

     redim preserve finalmacarray(k)

     finaliparray(k)=iparraysource(i)

     finalmacarray(k)=macarraysource(i)

     k=k+1  

     end if

     next

    end if

    end sub

    function checkiprange(ip1,ip2,ip3)

    str2=split(ip1,".")

    str3=split(ip2,".")

    str4=split(ip3,".")

    if ubound(str4)<>3 then

    checkiprange=2

    exit function

    end if

    dim string

    if cint(str2(0))<=cint(str4(0)) and cint(str4(0))<=cint(str3(0)) then

    if cint(str2(1))<=cint(str4(1)) and cint(str4(1))<=cint(str3(1)) then

     for i=2 to 3

    if(cint(str2(i))>=0 and cint(str2(i))<10) then

    string1=string1&"00"&str2(i)

    else if(cint(str2(i))>=10 and cint(str2(i))<100) then

    string1=string1&"0"&str2(i)

    else string1=string1&str2(i)

    end if

    end if

    next

    for i=2 to 3

    if(cint(str3(i))>=0 and cint(str3(i))<10) then

    string2=string2&"00"&str3(i)

    else if(cint(str3(i))>=10 and cint(str3(i))<100) then

    string2=string2&"0"&str3(i)

    else string2=string2&str3(i)

    end if

    end if

    next

    for i=2 to 3

    if(cint(str4(i))>=0 and cint(str4(i))<10) then

    string3=string3&"00"&str4(i)

    else if(cint(str4(i))>=10 and cint(str4(i))<100) then

    string3=string3&"0"&str4(i)

    else string3=string3&str4(i)

    end if

    end if

    next

    if(cint(string1)<=cint(string3) and cint(string3)<=cint(string2)) then

    checkiprange=1

    else

    checkiprange=0

    end if

    else

    checkiprange=0

    end if

    else

    checkiprange=0

    end if

    end function

    sub helpfile()

    set fso = createobject("Scripting.FileSystemObject")

    set help = fso.OpenTextFile("help.txt")

    do while not help.atendofstream

    wscript.echo (help.readline)

    loop

    end sub

    ‘ this function will return 1 if the scope(scopestr) is present in the given server(serverarray….this is an array of server scopes)(be it source or destination)…else 0

    function isscope(scopestr,serverarray)

    l=0

    for i=0 to ubound(serverarray)

    if(strcomp(scopestr,serverarray(i))=0) then

    l=1

    exit for

    end if

    next

    isscope=l

    end function

    ‘this function will check if the server entered as argument is present or not(or rather entered correctly or not)

    function checkserver(str1)

    dim wshell

    dim pos1

    set wshell = CreateObject("WScript.Shell")

    set obj = wshell.exec("netsh dhcp server "&str1&" show scope")

    scopes = obj.stdout.readall()

    pos1=instr(scopes,"Command completed successfully.")      ‘if the string is present, the command has executed succesfully

    if(pos1<>0) then

     checkserver=1

    else

     checkserver=0

    end if

    end function

    ‘this function takes as argument the output string that we get after the command show scope is executed and returns

    ‘the array of all scopes present in the server

    function makescopearray(scopes)

      set fso = createobject("Scripting.FileSystemObject")

      Set scopefile = fso.CreateTextFile("scopefile.txt")

      scopefile.writeline(scopes)           ‘writing output string to a file

      scopefile.close

      Set scopefile = fso.OpenTextFile("scopefile.txt")

      do While not scopefile.AtEndOfStream   ‘reading line by line and checking for the string "Total No. of Scopes"

       line = scopefile.ReadLine

      if(instr(line,"Total No. of Scopes")=2) then  

      str = split(line)

      t=str(ubound(str)-1)        ‘taking no. of scopes into t

      exit do

      end if

      loop  

      scopefile.close

      t=cint(t)      ‘converting string to integer

      if t=0 then      ‘if no scopes are present

      makescopearray=""    

      else

      Set scopefile = fso.OpenTextFile("scopefile.txt")    

      for i=1 to 5

      scopefile.skipline     ‘skipping first five lines

      next    

      for i=0 to t-1

      str3=trim(scopefile.readline)

      str3 = split(str3," ")

      redim preserve serverscope(i)

      serverscope(i)=str3(0)     ‘adding scopes to array serverscope

      next

      makescopearray = serverscope    ‘returning the array to calling function

      scopefile.close

      end if

    end function

    ‘function to take reservations from source server and add to destination server for a given scope

    sub reservedip(sourceadd,destadd,scopestr,filename,command)

    dim outp

    set wshell = CreateObject("WScript.Shell")

    set obj = wshell.exec("netsh dhcp server "&sourceadd&" scope " &scopestr& " show reservedip" )

    outpsource = obj.stdout.readall()

    call makeipandmacarray(outpsource)

    iparraysource = iparray

    macarraysource = macarray

    if(not isarray(iparraysource)) then

    wscript.echo "no reservations in the scope "&scopestr&" on the server "&sourceadd

     exit sub

    end if

    if(m=1 or m=2) then

    set fso = createobject("Scripting.FileSystemObject")

    Set outfile = fso.openTextFile(filename,8)

    for i=0 to ubound(iparraysource)  

    outfile.writeline("netsh dhcp server "&destadd& " scope " &scopestr& " add reservedip " &iparraysource(i)& " " & macarraysource(i))

    next

    outfile.close

    exit sub

    end if

    call addreservation(destadd,scopestr,iparraysource,macarraysource)

    ‘now finaliparray and finalmacarray contain only the unique ip and mac addresses

    if (not isarray(finaliparray) or finaliparray(0)="") then

    if ((m=1 or m=2)) then

     if (strcomp(command,"-migrate")=0 or (strcomp(command,"-sync")=0 and synccount=1)) then

     wscript.echo"output is in file "&filename

     end if

    end if

    exit sub

    end if

    t=ubound(finaliparray)

     set wshell = CreateObject("WScript.Shell")

     for i=0 to t

     set obj = wshell.exec("netsh dhcp server "&destadd&" scope " &scopestr& " add reservedip " &finaliparray(i)& " " & finalmacarray(i))

     outp =  obj.stdout.readall()

     if(instr(outp,"Command completed successfully.")) then

     wscript.echo "added reservation "&finaliparray(i)& " "&finalmacarray(i)&" to server "&destadd&" in scope "&trim(scopestr)

     else

     wscript.echo "The specified IP address "&finaliparray(i)& " or hardware address "& finalmacarray(i)& " is being used by another client."

     end if

     next

    if ((m=1 or m=2)) then

     if (strcomp(command,"-migrate")=0 or (strcomp(command,"-sync")=0 and synccount=1)) then

     wscript.echo"output is in file "&filename

     end if

    end if

    end sub

    sub makeipandmacarray(outp)

    redim iparray(0)

    redim macarray(0)

    set fso = createobject("Scripting.FileSystemObject")

    Set scopefile = fso.CreateTextFile("scopefile.txt")

    scopefile.writeline(outp)

    scopefile.close

    Set scopefile = fso.openTextFile("scopefile.txt")

    do While not scopefile.AtEndOfStream

    line = scopefile.ReadLine

    if(instr(line,"No of ReservedIPs")=1) then

    str = split(line)

    t=str(4)         ‘taking no. of reservations into t

    exit do

    end if

    loop  

    scopefile.close

    if t=0 then

    iparray = ""

    macarray = ""

    exit sub

    else

    Set scopefile = fso.openTextFile("scopefile.txt")

    for i=1 to 7

    scopefile.skipline

    next

    for i=1 to t

    redim preserve iparray(i-1)

    redim preserve macarray(i-1)

    string1=split(trim(scopefile.readline))

    iparray(i-1)=string1(0)

    macarray(i-1)=replace(string1(ubound(string1)),"-","")

    next

    end if

    scopefile.close

    end sub

    sub migrate(str1)

     if ubound(str1)<4 then        ‘ ensures that the command entered has required no. of arguments

            wscript.echo "Error: unrecongnized or incomplete command line."

     call helpfile()

     argerror=1

     exit sub

     end if

     dim k      

     k = checkserver(str1(2))               ‘checking if the source server exists or not…

                 ‘or if they are entered correct or not

     if k=0 then

     wscript.echo "server "&str1(2)& " not present or is entered wrongly"

     argerror=1

     exit sub

     end if

     scopes1 = scopes

     k = checkserver(str1(3))  

     if k=0 then

     wscript.echo "server "&str1(3)& " not present or is entered wrongly"

     argerror=1

     exit sub

     end if

     scopes2 = scopes  

    ‘comes here if both servers are entered correctly

      sourcescope = makescopearray(scopes1)      ‘sourcescope here has all scopes from source server

      if(isarray(sourcescope)) then     ‘checking if there are any scopes in source server  

      else

      wscript.echo "no scopes present on server "&str1(2)

      argerror=1

      exit sub

      end if  

      bound=ubound(str1)

      if(strcomp(str1(4),"all")=0) then           ‘checking if the option "all" is entered

       if(bound>4 and bound<7) then

        if(strcomp(str1(5),"-preview")<>0) then

         argerror=1

         wscript.echo "Error: unrecongnized or incomplete command line."

         call helpfile()

         exit sub

        else

         if(bound=6) then

         filename = str1(6)

         m=2

         else

         m=1

         filename = "output.txt"

         end if

        end if

       else

        m=0

       end if

       scope=sourcescope       ‘scope array now contains all the scopes from source server

      else

       if(strcomp(str1(bound),"-preview")=0) then

        m=1

        filename = "output.txt"

       else if(strcomp(str1(bound-1),"-preview")=0) then

        m=2

        filename = str1(bound)

       else

        m=0

       end if

       end if

      p=0

      for i = 4 to ubound(str1)-m      ‘this is the case when scopes are entered as arguments

      if(isscope(str1(i),sourcescope)) then     ‘checking if the given scope is present in source server

      redim preserve scope(p)       ‘if present then add the scope entered to the array ‘scope’

      scope(p)=str1(i)

      p=p+1  

      else

       if(synccount=0) then         ‘if the scope entered is not present in the source server

      wscript.echo "scope "&str1(i)& " is not present in the server "&str1(2)  

      end if

      end if

      next

      end if

      erase sourcescope       ‘freeing the space allocated to sourcescope

      if(not isarray(scope)) then      ‘checking if the scope array has some scopes or not  

      argerror=1

      exit sub

      end if

      destscope = makescopearray(scopes2)     ‘destscope has all scopes from destination server

      if(isarray(destscope)) then      ‘checking if there are scopes present on destination server

      else

      wscript.echo "no scopes present on server "&str1(3)

      argerror=1

      exit sub

      end if

     if(m=1 or m=2) then

      if(synccount=0) then

      set fso = createobject("Scripting.FileSystemObject")

      set outfile = fso.createtextfile(filename)

      outfile.close

      end if

     end if

      for i=0 to ubound(scope)      

      if(isscope(scope(i),destscope)) then     ‘checking if scopes are present on destination server or not  

       call reservedip(str1(2),str1(3),scope(i),filename,str1(1)) ‘this will take reservations in the given scope from source server and output add reservedip(destination) command to a file

      else        

       wscript.echo "scope "&scope(i)& " not present in server "&str1(3)  

      end if  

      next

     if (m=1 or m=2) then

      if(strcomp(str1(1),"-migrate")=0 or (strcomp(str1(1),"-sync")=0 and synccount=1)) then

      wscript.echo"output is in file "&filename

      end if

     end if

    end sub

    OUT.TXT

    10.150.0.2 00155d40a509

    10.150.0.3 00155d40a513

    10.150.0.4 00155d40a50b

    If you are getting error or stuck somewhere, use the prints like I have added to this one and debug, I think it is easier to do it that way.

    Let me know if you have any issues.

    Thanks,

    Subhash Badri

  11. ...1 says:

    Very nice site! Good work.

  12. ...1 says:

    Great site! Good luck to it’s owner!

  13. ...1 says:

    pagine piuttosto informative, piacevoli =)

  14. ...1 says:

    9 su 10! Ottenerlo! Siete buoni!

  15. Dragman says:

    Didn’t work for me I get error

    C:rmanager.vbs(681, 4) Microsoft VBScript runtime error: Subscript out of range

    : ‘[number: 0]’

    when I use the command

    cscript rmanager.vbs Rmanager -dumplease out.txt 120.0.0.1 all

  16. DanITP says:

    Same as above, only getting a VBScript runtime error: Subscript out of range: ‘[number: 0]’

    The dumplease works great, the makereservation is when I get the error.

  17. Matthew Flook says:

    I’d really like to use this tool, but I get the following error when running this command on SBS 2003 SP2:

    cscript rmanager.vbs Rmanager -makereservation 127.0.0.1 out.txt

    rmanager.vbs(523, 1)…runtime error: Type mismatch: ‘cint’

    What am I doing wrong?

  18. Alan Kaplan says:

    The support for DHCP script remains inadequate.  The enumeration of DHCP clients should contain the same fields that are in the MMC.  Leaving out the NAME of the computer with the lease is a major problem.  Worse, this information has never been available.  Not in dhcpobjs.dll.  Not in DHCPCMD.EXE, and not in WMI.  How about an ODBC connector for DHCP?  Nope.  

    It is depressing to see this elaborate vbscript that serves only as a front end for shell commands to netsh.

  19. Suheyla Ikiz says:

    Hello,

    We have the same error as above. We also got an error to addreservedip, imporper file format. Could you give an example of output.txt ( in this case it is inout file actually but the same aim in sense). Do we have to write ipaddress and macaddress coulumn name at the begining of the file and what sould be the seperator between the values ?

    Thanks

  20. Terry Crockett says:

    I am running -dumplease on scopes that have reservations

    If the lease is the result of a reservation the out.txt file only contains the IP of the lease.  There is no corresponding MAC address.  How can I modify the script so that MAC addresses for reserved leases will be written to out.txt?

    Thanks.

    TNC

  21. Matt H. says:

    Our DHCP servers have multiple scopes. All of the scopes are configured for subnets other than the one for which our DHCP servers reside on. 99% per of our reservations are for one particular subnet. If I used this tool, would actually work since the scope subnets and the DHCP servers subnet are different? If thats not a problem, will it just then copy over all reservations for each scope or can I define which scope to sync?

  22. Ben says:

    I’m working on an internal project to basically remove all "stale" dhcp reservations from our dhcp servers.  The process I’m assuming would require exporting all the reservations from the dhcp server, doing some verification (ie. Ping) to see if the server still responds, then removing the reservations.  I found your script and going to give it a try.  Before I get too deep into this, I was wondering if you knew of a tool or utility that already does this without me having to script something.

  23. Paul D. says:

    Has anyone tried the fixes posted by ronsdavis? If you have does it fix the problem?  Has anyone found another utiliy better than this one?

  24. Fortune Wang says:

    Hi All,

    I runing this script in my dhcp server,but get following error. what happen? please help me!!

    =============Begin==============

    E:>cscript rmanager.vbs Rmanager -dumplease d:test.txt 192.168.116.0 all

    Microsoft (R) Windows Script Host Version 5.6

    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    server 192.168.116.0 not present or is entered wrongly

    =============End==============

  25. rahul says:

    Dumping lease works, but reservation fails. Downloaded rmanager.vbs file and get same error as Matthew Flook.

    C:WINDOWSsystem32dhcp>cscript.exe rmanager.vbs Rmanager -makereservation 10.1

    44.0.99 out.txt

    Microsoft (R) Windows Script Host Version 5.6

    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    C:WINDOWSsystem32dhcprmanager.vbs(523, 1) Microsoft VBScript runtime error:

    Subscript out of range: ‘[number: 0]’

    Copy/pasted provided code in notepad, saved as rmanager.vbs and now getting improper file format error.

    Any idea where to get an working file?!…

  26. Eric says:

    I received the same error as Matthew Flook until I made the change listed by "ronsdavis". Now I’m receiving a similar error as Rahul, "subscript out of range: ‘[number:14]’.

    Any thoughts? Thank you! I look forward to using this tool!

  27. Eric says:

    Thank you for your help with this tool! I copied your code from above and created a new ‘rmanager.vbs’ file. My OUT.TXT looks similar to yours.

    When I try to make reservations, I now see much more output on the screen, but it still fails. I see the last IP in the range listed and then fails on line 777 (again, using your text copied from above) with a type mismatch: ‘cint’.

    Any ideas? Thank you!

  28. Eric says:

    I have emailed you at the address you listed. I look forward to your reply! Thank you!

  29. bsharper says:

    I was trying to set Dhcp reservation name in my C# application. But, the struct i got seems having IP and MAC address mapping as shown below:

    typedef struct _DHCP_IP_RESERVATION_V4 {

    DHCP_IP_ADDRESS ReservedIpAddress;

    DHCP_CLIENT_UID* ReservedForClient;

    BYTE bAllowedClientTypes;

    } DHCP_IP_RESERVATION_V4,

    How do i set the reservation name and possibily add comments/descriptions to the reservation through code. Isn't this doable?

  30. doum says:

    Hello, i just try your script :

    – If the DHCP server is not installed in English, script don’t work. I think it’s the problem for Fortune Wang

    blogs.technet.com/…/dhcp-reservation-tool.aspx

    – we loose name and description, so for me the script isn’t usable