Book Image

Penetration Testing with the Bash shell

By : Keith Harald Esrick Makan
Book Image

Penetration Testing with the Bash shell

By: Keith Harald Esrick Makan

Overview of this book

Table of Contents (13 chapters)

Using pipes


All we've been discussing in this section is redirecting output command to another file; what about redirecting output from one command to another? Well that's exactly what the next section is for.

Note

Pipes are interprocess communication mechanisms, which are mechanisms that allow processes to communicate with one another, in operating systems that allow output from one process to be funneled from to another process as input. In other words, you can turn the standard output of one program into the standard input of another.

In fact, many pipes work exactly this way by duplicating file descriptor 0 for one process and allowing another process to write to it.

The following command shows how to use a pipe in bash speak:

[command line] | [another command line]

Please note that this time the | character, referred to literally as a pipe if used this way, is an actual part of the command invocation. Of course, [command line] would be the command you would like to invoke. The pipe will feed output from the first command line as input to the second command line argument. You can actually specify as many pipes as you your machine will accommodate, which would look something like the following syntax:

[command] | [command] | [command] | ... | [command]

The following are a few examples:

  • cat /etc/passwd | wc –l

    • This is equivalent to the following:

               wc –l < /etc/passwd
      
    • The following screenshot shows the output of the previous commands:

  • Count the number of files in the operating system's root directory using the following command:

    ls –al / | wc –l
    
  • List all available usernames using the following command:

    cat /etc/passwd | awk –F: '{print $1}'
    

    The following screenshot shows the output of the previous command:

  • List all the open services from an nmap scan using the following command:

    nmap –v scanme.nmap.org | grep –e '^[0-9]*/(udp|tcp)[\ ]*open'