Sign in to follow this  
Followers 0
Darkinspiration

Question on windows Startup and autoit.

16 posts in this topic

Hello, I'm new here as you all can probably see. I've been using autoit for a few months now and i love it. But this time i'm trying to do something a bit challenging.

Situation:

I have several hundred windows XP workstations. They are running the novell netware client and are login in a tree.

Now each workstation must map drives relative to the local server on theyre sites (because i have multiple sites...)

My solution is to create maps on the login script using a environement variable (NW_SRV) that changes relative to the current subnet of the computer.

Exemple

Site 1 ip:192.168.0.0/24 server:SITE1

if pc1 ip:192.168.0.12 then pc:NW_SRV=SITE1

map g: %<NW_SRV>\app\user

Now for this to work i need the script to decode the ip address and install the environement variable via reg key before the login script is executed.

I have the script it's working as far as i can tel. But the problem now is how do i execute it before the login ?

Is it even possible with autoit?

I tried using the runservice key but i can't tell if the program is running or not, the registry is not getting modified and it's not writing log files.

And i use @ippaddr1 in the script will it work before the login ?

Share this post


Link to post
Share on other sites



Why not do the IP address detection and nw_srv figuring in your login script itself? That way, you can manage the ip to server mapping centrally, so if things change you don't have to run around and mod your workstations.


BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

Why not do the IP address detection and nw_srv figuring in your login script itself? That way, you can manage the ip to server mapping centrally, so if things change you don't have to run around and mod your workstations.

Okay but how ?

The only thing i can think of is launching my autoit script from the loginscript with #myautoitscript.exe and hoping that the environement variable stick.

I can't seem to be able to do ip manipulation in the login script itself.

Share this post


Link to post
Share on other sites

Well, you could do some nonsense like this batch file:

:: Map server based on IP address demonstration
:: This script only echoes the mapping, rather than actually maps the drive.
:: Windows 2000/XP/2003 etc ONLY (no Win9x)
:: Author: BlueBearr

@echo off
:: This next line will return your IP address. Commented out so we can test with the example IP addresses below
::for /f "tokens=4-7 delims=. " %%a in ('ipconfig ^| findstr /R /C:"IP Address.*: [^0]"') do set _ip=%%a.%%b.%%c.%%d
:: This IP address maps to SRV1
set _ip=192.168.40.1
:: This IP address maps to SRV2
::set _ip=192.168.60.1
:: This IP address maps to SRV3
::set _ip=192.168.150.1

for /f "delims=. tokens=1-4" %%a in ("%_ip%") do set _ipa=%%a&set _ipb=%%b&set _ipc=%%c&set _ipd=%%d

if "%_ipa%.%_ipb%" == "192.168" (
  if %_ipc% GEQ 50 (
    if %_ipc% LEQ 110 (
::  Between 50 and 110
      echo net use j: \\srv2\share1
    ) ELSE (
::  Greater than 110
      echo net use j: \\srv3\share1
    )
  ) ELSE (
::  Less than 50
    echo net use j: \\srv1\share1
  )
)
set _ip=
set _ipa=
set _ipb=
set _ipc=
set _ipd=

But I would use something other than DOS. I've seen Kix or VBScript used for this kind of stuff. I've never used AutoIt in a login script, but it should work to detect the IP address, do the IP-to-server mapping, and then map the actual drive (using the DriveMapAdd command).

Here is the same drive mapping done with AutoIt:

Dim $srv
$ip=@IPAddress1
If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
    $ip = @IPAddress2
EndIf
If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
    $ip = @IPAddress3
EndIf
If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
    $ip = @IPAddress4
EndIf
If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
    Exit
EndIf

; These values are for testing
; This IP address maps to SRV1
;$ip="192.168.40.1"
; This IP address maps to SRV2
;$ip="192.168.60.1"
; This IP address maps to SRV3
$ip="192.168.150.1"

$ips = StringSplit($ip, ".")
If $ips[1] & "." & $ips[2] = "192.168" Then
    Select
        Case $ips[3] < 50
            $srv = "SRV1"
        Case $ips[3] >= 50 And $ips[3] <= 110
            $srv = "SRV2"
        Case $ips[3] > 110
            $srv = "SRV3"
        Case Else
            $srv = "SRV1"   ; Default case
    EndSelect
EndIf

;DriveMapAdd("J:", "\\" & $srv & "\\share1")
MsgBox(0, "Drive Mapping", 'DriveMapAdd("J:", "\\' & $srv & '\\share1")')

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

Nice! Remouve the maps from the login and map stuff in autoit instead. Now why didn't i think ou that ;)

The only real problem with this approch is that it's a lot harder to change your mapping since you have to modify and recompile the autoit script. A batch file would indeed be a lot more effective in that regard.

Also i will not have te same amount of message i get from a novell login script. Unless i buil my script really well.

Still it's a very interesting solution to my problem. Thanks

Share this post


Link to post
Share on other sites

Now why didn't i think ou that

The advantage of fresh eyes... ;)

