PDA

Click to See Complete Forum and Search --> : Yet Another Batch File Tutorial


jethro
February 24th, 2002, 09:48 PM
I wrote this ages ago, when I first joined AntiOnline. I have seen other posts regarding batch files, but are they this pretty?

Introductory Note: If I say anything that is hilariously obvious, I'm extremely sorry.

Batch files are basically the same as shell scripts but different commands, obviously. They are meant to save you time so you don't have to keep performing the same action over and over again but they can also be constructed to do so much more (within reason), like test for vulnerabilities in a server, DoS someone or even become a lethal virus or trojan. Seeing as we don't want it to do any of these, except maybe the first, we'll take it slowly.



Here is a sample "Hello World" program:

**************** helloworld.bat *************

@echo off
cls
echo Hello World

*********************************************

Basically what that does is clear the screen (cls) and print out "Hello World". The @echo off is so that you don't see the actual commands themselves and the command prompt. Try it with and without the @echo off and you will see what I mean.

Now save all that as helloworld.bat and either double-click it or call it from the command prompt: "helloworld.bat" or "helloworld" will both do, when you are calling it from the prompt.

Batch files are really easy and useful as long as you have a list of interesting commands. Batch files can do anything from test vulnerabilities in servers (like is there an anonymous login?) to extremely dangerous virii to even solve world hunger. But I think batch files were orginally intended to move files and create directories. Oh well, welcome to the 21st century.

Here is another program:
It can do a load of stuff. It can even test for an anonymous login through FTP. If you want it to do that, when you are calling the program from the command prompt enter in a server after the name. For example: "jethbat.bat www.myserver.com"


******************** jethbat.bat *****************************


@echo off

cls

rem This is a remark.. eh.. hello?

echo.
echo.
echo Welcome to the Amazing Program for all you Crayzee Catz
echo --------------------------------------------------------
echo.

echo Where do you want to go today? (tm)

echo 1) Give me a list of all my screensavers
echo 2) Test out the server (%1) for anonymous logins
echo 3) Send Jethro an email
echo 4) Go to AntiOnline.com
echo 5) Give me a list of open ports
echo 6) Tell me my IP address
echo 7) Tell me what Windows version I am using
echo 8) Open up my Windows Directory
echo 9) Create a directory on my desktop and call it 'jethro'

echo.

choice /c:123456789 Make your selection now:

IF ERRORLEVEL 9 goto createdir
IF ERRORLEVEL 8 goto openwindows
IF ERRORLEVEL 7 goto winversion
IF ERRORLEVEL 6 goto tellmemyip
IF ERRORLEVEL 5 goto openports
IF ERRORLEVEL 4 goto antionline
IF ERRORLEVEL 3 goto emailjethro
IF ERRORLEVEL 2 goto testserver
IF ERRORLEVEL 1 goto screensavers

goto end

:screensavers
dir C:\*.scr /B /S /W
goto end

:testserver
echo open %1>C:\testserver.log
echo anonymous>>C:\testserver.log
echo yahoo@yahoo.com>>C:\testserver.log
ftp -s:C:\testserver.log
del C:\testserver.log
goto end

:emailjethro
start mailto:jethrojones@gmx.net
goto end

:antionline
start http://www.antionline.com
goto end

:openports
netstat -a
goto end

:tellmemyip
winipcfg /batch C:\winipcfg.000
type C:\winipcfg.000
del C:\winipcfg.000
goto end

:winversion
ver
goto end

:openwindows
start C:WINDOWS
goto end

:createdir
IF EXIST C:\WINDOWS\Desktop\jethro\*.* goto end
md C:\WINDOWS\Desktop\jethro
echo Jethro Folder > C:\WINDOWS\Desktop\jethro\jethro.txt
goto end

:end
pause
echo Thank you for using my program
echo The end


************************************************************


There are a number of commands here you might not have come across. I will do my best to go through any possible new commands here.

~~~~~~~~~~~~~~~~~~~~~~~~~~

REM - This is where you can put in a command. Like the or the // C++ comment and the ' BASIC comment.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ECHO. - This prints out a blank line

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CHOICE - This gives you an INPUT prompt with a number of choices. For example, used with the /c:123456789 attribute you can have the choices 1,2,3,4,5,6,7,8 or 9. If I had used /c:abzqrst3291, your options would have been a,b,z,q,r,s,t,3,2,9 or 1. If you leave the /c switch out your options are either y or n. The program continues when they input one of the options.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

