Answered Login script IP address check

  • Monday, January 21, 2013 8:21 PM
     
     

    Hi!

    We have some users that when they travel among the branch offices, they doesn´t need to map network drives, that are mapped during login script.

    When they logon the network access becomes very slowly. So I´d like to change my login script below with a IP Address Checking, like the second script, but I don´t know how to do that.

    My actual login script is:

          net use p: /delete
          net use p: \\site01-fs01\Publico$ /y

          net use w: /delete /y
          net use w: \\site01-fs01\trab$ /y

          net use u: /delete /y
          net use u: \\site01-fs01\aplic$ /y

    The new Script should be like this:

          If LOCAL_IP (is between 10.1.0.1 and 10.1.255.254) {

                net use p: /delete
                net use p: \\site01-fs01\Publico$ /y

                net use w: /delete /y
                net use w: \\site01-fs01\trab$ /y

                net use u: /delete /y
                net use u: \\site01-fs01\aplic$ /y

          } else exit;

    Can anyone help-me?

    The workstations are Windows XP and this script is set on GPO under User Configuraitons\Policies\Windows Settings\Scripts\Logon.

    Thankyou in advance.


    Fabio Martins MCDST/MCSA Brasil!!!

All Replies

  • Monday, January 21, 2013 9:04 PM
     
     Answered

    Try this:

    @echo off
    for /F "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "ipv4 address"') do set IP=%%a
    for /F "tokens=3 delims=." %%a in ('echo %IP%') do set E3=%%a
    if %E3% GTR 1 goto :eof
    net use p: /delete
    . . .

    Depending on the structure of the other IP addresses, you may need to run additional tests on the left-most elements of the IP address. Note also that curly brackets { } are meaningless in batch files.

  • Wednesday, January 23, 2013 7:57 PM
     
     

    The string vary on Win7 and XP and Language.

    My client computers are all in Portuguese/BR.

    WinXP: Endereço IP . . . . . . . . . . . . : 10.8.15.7

    Win7: Endereço IPv4. . . . . . . .  . . . . . . . : 10.1.15.4

    All IP addresses start with 10. and the second octect represents the subnet (site). I adapted this to the script below

    @Echo off
    for /F "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "ipv4"') do set IP=%%a
    for /F "tokens=2 delims=." %%a in ('echo %IP%') do set E3=%%a
    if %E3% EQU 1 Echo "Rede 1"
    if %E3% EQU 4 Echo "Rede 4"
    if %E3% EQU 5 Echo "Rede 5"
    if %E3% EQU 6 Echo "Rede 6"
    if %E3% EQU 7 Echo "Rede 7"
    if %E3% EQU 8 Echo "Rede 8"
    if %E3% EQU 9 Echo "Rede 9"

    But I need to generalize, put an "if" like...

    if WINXP for /F "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "Endereço IP"') do set IP=%%a
    if WIN7 for /F "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "Endereço IPv4"') do set IP=%%a
    for /F "tokens=2 delims=." %%a in ('echo %IP%') do set E3=%%a
    if %E3% EQU 1 Echo "Rede 1"
    if %E3% EQU 4 Echo "Rede 4"
    if %E3% EQU 5 Echo "Rede 5"
    if %E3% EQU 6 Echo "Rede 6"
    if %E3% EQU 7 Echo "Rede 7"
    if %E3% EQU 8 Echo "Rede 8"
    if %E3% EQU 9 Echo "Rede 9"

    Can anyone help-me? May I start another thread?


    Fabio Martins MCDST/MCSA Brasil!!!

  • Wednesday, January 23, 2013 8:12 PM
    Moderator
     
      Has Code

    Hi,

    If you can use a WSH script it would be better because you could simply get the current IP address using WMI. Here's a VBScript sample:


    Function IsIPv4Address(Address)
      Dim RegExp
      Set RegExp = New RegExp
      RegExp.Pattern = "^(\d+)\.(\d+)\.(\d+)\.(\d+)$"
      IsIPv4Address = RegExp.Test(Address)
    End Function
    
    Dim WMI
    Set WMI = GetObject("winmgmts:{impersonationlevel=impersonate}!root/CIMV2")
    
    Dim Adapters, Adapter
    Set Adapters = WMI.ExecQuery("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=True")
    For Each Adapter In Adapters
      If (Not IsNull(Adapter.IPAddress)) Then
        Dim I
        For I = 0 To UBound(Adapter.IPAddress)
          Dim Address
          Address = Adapter.IPAddress(I)
          If IsIPv4Address(Address) Then
            WScript.Echo Address
          End If
        Next
      End If
    Next
    

    Note that this VBScript example accounts for multiple adapters and multiple IP addresses per adapter.

    Bill

  • Wednesday, January 23, 2013 8:13 PM
     
     

    Can anyone help-me? May I start another thread?


    Fabio Martins MCDST/MCSA Brasil!!!

    You never said anything about the script I wrote for you.
    Furthermore your new specification looks completely different from the spec in your initial post.

  • Wednesday, January 23, 2013 8:26 PM
     
     

    Can anyone help-me? May I start another thread?


    Fabio Martins MCDST/MCSA Brasil!!!

    You never said anything about the script I wrote for you.
    Furthermore your new specification looks completely different from the spec in your initial post.


    I gave you a point voting as helpfull.

    Your script was OK, except about the first "if" that I changed to (for /F "tokens=2 delims...)

    I understood how to change the script and made my script more usefull then the previous.

    My question was if can anyone could help-me in the "if xp/7" clause in this same thread.

    Thankyou!


    Fabio Martins MCDST/MCSA Brasil!!!

  • Wednesday, January 23, 2013 8:43 PM
     
     

    My question was if can anyone could help-me in the "if xp/7" clause in this same thread.

    Thankyou!


    Fabio Martins MCDST/MCSA Brasil!!!

    Open a Command Prompt and type the command ver. Its output lets you differentiate between WinXP and Windows 7.
  • Monday, January 28, 2013 5:24 AM
     
     
    Besides looking for a scripting solution, have you ever considered using Group Policy Preferences if you DCs are running Windows 2008 or later?  The IP Range and Site targeting may fit well in this situation.