Академический Документы
Профессиональный Документы
Культура Документы
www.askapache.com/windows/advanced-batch-scripting.html
1/13
4/21/13
www.askapache.com/windows/advanced-batch-scripting.html
2/13
4/21/13
3/13
4/21/13
CALL :PUTTYCHECK
REM CHECK PORT IS LISTENING (FOR SOCKISFIED TUNNEL)
CALL :PORTCHECK %_PPORT%
REM CALL :SPEAK "OK"
SLEEP 100 && ECHO "OK" && SLEEP 1 && ENDLOCAL && POPD && EXIT
REM =======================================================================================
REM =
SPEAK - Speak text
REM =======================================================================================
:SPEAK
nircmd.exe speak text "%~1" 5 60
ECHO "%~1"
EXIT /B
REM =======================================================================================
REM =
RESTARTPLINK - Sleeps for %1 number of seconds
REM =======================================================================================
:RESTARTPLINK
CALL :SPEAK "%~1 ReSTARTing Plink"
REM runas /savecred /user:admin "%_PUTTYBIN% -load %_PSESSION%"
psexec.exe -i 0 -e -d -u %_RUNUSER% %_PUTTYBIN% -load %_PSESSION%
EXIT
EXIT /B
REM =======================================================================================
REM =
PORTCHECK - Check that Port is being used (for tunnels)
REM =======================================================================================
:PORTCHECK
netstat.exe -n -v -a -p TCP | FIND "ESTABLISHED" | FIND ":%~1" >nul 2>&1
IF ERRORLEVEL 1 CALL :RESTARTPLINK "PORT CHECK FAILED"
EXIT /B
REM =======================================================================================
REM =
INACTIVEPUTTYCHECK - Check for inactive putty windows
REM =======================================================================================
:PUTTYCHECK
tasklist.exe /V /NH /FI "WINDOWTITLE eq PuTTY (inactive)" 2>nul|FIND "INFO: " >nul 2>&1
IF ERRORLEVEL 1 taskkill.exe /T /F /FI "WINDOWTITLE eq PuTTY (inactive)" >nul 2>&1
:: MAKE SURE ONLY 1 putty is running that is connected to the remote server
netstat.exe -n -a -o -p TCP | FIND ":%_PPORT%" | FIND /C ":%_PPORT%" | FIND "1" >nul 2>&1
IF ERRORLEVEL 1 (
tasklist.exe /V /FO TABLE /NH /FI "IMAGENAME eq putty.exe" 2>nul | FIND /C "Running" | FIND
"1" >nul 2>&1
IF ERRORLEVEL 1 (
REM kill all running puttys (owned by system)
FOR /F "usebackq tokens=2 skip=2" %%p IN (`tasklist.exe /V /FO TABLE /NH /FI "IMAGENAME
eq putty.exe"`) DO taskkill.exe /F /PID %%p /T >nul 2>&1
CALL :RESTARTPLINK "EXTRA PUTTY FOUND"
)
)
EXIT /B
REM =======================================================================================
REM =
PINGCHECK - PING address to make sure it is reachable
REM =======================================================================================
:PINGCHECK
PING %~1 -n 1 -w 5000 | FIND "TTL=" >nul 2>&1
IF ERRORLEVEL 1 (
PING google.com -n 1 -w 500 | FIND "TTL=" >nul 2>&1
IF ERRORLEVEL 1 ( CALL :SPEAK "CHECK INTERNET CONNECTION" && SLEEP 60 )
PING google.com -n 1 -w 5000 | FIND "TTL=" >nul 2>&1
IF ERRORLEVEL 1 ( CALL :SPEAK "CHECK INTERNET CONNECTION" && SLEEP 60 )
PING google.com -n 1 -w 5000 | FIND "TTL=" >nul 2>&1
IF ERRORLEVEL 1 (
CALL :SPEAK "KILL SOCKS PROGRAMS"
SLEEP 100
pskill.exe -t putty >nul 2>&1
pskill.exe -t thunderbird >nul 2>&1
pskill.exe -t ThunderbirdPortable >nul 2>&1
)
)
EXIT /B
REM =======================================================================================
REM =
KILLDUPES - kills duplicate processes, except for the one with lowest pid
REM =======================================================================================
:KILLDUPES
REM Check that more than 1 process is running
tasklist.exe /V /NH /FI "IMAGENAME eq %~1" /FI "USERNAME eq SYSTEM" 2>nul | FIND /C "K
Running" | FIND "1" >nul 2>&1
IF NOT ERRORLEVEL 1 EXIT /B
REM Create Filename in current dir (of this script)
www.askapache.com/windows/advanced-batch-scripting.html
4/13
4/21/13
SCRIPT VARIABLES ^
These are all local to this script thanks to the SETLOCAL above, so they won't exist outside the scripts execution
environment.
www.askapache.com/windows/advanced-batch-scripting.html
5/13
4/21/13
Exit Script ^
This is the last line executed in the Main, it forces the script to exit cleanly at this point, otherwise the functions below would
all get executed. This is what allows the use of all the functions below. I end all my scripts MAIN with this.
REM ** EXIT Script
CALL :MDYE "EOF" && POPD && ENDLOCAL && GOTO :EOF
SCRIPT FUNCTIONS ^
Now then, onto the MEAT of the script, all the functions. These functions are designed for global use in other batch files, so
that the only modification when you make a new batch is the above variables and main execution.. If you know much about
batch files you will realize that creating these functions was a very painful process in some cases.. I freakin hate windows!
Anyway, enjoy!
6/13
4/21/13
REM
REM
CALL :CRYPTMOUNT "%LEONARDO%" "%LEONARDO_FILE%" "%LEONARDO_KEY%"
REM
CALL :CRYPTMOUNT "L" "%SYSTEMDRIVE%CRYPTLEONARDO" "%SYSTEMDRIVE%CRYPTLEONARDO_KEY"
REM
REM :: print the settings
REM :: CALL :MP 3 "DRIVE: %DRIVE%"&&CALL :MP 3 " FILE: %FILE%"&&CALL :MP 3 " KEY:
%KEY%"&&CALL :MP 3 " VOL: %VOL%"
:CRYPTMOUNT
SET DRIVE=%~1&& SET FILE=%~2&& SET KEY=%~3&& SET VOL=!FILE:~0,3!
CALL :MP 1 "Mounting TrueCrypt on %DRIVE% from %FILE%"
:: Check for Truecrypt or die
CALL :EXISTORQUIT "%ProgramFiles%TrueCryptTrueCrypt.exe" && CALL :EXISTORQUIT "%FILE%" &&
CALL :EXISTORQUIT "%KEY%"
:: checks that MOUNTVOL works and the drive containing the truecrypt container file is
present or dies
MOUNTVOL %VOL% /L 2>NUL | FIND "\?Volume{" >NUL 2>&1
IF ERRORLEVEL 1 CALL :MDYE "%VOL% NOT FOUND"
REM ** Converts G: to its \?Volume{234234} equivalent for greater portability
FOR /F %%i IN ('MOUNTVOL %VOL% /L') DO @SET VOL=%%i%FILE:~3%
:: IF the drive is already mounted then continue, otherwise try to mount
MOUNTVOL %DRIVE%: /L >NUL 2>&1
IF NOT ERRORLEVEL 1 (
CALL :MP 2 "%DRIVE% ALREADY MOUNTED" && EXIT /B
) ELSE (
START "Mounting TrueCrypt" /D"%ProgramFiles%TrueCrypt" /MIN /B TrueCrypt.exe /c n /b /q
background /h n /k %KEY% /l %DRIVE% /p /v %VOL% && SLEEP 10
)
:: try again in case of bad password and accidental keypress
MOUNTVOL %DRIVE%: /L >NUL 2>&1
IF ERRORLEVEL 1 (START "Mounting TrueCrypt" /D"%ProgramFiles%TrueCrypt" /MIN /B TrueCrypt.exe
/c n /b /q background /h n /k %KEY% /l %DRIVE% /p /v %VOL% && SLEEP 10)
:: IF it still doesnt exist then quit
MOUNTVOL %DRIVE%: /L >NUL 2>&1
IF ERRORLEVEL 1 CALL :MDYE "Failed to mount %FILE% on %DRIVE%"
CALL :MF && ENDLOCAL && EXIT /B
EXIT /B
RUNONE - Starts one instance of executable after verifying it exists and is not already running. ^
REM
REM
REM
REM
REM
www.askapache.com/windows/advanced-batch-scripting.html
7/13
4/21/13
:RUNONE
SETLOCAL
CALL :MP 1 "Starting %~n1"
:: SLEEP FOR NICENESS, LOCAL VARS _P2 and _P3
SLEEP 2 && SETLOCAL
SET P=%~1
ECHO %P%|FIND " " >NUL 2>&1
IF NOT ERRORLEVEL 1 ( PUSHD "%~dp1" && SET P=%~nx1 )
:: SET _P2 TO DEFAULT TO "/MIN" IF EMPTY
SET _P2=/MIN
IF NOT " %~2" == " " SET _P2=%~2
IF NOT " %~3" == " " SET _P3=%~3
IF NOT " %~4" == " " SET _P4=%~4
:: CHECK THAT EXECUTABLE EXISTS
CALL :EXISTORQUIT "%~1"
REM ECHO START %_P2% /D"%~dp1" %P% %_P3% %_P4%&& PAUSE&&
:: CHECK FOR EXISTING PROCESSNAME ( %~n1 is file name without ext, %~nx1 is the file name and
extension. )
pslist.exe /e %~n1 >NUL 2>&1
IF ERRORLEVEL 1 (START %_P2% /D"%~dp1" %P% %_P3% %_P4% ) ELSE (CALL :MP 2 "%~n1 already
running!" )
ENDLOCAL && EXIT /B
CHECKUSERVALID - checks that defined username equals %ADMINUSER% or %RUNUSER%, then returns to CALLer ^
:CHECKUSERVALID
:: EXIT IF USERNAME IS NOT DEFINED, CATCHES SYSTEM ACCOUNTS TRYING TO RUN WHEN IN GLOBAL
STARTUP
IF NOT DEFINED USERNAME EXIT
SETLOCAL
SET UP=no
SET _P1= %~1
SET _P2= %~2
IF NOT "%_P1%" == " " ( IF /I "%~1" == "%USERNAME%" SET UP=yes)
IF NOT "%_P1%" == " " ( IF /I "%~1" == "%USERNAME%" SET UP=yes)
IF NOT "%_P1%" == " " ( IF /I "%~1" == "%USERNAME%" SET UP=yes)
IF /I "bill" == "%USERNAME%" SET UP=yes
IF /I "newbill" == "%USERNAME%" SET UP=yes
IF /I "max" == "%USERNAME%" SET UP=yes
IF /I NOT "%UP%" == "yes" EXIT
ENDLOCAL
EXIT /B
[user@MACHINE] [C:DRPEPPERSCRIPTS]
> REG /?
:SETPROMPT
set PROMPT=$_[%USERNAME%@%USERDOMAIN%]$S[$P]$_$M$G && EXIT /B
www.askapache.com/windows/advanced-batch-scripting.html
8/13
4/21/13
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
:MSETCOLOR
COLOR %~1 && EXIT /B
MSETCONSOLE - sets the cols and lines of current screen buffer, then returns to CALLer ^
:MSETCONSOLE
MODE CON COLS=%~1 LINES=%~2 && EXIT /B
www.askapache.com/windows/advanced-batch-scripting.html
9/13
4/21/13
:PARAMTEST
ECHO. && CALL :MP 1 "PARAMTEST CALLED WITH: %*" && ECHO %L1%
SET _P= %~1
IF NOT "%_P%" == " " ( ECHO %%1
= %1 && ECHO %%~f1
= %~f1 && ECHO %%~d1
= %~d1 && ECHO %%~p1
= %~p1
ECHO %%~n1
= %~n1 && ECHO %%~x1
= %~x1 && ECHO %%~s1
= %~s1 && ECHO
%%~dp1
= %~dp1
ECHO %%~nx1
= %~nx1 && ECHO %%~$PATH:1
= %~$PATH:1 && ECHO %%~dp$PATH:1 =
%~dp$PATH:1 && ECHO %L1% )
SET _P= %~2
IF NOT "%_P%" == " " ( ECHO %%2
= %2 && ECHO %%~f2
= %~f2 && ECHO %%~d2
= %~d2 && ECHO %%~p2
= %~p2
ECHO %%~n2
= %~n2 && ECHO %%~x2
= %~x2 && ECHO %%~s2
= %~s2 && ECHO
%%~dp2
= %~dp2
ECHO %%~nx2
= %~nx2 && ECHO %%~$PATH:2
= %~$PATH:2 && ECHO %%~dp$PATH:2 =
%~dp$PATH:2 && ECHO %L1% )
SET _P= %~3
IF NOT "%_P%" == " " ( ECHO %%3
= %3 && ECHO %%~f3
= %~f3 && ECHO %%~d3
= %~d3 && ECHO %%~p3
= %~p3
ECHO %%~n3
= %~n3 && ECHO %%~x3
= %~x3 && ECHO %%~s3
= %~s3 && ECHO
%%~dp3
= %~dp3
ECHO %%~nx3
= %~nx3 && ECHO %%~$PATH:3
= %~$PATH:3 && ECHO %%~dp$PATH:3 =
%~dp$PATH:3 && ECHO %L1% )
SET _P= %~4
IF NOT "%_P%" == " " ( ECHO %%4
= %4 && ECHO %%~f4
= %~f4 && ECHO %%~d4
= %~d4 && ECHO %%~p4
= %~p4
ECHO %%~n4
= %~n4 && ECHO %%~x4
= %~x4 && ECHO %%~s4
= %~s4 && ECHO
%%~dp4
= %~dp4
ECHO %%~nx4
= %~nx4 && ECHO %%~$PATH:4
= %~$PATH:4 && ECHO %%~dp$PATH:4 =
%~dp$PATH:4 && ECHO %L1% )
SET _P= %~5
IF NOT "%_P%" == " " ( ECHO %%5
= %5 && ECHO %%~f5
= %~f5 && ECHO %%~d5
= %~d5 && ECHO %%~p5
= %~p5
ECHO %%~n5
= %~n5 && ECHO %%~x5
= %~x5 && ECHO %%~s5
= %~s5 && ECHO
%%~dp5
= %~dp5
ECHO %%~nx5
= %~nx5 && ECHO %%~$PATH:5
= %~$PATH:5 && ECHO %%~dp$PATH:5 =
%~dp$PATH:5 && ECHO %L1% )
SET _P= %~6
IF NOT "%_P%" == " " ( ECHO %%6
= %6 && ECHO %%~f6
= %~f6 && ECHO %%~d6
= %~d6 && ECHO %%~p6
= %~p6
ECHO %%~n6
= %~n6 && ECHO %%~x6
= %~x6 && ECHO %%~s6
= %~s6 && ECHO
%%~dp6
= %~dp6
ECHO %%~nx6
= %~nx6 && ECHO %%~$PATH:6
= %~$PATH:6 && ECHO %%~dp$PATH:6 =
%~dp$PATH:6 && ECHO %L1% )
SET _P= %~7
IF NOT "%_P%" == " " ( ECHO %%7
= %7 && ECHO %%~f7
= %~f7 && ECHO %%~d7
= %~d7 && ECHO %%~p7
= %~p7
ECHO %%~n7
= %~n7 && ECHO %%~x7
= %~x7 && ECHO %%~s7
= %~s7 && ECHO
%%~dp7
= %~dp7
ECHO %%~nx7
= %~nx7 && ECHO %%~$PATH:7
= %~$PATH:7 && ECHO %%~dp$PATH:7 =
%~dp$PATH:7 && ECHO %L1% )
SLEEP 3
CALL :MF
EXIT /B
www.askapache.com/windows/advanced-batch-scripting.html
10/13
4/21/13
:EXAMINEFILE
CALL :MP 1 "Examine File %~1"
CALL :EXISTORQUIT "%~1"
STRINGS "%~1" | FINDSTR /R /C:"[A-Z][A-Z]="
CALL :MF
EXIT /B
RR - IF file %1 EXISTs then :MT "Removing %1" then :MF, then ( or IF %1 not EXISTs) returns to CALLer ^
:RR
CALL :MP 1 "Removing %~1"
IF EXIST "%~1" ERASE /q "%~1"
CALL :MF && EXIT /B
:SHUTDOWNIN
SHUTDOWN -r -t "%~1" && EXIT /B
www.askapache.com/windows/advanced-batch-scripting.html
11/13
4/21/13
:TASKS
SET _P=%~1
SET _PP= %~1
IF "%_PP%" == " " EXIT /B
REM SORTABLES
IF /I "%_P%" == "pid" ( tasklist.exe /V /NH | SORT /+29 && EXIT /B )
IF /I "%_P%" == "size" ( tasklist.exe /V /NH | SORT /+59 && EXIT /B )
IF /I "%_P%" == "user" ( tasklist.exe /V /NH | SORT /+89 && EXIT /B )
IF /I "%_P%" == "time" ( tasklist.exe /V /NH | SORT /+138 && EXIT /B )
IF /I "%_P%" == "window" ( tasklist.exe /V /NH | SORT /+152 && EXIT /B )
REM FILTERS
IF /I "%_P%" == "image" ( tasklist.exe /V /NH /FI "IMAGENAME eq %~2" && EXIT /B )
IF /I "%_P%" == "username" ( tasklist.exe /V /NH /FI "USERNAME eq %~2" && EXIT /B )
IF /I "%_P%" == "running" ( tasklist.exe /V /NH /FI "STATUS eq Running" && EXIT /B )
IF /I "%_P%" == "status" ( tasklist.exe /V /NH /FI "STATUS eq %~2" && EXIT /B )
CALL :MF
EXIT /B
MF - SLEEPs for 1 second, then prints out completed message, followed by 2 blank lines, then returns to CALLer ^
:MF
SLEEP 1 && ECHO [COMPLETED] && ECHO. && ECHO. && EXIT /B
MM - prints blank line, L1, changes title of the interpreter window to %~1, prints >>> %~1..., L2, blank line, then returns to
CALLer ^
:MM
SLEEP 1 && ECHO. && ECHO %L1% && title +++ %~1... && ECHO %P1% %~1... && ECHO %L2% && ECHO.
&& EXIT /B
MT - prints blank line, L1, changes title of the interpreter window to %~1, prints >>> %~1..., L2, blank line, then returns to
CALLer ^
:MT
CALL :MM "%~1" && CALL :SPEAK "%~1" && EXIT /B
MP3 - ECHO %~1, speak %~1 with nircmd.exe, then returns to CALLer ^
:MP3
CALL :MP 1 "%~1" && CALL :SPEAK "%~1" && EXIT /B
www.askapache.com/windows/advanced-batch-scripting.html
12/13
4/21/13
:MDYE
SETLOCAL
SET _M= %~1
IF NOT "%_M%" == " " SET _M=REASON: %~1
CALL :MP 1 "EXITING SCRIPT... %_M%" && ECHO. && ECHO.
ENDLOCAL && EXIT /B
EOF: Thoughts ^
So what did you think? I have around 20 batch scripts that utilize these and other functions to do all sorts of cool things. One
takes a screenshot of my desktop every 10 minutes and saves it for a real-cool archive of my activity. Another lets me edit a
boot.ini file with 1 command.. And another runs when I insert a USB drive to automatically mount a truecrypt volume and
create SSH tunnels in the background by using Plink, AT, and the runas.exe command.
If you want to program, please use linux... If you need to write a Windows batch file, I hope this helps.
September 3rd, 2010
Securing php.ini and php.cgi with .htaccessMagic in the Terminal: Screen, Bash, and SSH
Comments Welcome ^
It's very simple - you read the protocol and write the code. -Bill Joy
RSS(http://feedvalidator.org/check.cgi?url=http://www.askapache.com/feed/) | XHTML
1.1(http://validator.w3.org/check/referer?ss=1;outline=1;sp=1;debug) | CSS 2.1(http://jigsaw.w3.org/cssvalidator/check/referer?warning=0)
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0
License(http://creativecommons.org/licenses/by/3.0/) , just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation
produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd(http://hoohoo.ncsa.illinois.edu/) .
UNIX is a registered Trademark of The Open Group(http://www.opengroup.org/) . POSIX is a registered Trademark of
The IEEE(http://standards.ieee.org/) .
Charles Torvalds(https://plus.google.com/101616558702207714980)
Site Map | Contact Webmaster | License and Disclaimer | Terms of Service
Main(http://www.quantcast.com/p-5e44cjdXWaqOA) (http://www.alexa.com/data/details/main/www.askapache.com)
www.askapache.com/windows/advanced-batch-scripting.html
13/13