The only real problem with this approch is that it's a lot harder to change your mapping since you have to modify and recompile the autoit script. A batch file would indeed be a lot more effective in that regard.

I thought of that. You can get around this in several ways:

1. Separate your program and your data, and put your data in a text file that you can easily edit. For example, you could make a text file that contained four columns - lower IP address, upper IP address, UNC path, drive letter - and then have your script read the file to parse the data and do the appropriate mappings.

2. Make your AutoIt script really simple, and feed it all the information on the command line. You might write a script that just takes an upper limit, a lower limit, a UNC path and a letter and then maps the drive only if the current IP address falls between the limits, and then put a lot of lines like this in your script:

:: Site A
start /w MapDriveIf.exe 192.168.100.1 192.168.100.254 \\srv1\share1 J:
start /w MapDriveIf.exe 192.168.100.1 192.168.100.254 \\srv1\share2 K:
:: Site B
start /w MapDriveIf.exe 192.168.200.1 192.168.200.254 \\srv2\share1 J:
start /w MapDriveIf.exe 192.168.200.1 192.168.200.254 \\srv2\share2 K:

3. You could explore running your script without compiling it. I'm not sure if this is easily done with AutoIt.

Also i will not have te same amount of message i get from a novell login script. Unless i buil my script really well.

KiXtart (http://www.kixtart.org) might fulfill all of your requirements. It runs plain text files, runs from a single executable, and includes a function to get the IP address. VBScript would work too, but you kind of need to jump through hoops to get it to return the IP address - unless you use the DOS trick I showed you. :lmao: However, the syntax rules for KiXtart are different enough from AutoIt that you may find it slow going.

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

2. Make your AutoIt script really simple, and feed it all the information on the command line. You might write a script that just takes an upper limit, a lower limit, a UNC path and a letter and then maps the drive only if the current IP address falls between the limits, and then put a lot of lines like this in your script:

:: Site A
start /w MapDriveIf.exe 192.168.100.1 192.168.100.254 \\srv1\share1 J:
start /w MapDriveIf.exe 192.168.100.1 192.168.100.254 \\srv1\share2 K:
:: Site B
start /w MapDriveIf.exe 192.168.200.1 192.168.200.254 \\srv2\share1 J:
start /w MapDriveIf.exe 192.168.200.1 192.168.200.254 \\srv2\share2 K:
This option seem to make more sense. Looks like i will have to rewrite my script ;).

But just for argument sake... You haven't answered one of my previous questions. Is it possible to run a autoit script at startup before the login as service?

I even do my original problem will most likely be fixed by your solution (a thousand thanks by the way), i'm still wondering...

Share this post


Link to post
Share on other sites

You haven't answered one of my previous questions

I was hoping you wouldn't notice... ;)

Is it possible to run a autoit script at startup before the login as service?

I didn't know so I did a test, and it turns out that it is possible. It is even possible to set an environment variable as you asked in your original post, using the SETX.EXE utility that is part of the Microsoft Windows XP Support Tools.

In my test, I compiled the following script.

WARNING! Be careful with this script. It runs in a loop every 15 seconds. I did this to facilitate my testing, but this is probably way too frequent in actual practice.

AutoItSetOption("TrayIconHide", 1)
While 1
    ; Get the IP address
    $ip = _GetValidIP()
    ; Set the environment variable
    Run(@ComSpec & ' /c ""' & @ScriptDir & '\setx.exe" IPADDRESS ' & $ip & ' -m"', @ScriptDir, @SW_HIDE)
    FileWriteLine(@ScriptDir & "\IP.txt", _CurrTime() & "  " & $ip)
    Sleep(15000)
WEnd

Func _GetValidIP()
    $ip=@IPAddress1
    If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
        $ip = @IPAddress2
    EndIf
    If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
        $ip = @IPAddress3
    EndIf
    If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
        $ip = @IPAddress4
    EndIf
    Return $ip
EndFunc

Func _CurrTime()
    Return @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
EndFunc

Note that the script requires SETX.EXE to be in the same directory as itself, and wants to write to a file called IP.txt in that directory.

I then used INSTSRV.EXE and SRVANY.EXE from the Windows 2003 Resource Kit to set up the script as a service - you can read about how to do that here: http://support.microsoft.com/kb/137890 or search the forum - I know that there is info on how to do this.

After I set up the service I rebooted and then waited at the log in screen for about 15 minutes. When I logged in, the IP.txt file showed that the script had run successfully while no user was logged in. And, when I opened a command prompt, typing "echo %IPADDRESS%" displayed the IP address. Cool!

This was an instructive little exercise.


BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

I was hoping you wouldn't notice... ;)

I didn't know so I did a test, and it turns out that it is possible. It is even possible to set an environment variable as you asked in your original post, using the SETX.EXE utility that is part of the Microsoft Windows XP Support Tools.

In my test, I compiled the following script.

WARNING! Be careful with this script. It runs in a loop every 15 seconds. I did this to facilitate my testing, but this is probably way too frequent in actual practice.

