Bash Scripting > System Check

·

3 min read

Bash Scripting > System Check

<What>

I want to dive into automating simple tasks with Bash today. I’ve realized how much time I save using Bash scripts, and I’ve learned a ton about scripting. I enjoy learning Python and JavaScript, but one downside is that they often aren’t preinstalled in many environments. Bash, on the other hand, is everywhere! Whether you’re working on a remote CentOS server or a Linux cloud service, Bash is always ready to go.

So, what’s Bash scripting all about? It’s writing a bunch of commands in a text file that the Bash shell can run. This can be super helpful for automating repetitive tasks, whether you’re an admin or just a regular user. Plus, it’s a handy skill for hackers, too creating scripts to scan and explore compromised systems when you can’t install specific tools. After all, why not make your own scripts to get around security measures?

<Why>

If you can navigate a terminal and do some basic tasks, why not dive into Bash? Imagine you need to scan for hosts on a network but can’t install nmap or Python. Or maybe you need to check when the last backup was made and see which processes are hogging memory and CPU once a week. It can be frustrating to handle repetitive tasks and hit roadblocks because you don’t know Bash.

Learning Bash can seriously boost your skills as a hacker, admin, or Linux user in general. It helps you automate those annoying tasks, lets you run commands without extra software, and is handy for managing remote servers. Plus, once you get the hang of it, it’ll save you a ton of time and effort.

So why not?

<./script.sh>

Below me here is a script I’ve done to showcase how to retrieve CPU usage average load times, processes with high CPU usage, disk space, memory info, and test internet connectivity. A very basic yet simple script to exactly how bash works. If you are comfortable using the Linux CLI, then learning bash should be a breeze.

#!/bin/bash

# Creating our banner for each section of the system-check script
line=$'\n' # Start a new line
tab=$'\t'  # Start tab
sep="*****************************************${line}${tab}${tab} String\
 ${line}****************************************"

clear
#################################################

# Print CPU Usage Banner
section="${sep//String/CPU USAGE}"
echo "$section"

# Print the CPU average loads
cpu_usage="$(uptime | cut -d ' ' -f 12,13,14)"
echo "CPU average load times:${line}${cpu_usage}"

#################################################

# Print CPU Processes Banner
section="${sep//String/CPU PROCESSES}"
echo "$section"

# Print the processes that take the most % CPU
cpu_processes="$(top -b -n 1 | grep -v "%CPU" | awk '{print $12}' | head -n 15 | tail -n 9)"
echo "CPU-heavy processes:${line}${cpu_processes}"

#################################################

# Print the % of free disk space of the main drive on the Linux box
section="${sep//String/DISK USAGE}"
echo "$section"

# Prints the disk space available
free_disk_space="$(df | head -n 7 | awk 'NR==1 || NR==7')"
echo "Free disk space:${line}${free_disk_space}"

#################################################

# Print out available and used memory 
section="${sep//String/RAM USAGE}"
echo "$section"

# Prints the status of memory used and available
memory_available="$(cat /proc/meminfo | head -n 3)"
echo "Memory status:${line}${memory_available}"

#################################################

# Prints out Internet Check Banner
section="${sep//String/INTERNET CHECK}"
echo "$section"

# Grabs all IPv4 addresses on the machine
ipV4="$(ip a | grep "inet " | awk '{print $2}')"

# Uses conditional statement to check if we can ping Google; testing for internet connection.
if ping -c 4 8.8.8.8 > /dev/null 2>&1; 
then
    echo "IP address(es):${line}${ipV4}"
    echo "${line}Bottom Line... Internet Works"
else
    echo 'NO INTERNET'
fi

<Demonstration Video>

If you’re not sure exactly what each command does even with the comments above them, no worries I’ll go over it in more detail in the demonstration video below.