IF - Anyone who has any experience with any language will know what this is. The IF conditional statement is like a universal command. It can take many forms such as "IF (variable == value) command". In MS-DOS it takes the more BASIC form. "IF %variable% == value COMMAND". But the IF doesn't have to be like this. There are many other ways of using the if. Like in the following:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ERRORLEVEL - The choice command returns a value to the batch file when it is finished. This is called the errorlevel. Many programs (mostly the older ones) do this, if they are meant to help you in batch files. The errorlevel after the choice command, tells us which option was chosen. For example, if the errorlevel is 2, that means the second option was chosen. If the errorlevel was 8, that means the eigth option was chosen.

For example, if I had used the choice command with the switch /c:abc, the chosen option was the either going to be the first (a) the second (b) or the third (c) option. So if the errorlevel came back as 3 that would mean that the third option had been chosen: c. God, I'm sure there is a way to condense that explanation :).

(NOTE: When you are using the IF ERRORLEVEL N command thing, you must go in descending order, meaning the highest number first, down to the lowest number last. Notice the way I started with IF ERRORLEVEL 9 and moved down to IF ERRORLEVEL 1. You must do this, because if you start from IF ERRORLEVEL 1, the first condition will just be chosen and that would defy the whole point)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GOTO - The goto command is on many languages. Some programmers for some reason don't like this option, but I do. Basically, it sends the program to another part of the code. Usually only used with conditional statements like IF.

For example in the following line: "IF ERRORLEVEL 9 goto createdir", I wanted it to go to a label called "createdir" if the ninth option had been chosen.
If it had proceeded with the program, it would have gone to "IF ERRORLEVEL 8" but I didn't want that. I wanted to move it a label called "createdir" there it would make a folder on the desktop. When you are creating a label you must have it in the ":name" format but when you are sending the program to the label you only need to refer to it by the name, without the colon ":".

In "createdir" it checks if there is a folder on the desktop called "jethro" and if there isn't it makes one. At the end of the label, I didn't want it to continue so I sent it to another label, called ":end". All the labels, in this program, when they are finished, get sent to this label, because here it just gives a little message saying "Thank You" and it exits.

GOTO end - I had that at the bottom of all the errorlevel things so that if none of the conditions were met, I don't know how that would happen but anyway, it would go to the end label.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dir C:\*.scr /B /S /W - You are probably all aquainted with the dir command. It is like the "ls" command in UNIX. It gives you a list of all the files in the current directory. I made it find any *.scr files in the C:\ directory. I added three switches with it:
/B - This is so that is just gives the Bare neccesities (of life :). The name and nothing else, no modification date or size.

/S - This is so that it checks all the Subfolders: /WINDOWS, /PROGRA~1, all of them. When used in the C:\ directory, it effectievely searches the whole computer.
/W - This gives you the list in Wild list format, ie. in columns.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

echo open %1>C:\testserver.log - There are a couple of things in action here. First off all, we ECHO the message "open %1". %1 is substituted for anything you may have entered after jethbat.bat if you called it from the command prompt.

For example, in the command prompt, if you had called jethbat with the command: "jethbat.bat", %1 would just be a blank space. If you had entered: "jethbat.bat [servername]", %1 would be subsituted for the server name.

If you had entered "jethbat.bat christmas tree shiny lights", %1 would have been substituted for "christmas." %2 would equal tree, %3=shiny and %4=lights. %0 would be jethbat.bat or if you had left out the .bat part (which you can do) it would just be "jethbat". But for the sake of this example, I have only included the %1 variable.

But why wasn't "open %1" printed out to the screen? Well my inquistive friend, it's because I have included the ">" redirection. This means that the echo will be put into a NEW file called C:\testserver.log. In the following two lines, I have the ">" as a double, ">>" because this means that it is to APPEND to the named file, not create a new file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

FTP -s:C:\testserver.log - This fires up the FTP program using C:\testserver.log as it's command sequence. C:\testserver.log contains instructions for it to try and FTP to the server %1, use an anonymous login name and use a Yahoo! email address. You will see if it has worked. Of course you could do this yourself very easily, but think of the seconds you are saving by running this batch file. Astounding.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DEL C:\testserver.log - Destroy the evidence :). This command simply deletes the file. No questions asked. If you want there two be an "Are you sure prompt?" after this command, just use the /P switch with the command.
(NOTE: Any file deleted in MS-DOS is not sent to the Recycle Bin, it is completely removed altogether!)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