AutoItSetOption("TrayIconHide", 1)
While 1
    ; Get the IP address
    $ip = _GetValidIP()
    ; Set the environment variable
    Run(@ComSpec & ' /c ""' & @ScriptDir & '\setx.exe" IPADDRESS ' & $ip & ' -m"', @ScriptDir, @SW_HIDE)
    FileWriteLine(@ScriptDir & "\IP.txt", _CurrTime() & "  " & $ip)
    Sleep(15000)
WEnd

Func _GetValidIP()
    $ip=@IPAddress1
    If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
        $ip = @IPAddress2
    EndIf
    If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
        $ip = @IPAddress3
    EndIf
    If $ip = "127.0.0.1" Or $ip = "0.0.0.0" Then
        $ip = @IPAddress4
    EndIf
    Return $ip
EndFunc

Func _CurrTime()
    Return @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
EndFunc

Note that the script requires SETX.EXE to be in the same directory as itself, and wants to write to a file called IP.txt in that directory.

I then used INSTSRV.EXE and SRVANY.EXE from the Windows 2003 Resource Kit to set up the script as a service - you can read about how to do that here: http://support.microsoft.com/kb/137890 or search the forum - I know that there is info on how to do this.

After I set up the service I rebooted and then waited at the log in screen for about 15 minutes. When I logged in, the IP.txt file showed that the script had run successfully while no user was logged in. And, when I opened a command prompt, typing "echo %IPADDRESS%" displayed the IP address. Cool!

This was an instructive little exercise.

Wow thanks. That's very instructive indeed. I'll keep in mind.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

...I then used INSTSRV.EXE and SRVANY.EXE from the Windows 2003 Resource Kit to set up the script as a service - you can read about how to do that here: http://support.microsoft.com/kb/137890 or search the forum - I know that there is info on how to do this...

Nice work bluebearr.

@Darkinspiration,

See this Q4 in this sticky:

http://www.autoitscript.com/forum/index.php?showtopic=37289

Q4. How can I run my script as a service?

Edit: New URL due to post split

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Nice work bluebearr.

@Darkinspiration,

See this Q4 in this sticky:

http://www.autoitscript.com/forum/index.php?showtopic=34630

Q4. How can I run my script as a service?

Herr i plead ignorence of windows startup sequece.... i honestly did not know that the only to do this was to transform the script into a service.

Share this post


Link to post
Share on other sites

There is one thing that you need to be consider. If you are using Service to create a map drive, this is only for the SYSTEM account. Unless you create service to run under PC user. Which means, map drive won't be showing up when an user login.

Share this post


Link to post
Share on other sites

...the only to do this was to transform the script into a service.

I was not attempting to convey that running the script as a service was the only way... I just wanted to add to bluebearr's info on how to do so.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

There is one thing that you need to be consider. If you are using Service to create a map drive, this is only for the SYSTEM account. Unless you create service to run under PC user. Which means, map drive won't be showing up when an user login.

I should have mentioned that I was thinking of doing this in two steps.

Do this as a service:

...Now for this to work i need the script to decode the ip address and install the environement variable via reg key before the login script is executed...

Do this after the user logs in via a second script:

...My solution is to create maps on the login script using a environement variable (NW_SRV) that changes relative to the current subnet of the computer...

...but I think that bluebearr might have worked out a better way...

-MSP-


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

I would like to add that novell permits to do the thing i wanted with theyre login script.

example:

SET IPNET="%NETWORK" >>4 ;this will return the first four digit of the hexadecimal ip address

IF "%IPNET" = "XXXX" THEN

INCLUDE .something

ELSE

IF "%IPNET" = "YYYY" THEN

INCLUDE .something

ELSE

WRITE "ERROR your ip is not recognised"

PAUSE

END

END

This is not documented in the login script helpfile nor in novell current login script documentation

(grrrrr....)

so my autoit script is now no longer useful. But i does not mean that this will be forgoten. Thanks for your help everyone.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

There is one thing that you need to be consider. If you are using Service to create a map drive, this is only for the SYSTEM account. Unless you create service to run under PC user. Which means, map drive won't be showing up when an user login.

I have actually done such a thing: mapping drive letters to network shares before a user account is singled out at login. I used a Group Policy Machine Startup Script entry to map two drive letters that I wanted as global, or available-to-all, drive maps. It does work and users can access the mapped drive after logging in, even though the drive is mapped by NT AUTHORITY\SYSTEM, or such. In a way, it can make the drive mappings more secure, as the users can only use the mapped drive, not change or remove it. The only way, that I discovered after much balding of my own head :P , to alter the SYSTEM-owned maps is with a registry "hack." A user belonging to the Administrators group has to edit the Registry and manually delete, or alter, the mappings, then reboot the PC to apply the changes. Unfortunately, I can't recall right now where the mappings were stored, but it wouldn't be hard to find by simply searching the Registry for the share path used by the mapping.

...My $0.02... :D

Edited by 1of10

[right][img]style_emoticons/autoit/robot.gif[/img]One of TenSecondary Adjunct of Unimatrix Z03[/right]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0