Читать книгу Kali Linux Penetration Testing Bible - Gus Khawaja - Страница 68

Variables

Оглавление

What is a variable, and why does every programming language use it anyway?

Consider a variable as a storage area where you can save things like strings and numbers. The goal is to reuse them over and over again in your program, and this concept applies to any programming language (not just Bash scripting).

To declare a variable, you give it a name and a value (the value is a string by default). The name of the variable can only contain an alphabetic character or underscore (other programming languages use a different naming convention). For example, if you want to store the IP address of the router in a variable, first you will create a file var.sh (Bash script files will end with .sh ), and inside the file, you'll enter the following:

#!/bin/bash #Simple program with a variable ROUTERIP="10.0.0.1" printf "The router IP address: $ROUTERIP\n"

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.

First, open the .bashrc file using any text editor. Once the file is loaded, scroll to the bottom and add the line highlighted in Figure 2.2.


Figure 2.2 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):

root@kali:~# cp var.sh /opt/ root@kali:~# cd /opt root@kali:/opt# ls -la | grep "var.sh" -rwxr-xr-x 1 root root 110 Sep 28 11:24 var.sh root@kali:/opt# var.sh The router IP address: 10.0.0.1

Kali Linux Penetration Testing Bible

Подняться наверх