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:
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:
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:
Unless you have a file named afgfertretetre, it shouldn't find it, and will send the file not found line into dir.txt, right?
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:
And yes, you can use both.
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:
The program can also have an interactive interface like this:
This can be automated with use of files and redirection. In a text file, write this script:
and save it as script.txt. Then run this command:
The use of < lets you automate almost any CLI program with simple scripts. |