START - This command opens any directory or file in it's default program, specified in the registry. For example a mailto: address will be opened in Outlook (by default that is, unless you have changed it), http:// will be opened in your default browser, a folder will be opened in Windows...etc

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Netstat - If you don't know what netstat is... Basically netstat tells you a load of stuff about your connections. With the -a it gives you a list of open ports. Time to pray and hope to God you don't have a Trojan :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

winipcfg - This gives you information basically about your internet protocol thing. Used with the /batch winipcfg.000 switch it saves all that to a file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

type winipcfg.000 - This types out all the information in winipcfg.000 to the screen.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ver - This gives you the version of your operating system.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

IF EXIST - This checks whether a file exists or whether it doesn't. If it does and the condition is met, I have it go to the end, because there is no point trying to make a new folder that already exists. I have it check for any *.* existing file in the jethro folder and if there aren't, I just take it for granted that there is no jethro folder there and I make one (and place a file in it, so that it won't be just an empty folder, if you run the program again it will skip trying to make one).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

md - This creates a directory.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pause - This gives the prompt "Press any key to continue...".
("where's the any key" - Quote courtsy of Homer Simpson).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

That's what the program does anyway...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Some other useful commands
=========================
rd - This deletes an EMPTY folder

deltree - This just deletes the folder, empty or not. /Y supresses the "are you sure?" prompt.

format - Format a drive

cd - Change the working directory.

time - Gives you the time

date - Gives you the date

prompt [message] - Changes the prompt. $p$g means the it gives the working directory and a >. But you can use things like "prompt ->" if you want it to look like one of those computer terminals that you see in the movies and jut use the CD command spontaneously to see what dir you are in.

CHDIR - CD

MKDIR - MD

RMDIR - RD

EXIT - Quit dos

MEM - Memory Information

[Drive] - Move to a drive. The CD doesn't work if you want to move to a completely different drive altogether.

EDIT - This opens MS-DOS' lovely text editor edit.com

COPY [file] [destination] - Copy a file to a new directory

MOVE [file] [destination] - Move a file to a new directory

RENAME [oldfilename] [newfilename] - Renames a file

REN - RENAME

TELNET [server] [port] - Telnets into a server through a specified port

ATTRIB - Displays or changes file attributes. Use the /? switch for help.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

That's all I can think of at the moment, but there are literally tons if you are just willing to look for them. I have never been great at explaining things, hence the length of this tutorial which could probably be condensed into a much smaller size.

cmnoop
February 24th, 2002, 09:50 PM
You might wanna sort out them smilies.

jethro
February 24th, 2002, 09:51 PM
Yeah I sorted them out just then 30 seconds after I posted it. Jesus, you are amazingly quick

Originally posted here (http://www.AntiOnline.com/showthread.php?threadid=220022#post461477) by cmnoop
You might wanna sort out them smilies.

cmnoop
February 24th, 2002, 09:53 PM
Speed is required at times!

VictorKaum
February 24th, 2002, 09:56 PM
Hey Jethro,
nice prog but I have a little remark:

your prog only works properly in WIN9x / ME cause winipcfg is not the proper command in Win NT / Win2K, for Win NT4.0 and Win2K it's ipconfig.

cmnoop
February 24th, 2002, 09:59 PM
Now thats speed!

VictorKaum
February 24th, 2002, 10:05 PM
However Jethro nice tutorial....

jethro
February 24th, 2002, 10:38 PM
Thanks for the compliment. Yeah, I had forgotten that WINIPCFG wouldn't work on other systems, ME clouds the brain.

Geric
February 24th, 2002, 10:44 PM
Nice post....

*Note* the choice command does not work with 2000.

Ennis
February 24th, 2002, 10:52 PM
Good post Declan!

jethro
February 24th, 2002, 11:18 PM
Thanks Ennis. When I saw you call me Declan, I just kind of froze and then I remember that I have posted my website on my profile and it has just about everything about me on that.

Let's abolish the use of handles
Yours in bycot,
Jethro "Declan" Jones

##prisoner##
February 25th, 2002, 12:12 AM
Really nice post, jethro!! Thanx for good info.

intruder
February 25th, 2002, 11:09 AM
hey that was greeeeeeeeeeeeeeeeeeat post... i learned many things thanks to jetro..

intruder...

Ennis
February 25th, 2002, 11:43 AM
Hey I checked out the ol site, nice stuff jethro!

sandsword2
February 25th, 2002, 12:24 PM
Great post. Some people are still responsibe here on ao. I save the tutorial for later use. I have been working on batch, but I was in the fog on a few commands. Thanks for the help.

VicTT
February 27th, 2002, 03:42 PM
Nice post...although you could get rid of the blanks and get rid of a few "goto's"...Nice tutorial,and I agree that ME clouds your mind(in fact ME sucks),that's my opinion anyhow!

darkangel007
March 3rd, 2002, 05:42 AM
Ah Nostalgia!! And a good tutorial too. I needed to brush up on some of the stuff myself. Hope we get some more postings in this vein.

DA007

3ntropy
March 3rd, 2002, 06:01 AM
Wow,
Its really been a while for Batch, but I think it is still being used quite a bit for as old as it is. Dos and all, you know.

Dygear
August 19th, 2003, 02:50 PM
ok this is what i made ....

@echo off

cls

rem This is a remark.. eh.. hello?
rem thanks to jethro ... i used his program as a base

echo Pressing CTRL+C cancels theis batch file program
echo Where do you want to go today? (tm)

echo 1) Give me a list of what in the amx forlder
echo 2) Give me a list of what in the dlls forlder
echo 3) Give me a list of what in the examples forlder
echo 4) Give me a list of what in the plugins forlder
echo 5) Give me a list of what in the logs forlder
echo 6) Give me a list of what in the compiled forlder
echo 7) Give me a list of what in the include forlder
echo 8) Send Dygear an email
echo 9) Go to amxmod.net

