Hack Like a Pro: Linux Basics for the Aspiring Hacker, Part 16 (Stdin, Stdout, & Stderror) « Null Byte :: WonderHowTo

Welcome back, my budding hackers!Inprevious tutorials, we've looked at some of the basic commands and concepts for using Linux. Along the way, I realized that I've failed to provide you with some basic background material on thestdin,stdout, andstderror.In human language, these are often referred to as standard input (stdin), standard output (stdout), and standard error (stderror). These represent how and where Linux sends the output from your commands (stdout), where it receives its input (stdin), and where it sendsits error messages (stderror).Since you've now been using Linux for awhile, you realize that both standard output and standard error are sent to your computer screen. Both as the name implies, this is the standard place and not necessarily the only place. Linux let's us define where our output and error messages should go.Before we go any further, let's take a moment to define some terms.Standard Output (1)Whenever you complete a command, it must know where to send the output. You might want to send it to a file, a printer, the screen, etc. The default for standard output is the computer screen. Standard output is often referred to as stdout or simply use the numeric representation of 1.Standard Input (0)Standard input is where the program or command gets the information it needs. By default, in Linux this is the keyboard, but can be a file, etc. Standard input is often referred to as stdin or simply represented by the numeric representation of 0.Standard Error (2)When we make a mistake or our program throws an error, it send the error message to stanadard error. By default, this is our computer screen. Standard error is often referred to as stderror or simply represented by the numeral 2.When we want to direct any of these three from the command line or a script, we use the numeric representation of each, 0 for stdin, 1 for stdout, and 2 for stderr.Step 1: List Two DirectoriesTo demonstrate how we can use and manipulation these I/O streams, let's do a listing of two different directories,/etc/hostsand/etc/snort.In Linux, you can do listings of more than one directory at a time. The/etc/snortdirectory is where our configuration file for snort resides and/etc/hostsis a directory where we can set static name resolution in Linux (I'll do a new Linux tutorial on DNS and name resolution in Linux soon).If we wanted to see the two directories, we could type:ls /etc/hosts /etc/snortAs you can see, the listing comes back to us by the standard output to our computer screen showing us the listing of both directories.Now, let's try the same thing, but this time let's list a directory that doesn't exist, such as/etc/aircrack-ng.ls /etc/hosts /etc/aircrack-ngAs you can see, our BASH shell comes back with two outputs, the standard output frometc/hostsand the standard error from the non-existent directory.Step 2: Send Standard Output to a FileNext, let's suppose that we want to separate our standard output from our standard error. Imagine we're running a script where we don't want to see our output messages until after the script has run, but we need to see error messages on our screen immediately. We could rewrite our command as:ls /etc/hosts /etc/aircrack-ng 1>goodoutputLet's now imagine just the reverse of our previous scenario where instead we want to see our output on screen, but store our error messages to a separate file for viewing later. We could write:ls /etc/hosts /etc/aircrack-ng 2>erroroutputNow, after the command has been run, we can go back andcattheerroroutputfile to view any possible error messages.Step 3: Send Standard Output & Standard Error to Separate FileNow, let's imagine a script where we want both our standard output and standard error to be directed to separate files for viewing later. We can type:ls /etc/hosts /etc/aircrack-ng 1>goodoutput 2>erroroutputNotice that nothing comes back to our screen, neither standard output or standard error.Step 4: Send Both Standard Output & Standard Input to Same FileFinally, what if we wanted both standard error and standard output to be written to same file? We could type:ls /etc/hosts /etc/aircrack-ng >goodoutput 2>&1Notice that I did not use the 1 before the>goodoutputas BASH defaults to stdout if no number is used.Hope you enjoyed this quick lesson in Linux stdin, stdout, and stderror, but we have so much more coming, sokeep coming backmy budding hackers!Want to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseRelatedNews:How to Study for the White Hat Hacker Associate Certification (CWA)How To:The Essential Skills to Becoming a Master HackerHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 11 (Apache Web Servers)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 6 (Networking Basics)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 1 (Getting Started)How To:Perl for the Aspiring Hacker - Part 1 - VariablesHow To:Linux Basics for the Aspiring Hacker: Managing Hard DrivesHow to Hack Like a Pro:Getting Started with MetasploitHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 13 (Mounting Drives & Devices)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 12 (Loadable Kernel Modules)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 2 (Creating Directories & Files)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 21 (GRUB Bootloader)News:You've Been HackedHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 5 (Installing New Software)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 9 (Managing Environmental Variables)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 25 (Inetd, the Super Daemon)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 22 (Samba)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 4 (Finding Files)Hack Like a Pro:Perl Scripting for the Aspiring Hacker, Part 1Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 17 (Client DNS)Goodnight Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingNews:Let Me Introduce MyselfCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 1 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 2 - Legal Hacker TrainingHack Like a Pro:Hacking Samba on Ubuntu and Installing the MeterpreterGoodnight Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 4 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingCommunity Byte:HackThisSite, Realistic 2 - Real Hacking SimulationsCommunity Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingHow To:How Hackers Take Your Encrypted Passwords & Crack Them