Shell Scripting
Shell scripting is a powerful method to automate tasks on Unix-like operating systems. A shell script is a text file with a series of commands that the shell can execute. It is often used for repetitive tasks, for instance, backups, system monitoring, and file manipulation, so that you don't have to enter each command manually.
Variables: In shell scripting, variables are placeholders or references to data that can change. Variables in shell scripts don't need to be declared and can store strings, integers, or the output of commands.
Accepting Input from User: You can accept input from the user using the read
command.
Showing Output: To show output in shell scripting, you typically use the echo
or printf
commands.
Standard Streams:
stdin
(standard input) is the stream where a program reads its input data. It's typically connected to the keyboard, but can also be redirected to read from a file or another command.stdout
(standard output) is the stream where a program writes its output data. It's typically displayed on the screen but can be redirected to files or other programs.stderr
(standard error) is the stream where a program writes its error messages. It's separate from stdout to allow users to separate regular output from error messages.
Redirecting Streams:
>
redirects stdout to a file, overwriting the file.>>
redirects stdout to a file, appending to the file.2>
redirects stderr to a file.<
redirects a file to stdin.
Conditions: Conditional statements allow you to perform different actions based on conditions.
Loops: Loops are used for executing a series of commands repeatedly.
For loop:
While loop:
Until loop:
Shell scripting is versatile and can include advanced features like functions, arrays, and signal trapping. It's a critical skill for system administrators and developers working in a Unix-like environment. Below is an example of a shell script that demonstrates core features of shell scripting in the context of a cybersecurity assessment scenario. This script will simulate the following:
Check if a list of domains is accessible.
Download the homepage of each domain.
Search for a specific pattern within the downloaded page that may indicate a security vulnerability (like an exposed admin panel or a specific software version).
Log the results and errors for review.
Please note that this script is for educational purposes only and should not be used without permission on any domain.
To run this script:
Save the script to a file, for example,
domain_check.sh
.Make the script executable with
chmod +x domain_check.sh
.Run the script with
./domain_check.sh
.
This script showcases variable usage, function definitions, conditional statements, loops, pattern searching with grep
, and network communication with wget
. The logging is split into standard output for successful checks and standard error for issues, demonstrating stream redirection.
Port Scanning Script
Below is a shell script that accepts an IP range as input and scans for open ports 21 (FTP), 22 (SSH), 80 (HTTP), and 443 (HTTPS) using nc
(Netcat). This script assumes that Netcat is installed on the system where it's executed. It uses a simple loop and timeout strategy to quickly check if the ports are open.
To use this script:
Save the script to a file, for instance,
port_scan.sh
.Make the script executable:
chmod +x port_scan.sh
.Run the script and enter the IP range when prompted:
./port_scan.sh
.
The script prompts for an IP range, calculates the integer equivalents of the start and end IPs for iteration, and uses a while
loop to scan each IP address in the range. The nc
(Netcat) command is used with the -z
flag to scan without sending data, and the -w 1
flag to set a timeout of 1 second for faster scans. The results will show only the connections that succeeded.
References:
Last updated
Was this helpful?