Hey there! You ever think about how amazing it would be to just automate your life a lot more every time by setting up systems, and all this stuff? Be it file management, task scheduling, or dealing with intricate processes — automation is the answer. For automation, use Python and Bash.

This article covers just how much Python and Bash can do to automate anything. But not the theory — we are going to be doing examples. And, you will get the knowledge of how to write scripts that can do boring tasks for you while saving yourself some time, but also automate all the repetitive things or system operations.

We’ll start with the basics: setting up your environment and writing simple scripts in Python and Bash. Next we will cover some more advanced using these two combinators so we can push our automation further. At the same time you will get a sneak peak to some of real world applications and how learning these skills can be a game-changer for you.

Oh, and one you are hungry for more — well, don’t forget our ebook The Art of Offensive Scripting. We need some real-life examples and in-depth tricks to go to the next level of automation from here onwards.

So, roll up your sleeves, and let’s get started on automating everything with Python and Bash!


Getting Started with Automation

Well then, let us delve into a bit of realistic automation. We will go back from the beginning (always good for understanding) and increase the difficulty each time. They will be using Linux and Gedit as their main tools, so enough words let’s get to it!

What is Automation?

Automation is the practice of writing scripts or using tools to take over redundant tasks for you. Sounds nice, imagine a virtual assistant doing the boring work for you.

Why Automate with Python and Bash?

Python or Bash are the two most powerful tools for automating your work. Python I s great for more complex tasks and data manipulation, yet Bash is amazing when you want a few lines to an operation on the system. The solution is powerful in combination: it can meet a spectrum of automation requirements.

Setting Up Your Environment

First things first, let’s set up your environment:

  1. Install Python: Most Linux distributions come with Python pre-installed. If you need to install or update it, use the command sudo apt install python3.
  2. Set Up Bash: Bash is included with most Linux systems, so you’re all set!
  3. Use Gedit: Gedit is a simple text editor perfect for writing scripts. Open it by typing gedit in your terminal.

Writing Your First Python Script

Let’s start with something simple—a “Hello, World!” script in Python. Open Gedit and create a file named hello_world.py:

print("Hello, World!")

Save the file and run it from your terminal using:

python3 hello_world.py

You should see the message “Hello, World!” printed to your terminal. Congratulations, you’ve just written and run your first Python script!

Writing Your First Bash Script

Now, let’s create a basic Bash script. Open Gedit and create a file named hello_world.sh:

#!/bin/bash

echo "Hello, World!"

Save the file, make it executable with chmod +x hello_world.sh, and run it with:

./hello_world.sh

You should see “Hello, World!” printed to your terminal. Awesome job!

Automating Tasks with Python

Now that you’ve got the basics down, let’s move on to more practical automation. Here’s a script that renames files in a directory. Open Gedit and create a file named rename_files.py:

import os

# Path to the directory containing files
path = '/path/to/your/directory'

# Loop through all files in the directory
for filename in os.listdir(path):
    new_name = 'prefix_' + filename
    os.rename(os.path.join(path, filename), os.path.join(path, new_name))

print("Files renamed successfully!")

Replace '/path/to/your/directory' with the actual path to your folder. Save the file and run it from your terminal using:

python3 rename_files.py

Your files should now have a new prefix.

Automating Tasks with Bash

Let’s automate cleaning up old log files with Bash. Create a file named cleanup_logs.sh in Gedit:

#!/bin/bash

# Path to the directory containing logs
LOG_DIR="/path/to/your/logs"

# Find and delete log files older than 7 days
find $LOG_DIR -type f -name "*.log" -mtime +7 -exec rm {} \;

echo "Old log files cleaned up!"

Replace "/path/to/your/logs" with your log directory path. Save the file, make it executable with chmod +x cleanup_logs.sh, and run it with:

./cleanup_logs.sh

Your old log files will be cleaned up!

Running Scripts Automatically

To automate running your scripts:

  • For Bash Scripts: Use cron jobs. Open your crontab crontab -e and add a line like 0 2 * * * /path/to/cleanup_logs.sh to run your script daily at 2 AM.
  • For Python Scripts: You can use cron jobs similarly or set up schedules as needed.

And there you have it—getting started with automation using Python and Bash on Linux with Gedit. From basic “Hello, World!” scripts to practical file handling and system cleanup, you’re now equipped to automate a variety of tasks. For more basics to advanced techniques, don’t forget to check out The Art of Offensive Scripting. We have mentioned step by step on the ebook so don’t miss it ..


Automating Tasks with Python

Alright, let’s dive into automating tasks with Python! We’re not just talking theory here—we’ll get right into some hands-on examples to make things crystal clear. And if you’re hungry for more, our book The Art of Offensive Scripting goes into even greater depth.

Python is super versatile for automation. Whether it’s handling files, processing data, or managing system operations, Python has you covered. Let’s start with some practical examples.

Example 1: Renaming Files

