Submitted by
Category
Review Cycle
.
Public
Joachim Mutter/sysarc
on
08/05/2011
at
08:57 AM
DOS\Batches
Automatic drive mapping
English
English
People who often work in different networks, would whish a simple script, which maps the appropriate drives which are available in one particular network.
But simple mapping of drives, especially more than one, could produce nightmares, because the system waits a long time, if you use "net use" with a non existing UNC path.
But with a bit shell programming you could avoid this behavior. And besides, it offers you the possiblilty of map different sets of drives depoending on the used subnet.
Take a look to the code section, where you find the code (standart DOS batch commands)
For the descission we use a normal ping with the parameters n -1 w -200 to use only one ping and a timeout period of 200 milliseconds, to shorten the waiting time.
If the ping finds a target, the system variable errorlevel will get the value 0, otherwise a value different to 0.
And this is the trick.
We ping the UNC Drive part (the net address) and if we got an errorlevel of 0, which could be evaluated by the construct
IF %ERRORLEVEL%==0 the machine is accessible and we could use "net use" to map the appropriate drive.
German
German
Leute die oft in unterschiedlichen Netzen unterwegs sind, würden sich ein einfaches Script wünschen, in das sie die Laufwerke, auf die sie in diesem speziellen Netz Zugriff haben, mappen möchten.
Doch ein einfaches Mappingscript kann zu einem Alptraum werden, wenn man vefrsucht per "Net use" Laufwerke zu mappen, deren Maschine nicht zugreifbar ist.
Aber mit ein wenig DOS Wissen und ein paar Zeilen Batch Code lässt sich das sehr elegant umgehen. Zudem kann mit diesem Script auch ein Automatismus implementiert werden, der abhänig vom Subnetz unterschiedliche Sets von Laufwerken mappt.
Wenn Sie sich folgenden Batchcode mal ein wenig näher ansehen, dann versteht man auf Anhieb den Mechanismus.
Für die Entscheidung ob eine Maschine da ist, kann man einfach einen Ping absetzen. Wir verwenden dazu die Parameter n-1
und w -200, um nur einen Ping mit einem Timeout von 200 Millisekunden zu schicken, was die Wartezeit verkürzt.
Und das ist der Trick!
Wenn der Ping mit dem UNC Pfad (nur die Netzadresse) nun eine erfolgreiche Verbindung hergestellt hat, dann setzt er die Systemvariable ERRORLEVEL auf 0. Im anderen Fall steht hier ein Wert ungleich 0, was per IF %ERRORLEVEL%==0 Konstrukt dazu verwendbar ist, ein "net use" auszuführen, oder eben nicht.
DOS Batch Code
DOS Batch Code
@echo off
@SET BASEPATH=%~d0%~p0
@SET BATCH=%~f0
@echo. > %BASEPATH%\error.txt
@IF "%1"=="/PARAM"; goto GETPARAMETER
@IF "%1"=="/MAP"; goto MAPPING
REM ============================================================
REM Check for help
REM ============================================================
@if "%1"=="?" @goto HELP
@if "%1"=="/?" @goto HELP
@if "%1"=="-?" @goto HELP
REM ============================================================
REM Extract paramteres
REM ============================================================
@set VERBOSE=
@set ADDRESS=
@set LASTADDRESS=
@set SINGLECALL=
@set VERBOSE=
@set QUIET=
@set LOCALUSER=
@set TARGET=
@set DELETE=
@set NOPING=
@call %BATCH% /PARAM %1
@call %BATCH% /PARAM %2
@call %BATCH% /PARAM %3
@call %BATCH% /PARAM %4
@call %BATCH% /PARAM %5
if "%LOCALUSER%"==""; @SET LOCALUSER=%USERNAME%
IF "%VERBOSE%"=="1"; (
@echo VERBOSE=%VERBOSE%
@echo SINGLECALL=%SINGLECALL%
@echo VERBOSE=%VERBOSE%
@echo QUIET=%QUIET%
@echo LOCALUSER=%LOCALUSER%
@echo TARGET=%TARGET%
@echo DELETE=%DELETE%
@echo NOPING=%NOPING%
)
REM ============================================================
REM Do the calls
REM ============================================================
@if "%DELETE%"=="1"; (
@net use * /Delete /YES
@goto EXIT
)
@if "%SINGLECALL%"=="1" @goto SINGLE_CALL
@if "%TARGET%"=="joe-tpn2" @goto JOE-TPN2
@if "%TARGET%"=="joehome" @goto JOEHOME
@if "%TARGET%"=="virtualbox" @goto virtualbox
@if "%TARGET%"=="" goto CHECK
echo Somethings wrong with the paramters ....
goto EXIT
:HELP
@echo ============================================================
@echo This batch maps drives given in the region mapping section
@echo - ? shows this help
@echo - /D Delete all mapped drives and exit
@echo - /V shows some additional internal processing
@echo - /T:[Direct Mapping resource]
@echo joe-tpn2
@echo joehome
@echo virtualbox
@echo - /Q Quiet call (no pause at the end
@echo - /U:[Username], relevant for vogsphere
@echo - /N Do not use ping for checking Hosts (VPN Mode)
@echo - Call with nothing: Executes the remapping
@echo Example: mapper /T:vogsphere /N [gets the drives from vogsphere
@echo wihout using PING for validation]
@goto EXIT
REM ============================================================
REM Only defined Mappings
REM ============================================================
:SINGLE_CALL
@echo ==================================
@echo ... Mapper SingleCall Interface
rem @net use
@net use * /delete /YES 2>nul >nul
@set _VERBOSE=
@set _QUIET=
@if "%VERBOSE%"=="1"; @set _VERBOSE=/V
@if "%QUIET%"=="1"; @set _QUIET=/Q
echo QUIET = %_QUIET%
@call %BATCH% /T:vogsphere /U:%LOCALUSER% %_VERBOSE% %_QUIET%
@call %BATCH% /T:sabine-1 /U:%LOCALUSER% %_VERBOSE% %_QUIET%
@call %BATCH% /T:joe-tpn3 /U:%LOCALUSER% %_VERBOSE% %_QUIET%
@if "%_QUIET%"=="/Q" @GOTO EXIT
@GOTO ENDE
REM ============================================================
REM Regions check
REM ============================================================
:CHECK
@echo ==================================
@echo Löscht aktuelle Laufwerke
@echo ==================================
@net use
@net use * /delete /YES 2>nul 1>nul
rem @net use S: /delete /YES 2>nul 1>nul
REM ============================================================
REM Check the IP Range to decide what to map
REM ============================================================
@set ADDRESS=fritz
@ping -n 1 %ADDRESS%>nul
@if %errorlevel%==0 goto HOME_REGION
REM ============================================================
REM Start regions
REM ============================================================
REM ===================================================================
:OTHER_REGION
REM ===================================================================
:OTHER_START
@echo ==================================
@echo Mappt alle Other Laufwerke
@echo ==================================
REM OTHER **********************************************
@set ADDRESS=10.10.0.10
@set USER=xxxx
@set PWD=xxxx
@call %BATCH% /MAP S %ADDRESS% share %USER% %PWD% /persistent:no
GOTO ENDE
REM ===================================================================
:HOME_REGION
@echo Be in Home-Region %ADDRESS%
REM ===================================================================
REM ============================================================
REM Check VPN
REM ============================================================
rem ============ WinVPN
@set ADDRESS=10.10.0.11
@ping -n 1 %ADDRESS%>nul
@if %errorlevel%==0 goto OTHER_VPN
rem ============ OpenVPN
@set ADDRESS=10.10.0.12
@ping -n 1 %ADDRESS%>nul
@if %errorlevel%==0 goto HOME_OS_VPN
@GOTO HOME_START
@echo ==================================
@echo Map al Other Drives
@echo ==================================
:OTHER_VPN
:HOME_START
@echo ==================================
@echo Mappt alle HOME Laufwerke
@echo ==================================
REM JOE-TPN2 **********************************************
:JOE-TPN2
@set ADDRESS=JOE-TPN2
@set USER=xxxx
@set PWD=xxxx
@call %BATCH% /MAP R %ADDRESS% C$ %USER% %PWD%
@call %BATCH% /MAP S %ADDRESS% D$ %USER% %PWD%
@call %BATCH% /MAP T %ADDRESS% E$ %USER% %PWD%
@call %BATCH% /MAP U %ADDRESS% F$ %USER% %PWD%
@if NOT "%TARGET%"=="" @GOTO EXIT1
REM VIRTUALBOX**********************************************
:VIRTUALBOX
@set ADDRESS=virtualBox
@set USER=xxxx
@set PWD=xxxx
@call %BATCH% /MAP V %ADDRESS% C$ %USER% %PWD%
@if NOT "%TARGET%"=="" @GOTO EXIT1
REM JoeHome **********************************************
:JOEHOME
@set ADDRESS=xxxx
@Set USER=xxxx
@set PWD=klingone
@call %BATCH% /MAP I %ADDRESS% system %PWD%
@call %BATCH% /MAP J %ADDRESS% Server %PWD%
@call %BATCH% /MAP L %ADDRESS% progs %PWD%
@call %BATCH% /MAP M %ADDRESS% data %PWD%
@call %BATCH% /MAP N %ADDRESS% backup1 %PWD%
@call %BATCH% /MAP O %ADDRESS% backup2 %PWD%
@if NOT "%TARGET%"=="" @GOTO EXIT1
:ENDE
@echo ==================================
@net use
@pause
goto EXIT1
REM ============================================================
REM Ende normal processing - Start Functions
REM ============================================================
rem =============================================
REM Set all paramters independend their position
:GETPARAMETER
@set var=%2
@set var2=%var:~0,2%
rem echo var=%var% var2=%var2%
@if "%var%"=="/SC"; @Set SINGLECALL=1
@if "%var%"=="/V"; @set VERBOSE=1
@if "%var%"=="/Q"; @set QUIET=1
@if "%var%"=="/D"; @set DELETE=1
@if "%var%"=="/N"; @set NOPING=1
@if "%var2%"=="/U"; @set LOCALUSER=%var:~3,40%
@if "%var2%"=="/T"; @set TARGET=%var:~3,40%
@GOTO EXIT
REM ============================================================
REM Map Drives
REM ============================================================
:MAPPING
@if "%1"=="/MAP"; @SHIFT
@set _DRIVE=%1
@set _ADDRESS=%2
@set _DIRPART=%3
rem Remove double quotes
@set _DIRPART=%_DIRPART:"=%
@Set _USER=%4
@set _PWD=%5
@set _PARAM1=%6
@if "%VERBOSE%"=="1" @echo . Drive:%_DRIVE% - Adress:%_ADDRESS% - Dir:%_DIRPART% - User:%_USER% - Pwd:%_PWD%
@if not "%_ADDRESS%"=="%LASTADDRESS%" @goto CHECK_ONLINE
@if not "%ONLINE%"=="1" @goto MAPPING_ENDE
goto MAPPING_CONTINUE
:CHECK_ONLINE
@set LASTADDRESS=%_ADDRESS%
if not "%_PARAM1%"=="NOPING" @GOTO DOCHECK
SET _PARAM1=
@goto MAPPING_START
:DOCHECK
@if not "%NOPING%"=="1"; @ping -n 1 -w 400 %_ADDRESS%>nul
@if %errorlevel%==0 goto MAPPING_START
@echo --- %_ADDRESS% is not available
@set ONLINE=0
goto MAPPING_ENDE
:MAPPING_START
@set ONLINE=1
@echo + Mapping devices for %_ADDRESS%
:MAPPING_CONTINUE
@if "%VERBOSE%"=="1" @ECHO net use %_DRIVE%: "\\%_ADDRESS%\%_DIRPART%" /User:%_USER% %_PWD% %_PARAM1% %7
@net use %_DRIVE%: /Delete /YES >nul 2>%BASEPATH%\error.txt
@grep "Systemfehler" %BASEPATH%\error.txt >nul
@if %errorlevel%==0 @echo XXX ERROR: Systemfehler duriong /DELETE: %_DRIVE% - %_ADDRESS%/%_DIRPART%
@net use %_DRIVE%: "\\%_ADDRESS%\%_DIRPART%" /User:%_USER% %_PWD% %_PARAM1% %7 >nul 2>%BASEPATH%\error.txt
@grep "85" %BASEPATH%\error.txt >nul
@if %errorlevel%==0 (
@echo XXX ERROR: Drive was already used: %_DRIVE% - %_ADDRESS%/%_DIRPART%
@Goto MAPPING_CONTINUE
)
@grep "1219" %BASEPATH%\error.txt >nul
@if %errorlevel%==0 (
@echo XXX ERROR: Multiple Usernames used: %_DRIVE% - %_ADDRESS%/%_DIRPART%
@Goto MAPPING_CONTINUE
)
@grep "Systemfehler" %BASEPATH%\error.txt >nul
@if %errorlevel%==0 (
@echo XXX ERROR: Systemfehler: %_DRIVE% - %_ADDRESS%/%_DIRPART%
@cat %BASEPATH%\error.txt
)
:MAPPING_CONTINUE
@DEL %BASEPATH%\error.txt
:MAPPING_ENDE
GOTO EXIT
REM ============================================================
REM End of batch
REM ============================================================
:EXIT1
@SET VERBOSE=
@SET ADDRESS=
@SET LASTADDRESS=
@Set SINGLECALL=
@set VERBOSE=
@set QUIET=
@set LOCALUSER=
@set TARGET=
@set DELETE=
@set NOPING=
:EXIT
Zur SysArc Homepage ...