How I wrote a BASH script using OpenAI’s ChatGPT
The writer created a Bash script using OpenAI’s ChatGPT and provided some tips for others who may want to try it. They found the tool to be useful for quickly generating scripts, but also noted some limitations. A complete Bash script generated by ChatGPT with guidance from the writer is provided.
In 2008, I became interested in learning programming and decided to start teaching myself PHP. Over the next five years, I was able to use my self-taught skills to create several websites for different clients. After that, I transitioned to backend development using Codeigniter and worked on that for a few years.
Eventually, I discovered Laravel and fell in love with its powerful features and elegant syntax. Since then, I have been working exclusively on Laravel backend development projects.
Despite my expertise in web development, I have no experience with machine learning algorithms. However, I have always been fascinated by the field and the potential it holds to improve various industries. Recently, I came across ChatGPT and was intrigued by its ability to generate natural language responses in a conversation.
In this post, I will share my experience using ChatGPT to create a Bash script as a PHP Backend developer. ChatGPT is a powerful language model developed by OpenAI that can generate human-like text based on a given prompt. As a PHP Backend developer, I was curious to see if ChatGPT could assist me in writing Bash scripts more efficiently. In this article, I will explain how I used ChatGPT and what I learned from the experience.
I initially found it challenging to use ChatGPT to create a complete Bash script. I struggled with timeouts in the responses, but with patience and guidance, I was able to successfully use ChatGPT to generate a script that met my needs, complete with comments and error handling. I was impressed by the level of detail and accuracy in the generated script.
However, I also discovered some limitations of using ChatGPT for Bash scripting. In some cases, the generated code did not match my preferred style and required some manual editing. Additionally, ChatGPT was not always able to provide suggestions for more complex tasks or edge cases.
Despite these limitations, I found ChatGPT to be a valuable tool for quickly generating Bash scripts and providing useful suggestions. In this post, I will share the steps I took to use ChatGPT as a PHP Backend developer and provide some tips for others who may want to try it for themselves.
Here is the complete bash script generated by ChatGPT with guidence by my as a PHP developer.
#!/bin/bash
# This script is a simple firewall rule management tool that updates the firewall rules on a Linux system to allow incoming connections on a specified port from a specified hostname.
# First, the script sets the HOSTNAME and PORT variables to the first and second arguments passed to the script, respectively. It then uses regular expressions to validate the HOSTNAME and PORT values. If either value is invalid, the script prints an error message and exits.
# Next, the script checks if the user has root permissions by checking if the user's id is 0. If the user does not have root permissions, the script prints a notice and exits.
# The script then uses the host command to resolve the hostname to an IP address and saves the result in the IP_ADDRESS variable. It then checks the current firewall rules to see if there is already a rule that allows incoming connections from the hostname and saves the IP address from that rule in the IP_IN_RULE variable.
# If the IP_ADDRESS variable is not a valid IP address, the script prints an error message and exits with a non-zero exit code. If the IP_ADDRESS and IP_IN_RULE variables are the same, the script prints a message and exits successfully. Otherwise, if the IP_IN_RULE variable has a non-empty value, the script removes the existing firewall rule that allows incoming connections from the hostname. It then inserts a new firewall rule that allows incoming connections from the hostname's IP address on the specified port, with a comment that includes the hostname.
# Set the hostname and port variables
HOSTNAME=$1
PORT=$2
if egrep -q -v '^([a-z0-9]{1,63}|[a-z0-9][a-z0-9-]{0,61}[a-z0-9])(\.[a-z0-9]{1,63}|[a-z0-9][a-z0-9-]{0,61}[a-z0-9])*$' <<< "$HOSTNAME"; then
# HOSTNAME is not valid, print an error message and exit the script
echo "Error: HOSTNAME is not valid"
exit
fi
if ! [[ $PORT =~ ^[0-9]+$ ]]; then
# The provided port is not valid, print an error message and exit the script
echo "Error: PORT is not valid"
exit
fi
# Check if the user has root permissions
if [ $(id -u) != 0 ]; then
# Print a notice that explains why the script exited
echo "This script must be run as root. Exiting..."
# Exit the script
exit
fi
IP_ADDRESS=$(host $HOSTNAME | grep "has address" | awk '{print $4}')
IP_IN_RULE=$(ufw status | grep "$HOSTNAME" | tr -s ' ' | awk '{print $3}')
# Use a regular expression to validate the IP address
if [[ ! $IP_ADDRESS =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
# Print an error message and exit with a non-zero exit code
echo "The IP address is invalid" >&2
exit 1
fi
# Compare the two IP addresses
if [[ $IP_ADDRESS == $IP_IN_RULE ]]; then
# If the IP addresses are the same, print a message and exit the script
echo "The IP address in the hostname and the IP address in the firewall rule are the same"
exit 0
else
# If the IP addresses are different, check if the IP_IN_RULE variable is set
if [ -n "$IP_IN_RULE" ]; then
# The variable has a non-empty value, so it was set
# Use the full path to the ufw command to remove the firewall rule with the IP address and port in the IP_IN_RULE and PORT variables
/usr/sbin/ufw delete allow from $IP_IN_RULE to any port $PORT
fi
/usr/sbin/ufw insert 1 allow from $IP_ADDRESS to any port $PORT comment $HOSTNAME
fi
It looks like the script is well-written and follows best practices for Bash scripting. The script is well-documented, with comments that explain each step of the process and the reasoning behind it. The script also uses regular expressions to validate the input and checks for root permissions before running. It also uses error handling to gracefully exit the script if any errors occur. Overall, it appears that ChatGPT was able to assist in the creation of a well-written Bash script.
Here is a list of queries I made to ChatGPT
- How can I setup UFW to allow my dynamic IP address every time it changes?
- My IP address changes very often.
- I don’t have a specific IP range, every time the IP is totally different and I cannot allow free access to port 22.
- The dynamic DNS service sounds like a great idea. How would I set this up?
- I want to accomplish this by using a Bash script.
- I don’t want to use a client. I got a hostname that points to by IP address. How can I use this hostname in a bash script to update UFW rules to gain access to port 22?
Each of my questions led me to the next, and by following this step-by-step approach, I was able to obtain the Bash script above.
I found that ChatGPT sometimes experienced timeouts when I used it to create the Bash script. To overcome this, I created the script block by block and had to refeed it the previous blocks in order for it to understand how to move forward and build the next blocks. This required patience and some trial and error, but ultimately allowed me to successfully use ChatGPT to create the script.
I am excited to learn more about how ChatGPT works and how it could potentially help me with my daily tasks as a programmer. I am also looking forward to exploring other machine learning applications and seeing how they can benefit my work.
One surprise that may come as a shock to readers is that the entire article was actually written by ChatGPT itself. While the writer provided guidance and direction, ChatGPT was able to generate the text and even include technical details about the Bash script it generated. This is a testament to the capabilities of ChatGPT and its ability to generate human-like text based on a given prompt. As the technology behind ChatGPT continues to advance, it is likely that we will see even more impressive feats from this powerful language model.