Kali Linux Penetration Testing Bible. Gus Khawaja

Чтение книги онлайн.

Читать онлайн книгу Kali Linux Penetration Testing Bible - Gus Khawaja страница 24

Kali Linux Penetration Testing Bible - Gus Khawaja

Скачать книгу

(Bash script files will end with .sh ), and inside the file, you'll enter the following:

      Let's explain your first Bash script file:

       #!/bin/bash is called the Bash shebang; we need to include it at the top to tell Kali Linux which interpreter to use to parse the script file (we will use the same concept in Chapter 18, “Pentest Automation with Python,” with the Python programming language). The # is used in the second line to indicate that it's a comment (a comment is a directive that the creator will leave inside the source code/script for later reference).

       The variable name is called ROUTERIP , and its value is 10.0.0.1.

       Finally, we're printing the value to the output screen using the printf function.

      To execute it, make sure to give it the right permissions first (look at the following output to see what happens if you don't). Since we're inside the same directory ( /root ), we will use ./var.sh to execute it:

      root@kali:~# ./var.sh bash: ./var.sh: Permission denied root@kali:~# chmod +x var.sh root@kali:~# ./var.sh The router IP address: 10.0.0.1

      Congratulations, you just built your first Bash script! Let's say we want this script to run automatically without specifying its path anywhere in the system. To do that, we must add it to the $PATH variable. In our case, we will add /opt to the $PATH variable so we can save our custom scripts in this directory.

Snapshot of Export Config.

      The changes will append /opt to the $PATH variable. At this stage, save the file and close all the terminal sessions. Reopen the terminal window and copy the script file to the /opt folder. From now on, we don't need to include its path; we just execute it by typing the script name var.sh (you don't need to re‐execute the chmod again; the execution permission has been already set):

      Commands Variable

      Sometimes, you might want to execute commands and save their output to a variable. Most of the time, the goal behind this is to manipulate the contents of the command output. Here's a simple command that executes the ls command and filters out the filenames that contain the word simple using the grep command. (Don't worry, you will see more complex scenarios in the upcoming sections of this chapter. For the time being, practice and focus on the fundamentals.)

       #!/bin/bash LS_CMD=$(ls | grep 'simple') printf "$LS_CMD\n"

      Here are the script execution results:

       root@kali:/opt# simplels.sh simpleadd.sh simplels.sh

      Sometimes, you will need to supply parameters to your Bash script. You will have to separate each parameter with a space, and then you can manipulate those params inside the Bash script. Let's create a simple calculator ( simpleadd.sh ) that adds two numbers:

      #!/bin/bash #Simple calculator that adds 2 numbers #Store the first parameter in num1 variable NUM1=$1 #Store the second parameter in num2 variable NUM2=$2 #Store the addition results in the total variable TOTAL=$(($NUM1 + $NUM2)) echo '########################' printf "%s %d\n" "The total is =" $TOTAL echo '########################'

      You can see in the previous script that we accessed the first parameter using the $1 syntax and the second parameter using $2 (you can add as many parameters as you want).

      root@kali:/opt# simpleadd.sh 5 2 ######################## The total is = 7 ########################

      There is a limitation to the previous script; it can add only two numbers. What if you want to have the flexibility to add two to five numbers? In this case, we can use the default parameter functionality. In other words, by default, all the parameter values are set to zero, and we add them up once a real value is supplied from the script:

      #!/bin/bash #Simple calculator that adds until 5 numbers #Store the first parameter in num1 variable NUM1=${1:-0} #Store the second parameter in num2 variable NUM2=${2:-0} #Store the third parameter in num3 variable NUM3=${3:-0} #Store the fourth parameter in num4 variable NUM4=${4:-0} #Store the fifth parameter in num5 variable NUM5=${5:-0} #Store the addition results in the total variable TOTAL=$(($NUM1 + $NUM2 + $NUM3 + $NUM4 + $NUM5)) echo '########################' printf "%s %d\n" "The total is =" $TOTAL echo '########################'

      To understand how it works, let's look at the NUM1 variable as an example (the same concept applies to the five variables). We will tell it to read the first parameter {1 from the terminal window, and if it's not supplied by the user, then set it to zero, as in :‐0} .

      Using the default variables, we're not limited to adding five numbers; from now on, we can add as many numbers as we want, but the maximum is five (in the following example, we will add three digits):

      TIP

      If you want to know the number of parameters supplied in the script, then you can use the $# to get the total. Based on the preceding example, the $# will be equal to three since we're passing three arguments.

      If you add the following line after the printf line:

       printf "%s %d\n" "The total number of params =" $#

       you should see the following in the terminal window:

       root@kali:~# simpleadd.sh 2 4 4 ######################## The total is = 10 The total number of params = 3 ########################

      Another way to interact with the supplied input from the shell script is to use the read function. Again, the best way to explain this is through examples. We will ask the user to enter their first name and last name after which we will print the full name on the screen:

       #!/bin/bash read -p "Please enter your first name:" FIRSTNAME read -p "Please enter your last name:" LASTNAME printf "Your

Скачать книгу