Script for Windows that distinguishes by IP

2

I need your help because I'm going crazy to create a script that when you log in runs in all the posts in the domain and you modify the name of the Organization in Windows based on the IP address of the PC. The problem I have to get it if the IP address of the PC is in this range

10.51.40.0/22

Run this

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v    RegisteredOrganization /t REG_SZ /d "Entidad 1" /f

And if it is not included in this range, do this:

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v RegisteredOrganization /t REG_SZ /d "Entidad 2" /f

Can anyone help me?

Thank you!

    
asked by Sergio Villanueva 26.03.2018 в 09:00
source

1 answer

1

I put a script in VBS language, you will have to save it with extension vbs and run it as administrator with the command cscript so you can modify this registry key.

It is a small script that first detects in what version of Windows it is running in order to correctly obtain all the IPs that the machine has.

Once the IPs of the machine have been collected, check whether this IP is in the indicated range or not, with the IPEnRed function (IP, "10.51.40.0/22").

In your case you may have problems if a computer has more than one ip, since it will modify the registry as many times as IPs have the machine. In that case, you should NOT use the else in the IPEnRed If, and mount as many IPEnRed IPs as you need (one for each network).

Option Explicit
Dim winVer

Dim ip, IPs, ArrIPs
Dim i, extraCommand

' Obtenemos version del sistema operativo
winVer = myGetWinVersion()

Set IPs = CreateObject("System.Collections.ArrayList")

If winVer(0) > 6 Then
    ' Windows Vista y siguientes 
    GetIPs(IPs)
Else
    ' Windows XP
    GetIPs_XP(IPs)
End If

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )

ArrIPs = IPs.ToArray()

For Each IP in ArrIPs
    'WScript.echo IP & " - " & IpEnRed(IP, "10.51.40.0/22")
    If IpEnRed(IP, "10.51.40.0/22") Then
        'Empresa 1
        objShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization", "Entidad 1", "REG_SZ"
    Else
        'Empresa 2
        objShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization", "Entidad 2", "REG_SZ"
    End If
Next

Set objShell = Nothing

' Devuelve un array con la version del windows separado por .
Function myGetWinVersion

    Dim strComputer
    Dim objWMIService
    Dim num
    Dim oss, os
    Dim arr(4), cad

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
    Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

    For Each os in oss
        cad = Split(os.Version, ".")
    Next

    i = 0
    For each num in cad
        arr(i) = num
        i=i+1
    Next

    myGetWinVersion = arr

End Function

Sub GetIPs_XP(IPs)
    Dim strComputer
    Dim objWMIService
    Dim colAdapters
    Dim objAdapter

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & "!\" & strComputer & "\root\cimv2")
    Set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    For Each objAdapter In colAdapters

        If not IsNull(objAdapter.IPAddress(0))  Then
            If objAdapter.IPAddress(0) <> "127.0.0.1" Then IPs.Add objAdapter.IPAddress(0)
        End If

    Next

    Set colAdapters = Nothing
    Set objWMIService = Nothing
End Sub

Sub GetIPs(IPs)
' Este funciona para Windows 10
    Dim strComputer
    Dim objWMIService
    Dim colAdapters
    Dim objAdapter

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & "!\" & strComputer & "\ROOT\standardcimv2")
    Set colAdapters = objWMIService.ExecQuery("Select * from MSFT_NetIPAddress")
    For Each objAdapter In colAdapters

        'Wscript.Echo "IP " & objAdapter.IPv4Address & " - Interfaz " & objAdapter.InterfaceIndex
        If not IsNull(objAdapter.IPv4Address) Then
            If objAdapter.IPv4Address <> "127.0.0.1" Then  IPs.Add objAdapter.IPv4Address
        End If

    Next

    Set colAdapters = Nothing
    Set objWMIService = Nothing
End Sub

Function IpEnRed(IP, Red)

    Dim minRed, maxRed, IpBin

    minRed = INET_NTOA(CIDR2IP(Red, false))
    maxRed = INET_NTOA(CIDR2IP(Red, true))
    IpBin = INET_NTOA(IP)

    If ( ( IpBin > minRed ) and ( IpBin < maxRed ) ) Then
        IpEnRed = True
    Else
        IpEnRed = False
    End If

End Function


'https://stackoverflow.com/questions/13232835/ip-range-caclulator-from-ip-address-netmask-using-vbscript
Function CIDR2IP(ip, high)
    Dim highs, lows, byte0, byte1, byte2, byte3, Mask, bytes, rangelow, rangehigh, iplow, iphigh

    highs = "11111111111111111111111111111111"
    lows = "00000000000000000000000000000000"
    byte0 = Dec2bin(Split(ip, ".")(0))
    byte1 = Dec2bin(Split(ip, ".")(1))
    byte2 = Dec2bin(Split(ip, ".")(2))
    byte3 = Dec2bin(Split(Split(ip, ".")(3), "/")(0))
    Mask = Split(Split(ip, ".")(3), "/")(1)
    bytes = byte0 & byte1 & byte2 & byte3
    rangelow = Left(bytes, Mask) & Right(lows, 32 - Mask)
    rangehigh = Left(bytes, Mask) & Right(highs, 32 - Mask)
    iplow = bin2ip(Left(bytes, Mask) & Right(lows, 32 - Mask))
    iphigh = bin2ip(Left(bytes, Mask) & Right(highs, 32 - Mask))
    If high Then
        CIDR2IP = iphigh
    Else
        CIDR2IP = iplow
    End If
End Function

Function Dec2bin(dec)
    Dim bin, m, x
    Const maxpower = 7
    Const length = 8
    bin = ""
    x = cLng(dec)
    For m = maxpower To 0 Step -1
        If x And (2 ^ m) Then
            bin = bin + "1"
        Else
            bin = bin + "0"
        End If
    Next
    Dec2bin = bin
End Function

Function bin2ip(strbin)
    Dim ip0, ip1, ip2, ip3

    ip0 = C2dec(Mid(strbin, 1, 8))
    ip1 = C2dec(Mid(strbin, 9, 8))
    ip2 = C2dec(Mid(strbin, 17, 8))
    ip3 = C2dec(Mid(strbin, 25, 8))
    'combines all of the bytes into a single string
    bin2ip = ip0 & "." & ip1 & "." & ip2 & "." & ip3 
End Function

Function C2dec(strbin)
    Dim length, dec, x, binval, temp

    length = Len(strbin)
    dec = 0
    For x = 1 To length
        binval = 2 ^ (length - x)
        temp = Mid(strbin, x, 1)
        If temp = "1" Then dec = dec + binval
    Next
    C2dec = dec
End Function

Function INET_NTOA(ip)
    Dim ip0, ip1, ip2, ip3, urlobfs

    ip0 = Split(ip, ".")(0)
    ip1 = Split(ip, ".")(1)
    ip2 = Split(ip, ".")(2)
    ip3 = Split(ip, ".")(3)
    urlobfs = 0
    urlobfs = ip0 * 256
    urlobfs = urlobfs + ip1
    urlobfs = urlobfs * 256
    urlobfs = urlobfs + ip2
    urlobfs = urlobfs * 256
    urlobfs = urlobfs + ip3
    INET_NTOA = urlobfs
End Function
    
answered by 26.03.2018 / 13:00
source