Imagine you have a bunch of files that need renaming—say, you want to add a prefix to each file. Here’s how you can automate that:

  1. Create Your Script: Open Gedit and create a file named rename_files.py:
   import os

   # Path to the directory containing files
   path = '/path/to/your/directory'

   # Loop through all files in the directory
   for filename in os.listdir(path):
       new_name = 'prefix_' + filename
       os.rename(os.path.join(path, filename), os.path.join(path, new_name))

   print("Files renamed successfully!")
  1. Customize and Run: Replace '/path/to/your/directory' with the path to your actual directory. Save the file and run it with:
   python3 rename_files.py

Your files should now have a prefix!

Example 2: Monitoring Disk Usage

Let’s say you want to keep an eye on disk usage and get an alert if it’s getting too high. Here’s a script for that:

  1. Create Your Script: Open Gedit and create a file named disk_usage.py:
   import shutil

   # Set your threshold (e.g., 80% usage)
   threshold = 80

   # Get disk usage
   total, used, free = shutil.disk_usage("/")

   # Calculate usage percentage
   usage_percentage = (used / total) * 100

   # Check if usage exceeds the threshold
   if usage_percentage > threshold:
       print(f"Warning! Disk usage is at {usage_percentage:.2f}%")
   else:
       print(f"Disk usage is under control at {usage_percentage:.2f}%")
  1. Run Your Script: Save the file and run it with:
   python3 disk_usage.py

You’ll get a warning if disk usage is too high!

Example 3: Automating Email Notifications

Here’s a script to send automated email notifications. Make sure you have the smtplib and email libraries:

  1. Create Your Script: Open Gedit and create a file named send_email.py:
   import smtplib
   from email.mime.text import MIMEText
   from email.mime.multipart import MIMEMultipart

   # Email settings
   sender_email = 'your_email@example.com'
   receiver_email = 'receiver@example.com'
   password = 'your_password'
   subject = 'Automated Email Notification'
   body = 'This is an automated email sent from Python!'

   # Create the email
   msg = MIMEMultipart()
   msg['From'] = sender_email
   msg['To'] = receiver_email
   msg['Subject'] = subject
   msg.attach(MIMEText(body, 'plain'))

   # Send the email
   with smtplib.SMTP('smtp.example.com', 587) as server:
       server.starttls()
       server.login(sender_email, password)
       server.sendmail(sender_email, receiver_email, msg.as_string())

   print("Email sent successfully!")
  1. Customize and Run: Replace placeholders with your email details. Save the file and run it with:
   python3 send_email.py

Your email will be sent automatically!

Note: Make sure that you provide the permission before running the scripts. You can set permission simply using this command: chmod +x filename.py

And there you have it—some cool ways to automate tasks with Python. We’ve just scratched the surface here, and if you’re excited to learn more, check out our book The Art of Offensive Scripting. It’s packed with advanced examples and in-depth techniques to take your scripting skills to the next level.

Discover: Python Mastery: The Ultimate Comprehensive Cheat Sheet for Beginners and Experts Alike


Automating Tasks with Bash

First, we will cover how to carry out automation of tasks using Bash. For our case, Bash is really handy when you need to process system operations and even execute scripts right from your command line. In the next few sections, we need to walk through a couple of practical examples so you can get an idea of how much automation you can achieve with Bash. If you are hungry for more, our book The Art of Offensive Scripting gives a deep dive into these sub-products.

Getting Started

Bash scripts are perfect for tasks like file management, system monitoring, and batch processing. Let’s start with a few hands-on examples to get you comfortable.

Example 1: Creating a Backup Script