echo.

choice /c:123456789 Make your selection now:

IF ERRORLEVEL 9 goto amxmod
IF ERRORLEVEL 8 goto emailDYGEAR
IF ERRORLEVEL 7 goto include
IF ERRORLEVEL 6 goto compiled
IF ERRORLEVEL 5 goto logs
IF ERRORLEVEL 4 goto plugins
IF ERRORLEVEL 3 goto examples
IF ERRORLEVEL 2 goto dlls
IF ERRORLEVEL 1 goto amx

goto end

:amx
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your amx forlder amx.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx
dir > amx.txt
PAUSE
goto end

:dlls
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your dlls forlder dlls.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx\dlls
dir > dlls.txt
PAUSE
goto end

:examples
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your examples forlder examples.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx\examples
dir > examples.txt
PAUSE
goto end

:plugins
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your plugins forlder called plugins.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx\plugins
dir *.amx > plugins.txt
PAUSE
goto end

:logs
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your logs forlder called logs.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx\logs
dir > logs.txt
PAUSE
goto end

:compiled
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your compiled forlder called compiled.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx\examples\compiled
dir > compiled.txt
PAUSE
goto end

:include
ECHO This program was created by Dygear
ECHO go to amxmod.net for some fun ...
ECHO this will make a file in your include forlder called include.txt
cd C:\Sierra\Counter-Strike\cstrike\addons\amx\examples\include
dir > include.txt
PAUSE
goto end

:emailDYGEAR
start mailto:DYGEAR@AOL.COM
goto end

:amxmod
start http://www.amxmod.net
goto end

:end
pause
echo Thank you for using my program
echo The end

... and it allways comes up with unknow command choice ... i am using windows XP

keezel
August 19th, 2003, 03:36 PM
dude.....this post has been dead for a year and a half now....please don't drag up old stuff (unless it's for personal use/reference of course).

Dygear
August 19th, 2003, 04:20 PM
this is for personal use and reference, of course ...

could you or some one help

Dygear
August 20th, 2003, 01:38 AM
is there a way to get what key there user is pressing ... ie
if user_presses(1)
run(1.bat)

or

if user_presses(1)
run(goto 1)

or eney think like that ...

VicE$DoS$
August 20th, 2003, 12:16 PM
Dygear,

Repost your question in a new thread - include a link to this thread.
It may stop you from being negged to death.

Cheers & Good Luck
V$D$