Skip to content

Instantly share code, notes, and snippets.

@fcojperez
Created March 10, 2013 21:00
Show Gist options
  • Save fcojperez/5130389 to your computer and use it in GitHub Desktop.
Save fcojperez/5130389 to your computer and use it in GitHub Desktop.
'-----------------------------------------------------------------------------------------------
' Script que configura la red de los equipos.
' Creado por Francisco Jose Pérez Jiménez. 11/02/2008
' mail: fcojperez@gmail.com
'-----------------------------------------------------------------------------------------------
'on Error Resume Next
Const cntByteIP = 2 ' Leer el byte de la dirección, los valores deben ser: Byte 1 = 0,Byte 2 = 1,Byte 3 = 2,Byte 4 = 3
Const NewByteIP = "55" 'Los valores debería ser entre 0 y 255
Const CurrentByteIP = "67" 'Valor actual del Byte a modificar en la dirección ip el valor entre 0 y 255
Call LeerDireccionIP
WSCript.Echo "Final"
WSCript.Quit
Sub LeerDireccionIP()
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
Dim ArrIP
Dim strIP
strIP = TruncateIP(IPConfig.IPAddress(0), cntByteIP+1, NewByteIP)
ArrIP = Split(IPConfig.IPAddress(0), ".", -1, 1)
'If ArrIP(cntByteIP) = CurrentByteIP then
WScript.Echo IPConfig.IPAddress(0) & " ---- Nueva Direccion ---> " & strIP & " --- Byte ->> " & ArrIP(cntByteIP)
Call CambiarIP(strIP)
'End if
End If
Next
End Sub
'-----------------------------------------------------------------------------------------------
' CAMBIO DE LA DIRECCIÓN IP DEL EQUIPO
'-----------------------------------------------------------------------------------------------
Sub CambiarIP (strIP)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array(strIP) ' <---- PONER LA DIRECCION IP
strSubnetMask = Array("255.255.255.0") ' <---- PONER LA MASCARA DE SUBRED
strGateway = Array("192.168.67.1") '<---- PONER LA PUERTA DE ENLACE
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "OK: LA CONFIGURACIÓN IP HA SIDO INTRODUCIDA CORRECTAMENTE"
Else
WScript.Echo "¡¡¡ERROR!!!: LA DIRECCIÓN IP NO HA PODIDO SER INTRODUCIDA."
End If
Next
End Sub
'-----------------------------------------------------------------------------------------------
' INTRODUCCIÓN DE LAS DNS EN EL EQUIPO
'-----------------------------------------------------------------------------------------------
Call CambiarDNS()
Sub CambiarDNS()
Dim StrLine
strComputer = "."
Set objNet = WScript.CreateObject( "WScript.Network" )
strDNS = Array("192.168.67.34","192.168.67.35") '<---- PONER LAS DIRECCIONES DNS
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objNetCard in colNetCards
ObjNetCard.SetDNSServerSearchOrder(strDNS) 'Configuración del DNS
Next
End Sub
'-----------------------------------------------------------------------------------------------
' FUNCION PARA TRUNCAR EL BYTE DE LA DIRECCION IP
'-----------------------------------------------------------------------------------------------
Function TruncateIP(strIP, strByteIP,strNewByteIP)
Dim ArrIP
ArrIP = Split(strIP, ".", -1, 1)
Select Case strByteIP
Case 1 'Byte 1 de la dirección IP
TruncateIP = strNewByteIP & "." & ArrIP(1) & "." & ArrIP(2) & "." & ArrIP(3)
Case 2 'Byte 2 de la dirección IP
TruncateIP = ArrIP(0) & "." & strNewByteIP & "." & ArrIP(2) & "." & ArrIP(3)
Case 3 'Byte 3 de la dirección IP
TruncateIP = ArrIP(0) & "." & ArrIP(1) & "." & strNewByteIP & "." & ArrIP(3)
Case 4 'Byte 4 de la direccion IP
TruncateIP = ArrIP(0) & "." & ArrIP(1) & "." & ArrIP(2) & "." & strNewByteIP
End Select
End Function
'-----------------------------------------------------------------------------------------------
' FIN DEL SCRIPT
'-----------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment