Simple Command Line Tricks
By Ozzy_98
CompTIA A+\Net+ MCP\MCSA\MCSE (2000)

The command line interface (CLI) is the windows user's best friend. It's also a computer hacker's best friend too. Telnet into a windows system, what do you get? Command prompt. So this is going to cover some basic commands and tricks for the command prompt.



-----------
Redirection
-----------

Redirection is one of the simplest tricks you can learn the CLI. It sends all output from a command into a file. Like this:

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.

c:\windows>Dir /w > Dir.txt

c:\windows>



Now all the files that would have been sent to the screen are now in Dir.txt. If there was a file called dir.txt, it was over wrote. A single greater then sign overwrites the file, two of them append. So using this:


Microsoft Windows XP [Version 5.1.2500]<

c:\windows>Dir a*.* /w > Dir.txt

c:\windows>Dir b*.* /w >> Dir.txt

c:\windows>



The first command will delete any file called Dir.txt and fill it with the results from dir a*.* /w, while the second command appeneds dir b*.* /w to the end of the file. So remember, > will erase a file, >> will append data.

Now, what will happen if you did this:

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.


c:\windows>Dir afgfertretetre > Dir.txt



Unless you have a file named afgfertretetre, it shouldn't find it, and will send the file not found line into dir.txt, right?

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.


C:\Batch Files>dir fgdfgsd >test.txt
File Not Found

C:\Batch Files>



The > and >> by default only redirect sucessful commands. Any command with an error will be displayed. Why? When you use just a > or >>, the command line add's in a 1 in front of it. a 1> and > are the same thing. To grab error data, you need to use a 2> or 2>>. Like this:


Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.


C:\Batch Files>dir fgdfgsd 2>Error.txt

C:\Batch Files>



And yes, you can use both.

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.


C:\Batch Files>dir dfsfdsfs*.* 1> good.txt 2>Error.txt

C:\Batch Files>type good.txt
Volume in drive C is 1 - Windows
Volume Serial Number is F8ED-8503

Directory of C:\Batch Files


C:\Batch Files>type Error.txt
File Not Found

C:\Batch Files>



This can give you problems too. Since 1> and 2> are commands, if you want to redirect a 1 or 2 into a file, you need a space. And so there will be a space at the end of the file. and sometimes a space at the end of the line can cause the program reading the file to error out. For example, using Echo's to write an ini file, you might get problems in this area.

The second for of redirection goes the other way, it redirects data into a program. It is NOT the same as a parameter. This redirection is only for programs that ask you questions in the command line. A good command to test this with will be netsh, a program I plan on covering later. Netsh has two modes, you can use it for a single command, like this:
Microsoft Windows XP [Version 5.1.2500]<
C:\WINDOWS\system32>netsh firewall set opmode disable
Ok.


C:\WINDOWS\system32>


The program can also have an interactive interface like this:

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>netsh
netsh>interface
netsh interface>show in

Admin State State Type Interface Name
--------------------------------------------------------------------------
Enabled Dedicated Local Area Connection
Enabled Dedicated VMware Network Adapter VMnet1
Enabled Internal Internal
Enabled Loopback Loopback

netsh interface>quit


C:\WINDOWS\system32>


This can be automated with use of files and redirection. In a text file, write this script:

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.


interface
show in
quit



and save it as script.txt. Then run this command:

Microsoft Windows XP [Version 5.1.2500]
(C) Copyright 1985-2001 Microsoft Corp.

J:\>netsh < script.txt
netsh>netsh interface>
Admin State State Type Interface Name
--------------------------------------------------------------------------
Enabled Dedicated Local Area Connection
Enabled Dedicated VMware Network Adapter VMnet1
Enabled Internal Internal
Enabled Loopback Loopback

netsh interface>

J:\>


The use of < lets you automate almost any CLI program with simple scripts.
1