Imagine you need to back up files from one directory to another. Here’s how you can automate that with Bash:

  1. Create Your Script: Open Gedit and create a file named backup_files.sh:
   #!/bin/bash

   # Source and destination directories
   SOURCE_DIR="/path/to/source"
   BACKUP_DIR="/path/to/backup"

   # Create backup directory if it doesn't exist
   mkdir -p $BACKUP_DIR

   # Copy files to the backup directory
   cp -r $SOURCE_DIR/* $BACKUP_DIR/

   echo "Backup completed successfully!"
  1. Customize and Run: Replace /path/to/source and /path/to/backup with your actual paths. Save the file, make it executable with chmod +x backup_files.sh, and run it with:
   ./backup_files.sh

Your files will be backed up!

Example 2: Cleaning Up Old Files

Here’s a script to delete old files from a directory. This is useful for cleaning up temporary files:

  1. Create Your Script: Open Gedit and create a file named cleanup_old_files.sh:
   #!/bin/bash

   # Directory to clean up
   TARGET_DIR="/path/to/your/directory"

   # Find and delete files older than 30 days
   find $TARGET_DIR -type f -mtime +30 -exec rm {} \;

   echo "Old files cleaned up!"
  1. Customize and Run: Replace /path/to/your/directory with your directory path. Save the file, make it executable with chmod +x cleanup_old_files.sh, and run it with:
   ./cleanup_old_files.sh

Old files will be removed!

Example 3: Monitoring System Resources

Let’s create a script to check your system’s memory usage and alert you if it’s getting too high:

  1. Create Your Script: Open Gedit and create a file named monitor_memory.sh:
   #!/bin/bash

   # Set your memory usage threshold (e.g., 80%)
   THRESHOLD=80

   # Get memory usage percentage
   MEMORY_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}')

   # Check if memory usage exceeds the threshold
   if (( $(echo "$MEMORY_USAGE > $THRESHOLD" | bc -l) )); then
       echo "Warning! Memory usage is at ${MEMORY_USAGE}%"
   else
       echo "Memory usage is under control at ${MEMORY_USAGE}%"
   fi
  1. Run Your Script: Save the file, make it executable with chmod +x monitor_memory.sh, and run it with:
   ./monitor_memory.sh

You’ll get an alert if memory usage is too high!

Running Scripts Automatically

To automate running your Bash scripts:

  • Use Cron Jobs: Open your crontab with crontab -e and add a line like 0 2 * * * /path/to/backup_files.sh to run your script daily at 2 AM.

That’s it for automating tasks with Bash! From backing up files to cleaning up old data and monitoring system resources, Bash scripts can handle a lot of your routine tasks. For even more advanced scripting techniques and examples, don’t forget to check out our book The Art of Offensive Scripting. It’s loaded with in-depth content to take your Bash scripting to the next level.


Integrating Python and Bash

Ok, so here’s how you can further super power your automation by mixing Python with Bash. Combine them both and these individual scripts turn into a powerhouse that will speed up your workflow. This post cover some use cases through practical examples to demonstrate how they can work together. And for a deep dive take a look at our book: The Art of Offensive Scripting

Python is superb for processing data, branching logic, and everything else that is not related to quick system tasks or file management unique to Bash. You can use them together to get the best of both in one workflow.

Example 1: Running a Python Script from Bash

Let’s say you have a Python script that processes data, and you want to run it from a Bash script. Here’s how you can do it:

  1. Create Your Python Script: Open Gedit and create a file named process_data.py:
   # process_data.py
   import sys

   def main(input_file):
       with open(input_file, 'r') as file:
           data = file.read()
           print(f"Data from {input_file}:")
           print(data)

   if __name__ == "__main__":
       if len(sys.argv) != 2:
           print("Usage: python process_data.py <file>")
       else:
           main(sys.argv[1])
  1. Create Your Bash Script: Open Gedit and create a file named run_process.sh:
   #!/bin/bash

   # Path to your input file
   INPUT_FILE="/path/to/your/file.txt"

   # Run the Python script
   python3 process_data.py $INPUT_FILE
  1. Run Your Scripts: Save both files, make the Bash script executable with chmod +x run_process.sh, and run it with:
   ./run_process.sh

This will execute the Python script from within the Bash script.

Example 2: Using Bash to Prepare Data for Python

Suppose you need to prepare a file before processing it with a Python script. Here’s how you can do it:

  1. Create Your Bash Script: Open Gedit and create a file named prepare_data.sh:
   #!/bin/bash

   # Create a sample file
   echo "Sample data for processing" > sample_data.txt

   # Run the Python script
   python3 process_data.py sample_data.txt
  1. Run Your Bash Script: Make it executable with chmod +x prepare_data.sh, and then run:
   ./prepare_data.sh

This will create a file and then pass it to the Python script for processing.

Example 3: Using Python to Generate a Bash Command

Sometimes, you might want to use Python to generate a Bash command dynamically. Here’s an example:

  1. Create Your Python Script: Open Gedit and create a file named generate_command.py:
   # generate_command.py
   import sys

   def generate_command(file_path):
       command = f"cat {file_path} | grep 'example'"
       print(command)

   if __name__ == "__main__":
       if len(sys.argv) != 2:
           print("Usage: python generate_command.py <file>")
       else:
           generate_command(sys.argv[1])
  1. Create Your Bash Script: Open Gedit and create a file named run_generated_command.sh:
   #!/bin/bash

   # Generate the command using Python
   COMMAND=$(python3 generate_command.py /path/to/your/file.txt)

   # Run the generated command
   eval $COMMAND
  1. Run Your Scripts: Save both files, make the Bash script executable with chmod +x run_generated_command.sh, and run:
   ./run_generated_command.sh

This will generate and execute a Bash command based on the Python script’s output.

Here’s a practical and casual conclusion for the article:


Conclusion

These are just the basics for getting new scripts working when calling Python from bash and vice versa, but this introduction should at least get you started in uncharted territory! We discussed everything from basic “Hello, World!” variations to more complex patterns like filters and conversational assistance. scripts to even complex file management and system monitoring. You have also learned how Python and Bash can together improve your workflow to be productive across a wider range of tasks.

If you’re eager to dive deeper and explore advanced techniques, don’t forget to check out our book The Art of Offensive Scripting. It’s packed with detailed examples and in-depth content to help you master automation and scripting.

Thanks for following along! Happy automating, and see you next time!


Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *