' vmware-mac-randomizer.vbs 7/27/09 Matt Brinkhoff (matt.brinkhoff@nebraskatechnology.com) 
' this vbs will randomize a mac address in a vmware vmx file
' it has to read in the file and write it back out to do this
' it takes two parameters, the first is the name of the vmx
' file and the second is the name of the interface

Const adVarChar = 200
Const MaxCharacters = 255
Const ForReading = 1
Const ForWriting = 2

if wscript.arguments.count <> 2 then
	msgbox "USAGE:  vmware-mac-randomizer.vbs VMXFileName Interface"
	wscript.quit
end if

strFileName = wscript.arguments(0)
strInterface = lcase(wscript.arguments(1))

' create a random mac address
' in the range 00:50:56:00:00:00 to 00:50:56:3f:ff:ff

strNewMAC = "00:50:56"

For I = 1 to 3
 Randomize
 intR1 = Int(16*Rnd)
 Randomize
 intR2 = Int(16*Rnd)
 strNewMac = strNewMac & ":" & hex(intR1) & hex(intR2)
Next

' create recordset for sorting
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "TextData", adVarChar, MaxCharacters
DataList.Open

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
	' lcase the interface lines so that we don't have to worry about case in our filter later
	' we could lcase the whole file but some people might like their caps
	if lcase(left(strLine, len(strInterface))) = strInterface then
		strLine = lcase(strLine)
	end if
    DataList.AddNew
    DataList("TextData") = strLine
    DataList.Update
Loop
objFile.Close

' backup the original file
objFSO.CopyFile strFileName, strFileName & ".VMR_bak", true

' sort the dataset to remove potential dupes of the address lines we're going to write
Dim strFilterValue(1)

strFilterValue(0) = "address"
strFilterValue(1) = "generated"

' now build our filter criteria
For I = lbound(strFilterValue) to ubound(strFilterValue)
	strFilterTemp = "(TextData LIKE '" & strInterface & "." & strFilterValue(I) & "*')"
	if I = lbound(strFilterValue) then
		strFilter = strFilterTemp
	else
		strFilter = strFilter & " OR " & strFilterTemp
	end if
Next

' add the comment to the filter
strFilter = strFilter & " OR (TextData LIKE '# Randomized MAC for interface*')"

' apply the filter to the data list
DataList.Filter = strFilter 

' delete the records we don't want
Do While Not DataList.EOF
    DataList.Delete
    DataList.MoveNext
Loop

' reset the filter
DataList.Filter = ""

' now write out the new file
DataList.MoveFirst

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting, True)

Do Until DataList.EOF
		strTemp = DataList.Fields.Item("TextData")
		objFile.WriteLine strTemp
		DataList.MoveNext
Loop

' now append our new ethernet values
objFile.WriteLine "# Randomized MAC for interface " & strInterface
objFile.WriteLine strInterface & ".addresstype = ""static"""
objFile.WriteLine strInterface & ".address = """ & strNewMac & """"

objFile.Close

