Skip to content

Instantly share code, notes, and snippets.

@a-kbv
Last active August 20, 2023 12:27
Show Gist options
  • Save a-kbv/c5fc8e4403064f332861f90b7bde7d15 to your computer and use it in GitHub Desktop.
Save a-kbv/c5fc8e4403064f332861f90b7bde7d15 to your computer and use it in GitHub Desktop.
helper.sh
#!/bin/bash
VERSION="0.0.2"
# Function to countdown and clear the screen
countdown() {
for (( i=$1; i>=1; i-- )); do
printf "\r$2 %02d seconds.. press any key to cancel" $i
read -s -n 1 -t 1 key
if [[ $key ]]; then
echo ""
return 1
fi
done
echo ""
clear
exit
}
check_and_download_updates() {
echo "Checking for updates..."
# Check for curl command
if ! command -v curl &> /dev/null
then
echo "curl command could not be found."
echo "Please install curl to check for updates."
return
fi
# Try to fetch the latest script from the gist
if ! curl --silent --max-time 10 --output helper_latest.sh https://gist.githubusercontent.com/a-kbv/c5fc8e4403064f332861f90b7bde7d15/raw/helper.sh
then
echo "Network error while checking for updates."
return
fi
# Parse the version in the downloaded file
NEW_VERSION=$(awk -F\" '/^VERSION/{print $2}' helper_latest.sh)
# Check if updates are available
if [[ $NEW_VERSION != $VERSION ]]
then
echo "Update found. Downloading..."
mv helper_latest.sh helper.sh
chmod +x helper.sh
echo "Script updated."
echo "Restarting script with the latest version."
exec ./helper.sh
exit
else
echo "No updates found."
rm helper_latest.sh
fi
}
# Function to check disk partitions
check_disk_partitions() {
echo "Checking disk partitions..."
lsblk -o name,size
# Call countdown function
countdown 10 "Clearing the screen in"
}
# Function to download a website
download_website() {
echo "You opted to download a website."
read -e -p "Please enter the directory to download the website: " DIRECTORY
if [ ! -d "$DIRECTORY" ]; then
echo "Directory does not exist. Do you want to create it? [Y/n]: "
read RESPONSE
if [[ "$RESPONSE" =~ ^([yY][eE][sS]|[yY])$ ]]
then
mkdir -p "$DIRECTORY"
else
echo "Exiting, please try again with a valid directory."
return
fi
fi
echo "Please enter the URL of the website that you want to download: "
read URL
echo "---------------------------------------------------------"
echo "Starting website download..."
wget -P "$DIRECTORY" --mirror --convert-links --adjust-extension --page-requisites --no-parent $URL
echo "Website downloaded successfully to the path: $DIRECTORY"
# Call countdown function
countdown 10 "Clearing the screen in"
}
# Function to start a HTTP server
start_http_server() {
echo "You opted to start a HTTP server."
read -e -p "Please enter the directory where you want to start the server: " DIRECTORY
cd "$DIRECTORY" && python3 -m http.server 8888
# Call countdown function
countdown 10 "Clearing the screen in"
}
# Function to switch PHP version
switch_php_version() {
echo "You opted to switch PHP version."
# php versions array
declare -a phpVersioArr=("php8.0" "php8.1" "php8.2")
# get length of the array
arraylength=${#phpVersioArr[@]}
# taking current php version
php_version_command=`php -v`;
php_version_current=${php_version_command:3:4}
# trim the string
php_version_current=`echo $php_version_current | sed 's/ *$//g'`
echo "Select PHP Version from the list:"
echo "========================================="
# use for loop to read all values and indexes
for (( i=0; i<${arraylength}; i++ ));
do
echo "$i - ${phpVersioArr[$i]}"
done
echo "========================================="
# waiting for user to select correct version of php to switch to
pattern="^[0-9]{1}$"
selectedPHPVersion="";
while [[ (! $selectedPHPVersion =~ $pattern) || (! -v "phpVersioArr[selectedPHPVersion]") ]]
do
echo Please enter valid input
read selectedPHPVersion
done
new_php_version="${phpVersioArr[$selectedPHPVersion]:3:4}"
echo "Switching to PHP$new_php_version. . ."
sudo a2disconf "php${php_version_current}-fpm";
sudo a2enconf "${phpVersioArr[$selectedPHPVersion]}-fpm";
sudo systemctl restart apache2;
sudo update-alternatives --set php /usr/bin/"${phpVersioArr[$selectedPHPVersion]}";
sudo update-alternatives --set phar /usr/bin/phar"$new_php_version";
sudo update-alternatives --set phar.phar /usr/bin/phar.phar"$new_php_version";
sudo update-alternatives --set phpize /usr/bin/phpize"$new_php_version";
sudo update-alternatives --set php-config /usr/bin/php-config"$new_php_version";
echo ""
echo "---------------------------------------------------------"
echo "Switched to PHP$new_php_version successfully."
echo "---------------------------------------------------------"
echo ""
echo "Current PHP version:"
php -v
echo "---------------------------------------------------------"
echo "Done!"
echo "---------------------------------------------------------"
# Call countdown function
countdown 10 "Clearing the screen in"
}
# Function to clear Linux cache
clear_linux_cache() {
echo "Clearing the Linux cache..."
sudo free && sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
echo "Linux cache cleared successfully."
}
# Function for directory beautifier
directory_beautifier() {
echo "You opted for directory beautifier."
read -e -p "Please enter the directory: " DIRECTORY
if [ -d "$DIRECTORY" ]; then
echo "Beautifying directory: $DIRECTORY"
find "$DIRECTORY" -print | sed -e 's;[^/]*/;|-- ;g;s;-- |; |;g'
else
echo "Directory does not exist. Please try again with a valid directory."
return
fi
# Call countdown function
countdown 10 "Clearing the screen in"
}
retrieve_ip_address() {
echo "Retrieving IP addresses..."
# Retrieving public IP address
PUBLIC_IP=$(curl -fSs https://1.1.1.1/cdn-cgi/trace | awk -F= '/ip/ { print $2 }')
echo "PUBLIC: $PUBLIC_IP"
# Retrieving local IP address
LOCAL_IP=$(ifconfig | awk '/inet /{print $2;exit}')
echo "LOCAL: $LOCAL_IP"
# Call countdown function
countdown 10 "Clearing the screen in"
}
display_top_commands() {
echo ""
echo "Top 20 most frequently used commands:"
echo "-------------------------------------"
sort ~/.bash_history | uniq -c | sort -rn | head -n 20
echo "-------------------------------------"
echo ""
}
logout_current_user() {
if countdown 10 "Logging out the current user in"; then
pkill -KILL -u $USER
echo "Logged out successfully."
else
echo "Logout cancelled."
fi
}
# Function to manage Apache Virtual Hosts
manage_apache_vhosts() {
echo "---------------------------------------------------------"
echo "Apache Virtual Hosts Management:"
echo "---------------------------------------------------------"
echo "[1] List all virtual hosts."
echo "[2] Create a new virtual host."
echo "[3] Delete a virtual host."
echo "[4] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read VHOSTOPTION
echo "---------------------------------------------------------"
case $VHOSTOPTION in
1) list_vhosts ;;
2) create_vhost ;;
3) delete_vhost ;;
4) return ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
}
# Function to list Apache Virtual Hosts
list_vhosts() {
echo "Listing all virtual hosts:"
ls -1 /etc/apache2/sites-available
}
# Function to create a new Apache Virtual Host
create_vhost() {
echo "Create a new virtual host."
read -e -p "Please enter the directory: " DIRECTORY
if [ ! -d "$DIRECTORY" ]; then
echo "Directory does not exist. Please try again with a valid directory."
return
fi
read -e -p "Please enter the server name: " SERVERNAME
if [ -z "$SERVERNAME" ]; then
echo "Server name cannot be empty. Please try again with a valid server name."
return
fi
# Create virtual host file
sudo tee "/etc/apache2/sites-available/$SERVERNAME.conf" > /dev/null << EOT
<VirtualHost *:80>
DocumentRoot "$DIRECTORY"
ServerName $SERVERNAME
<Directory "$DIRECTORY">
Options -Indexes +FollowSymLinks +MultiViews
Require all granted
AllowOverride All
</Directory>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "$DIRECTORY"
ServerName $SERVERNAME
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<Directory "$DIRECTORY">
Options -Indexes +FollowSymLinks +MultiViews
Require all granted
AllowOverride All
</Directory>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
EOT
# Create symlink in sites-enabled
sudo ln -s "/etc/apache2/sites-available/$SERVERNAME.conf" "/etc/apache2/sites-enabled/$SERVERNAME.conf"
# Restart Apache
sudo systemctl restart apache2
echo "Virtual host created successfully."
}
# Function to delete an Apache Virtual Host
delete_vhost() {
echo "Delete a virtual host."
list_vhosts
read -e -p "Please enter the server name to delete: " SERVERNAME
if [ -z "$SERVERNAME" ]; then
echo "Server name cannot be empty. Please try again with a valid server name."
return
fi
# Remove virtual host file
sudo rm "/etc/apache2/sites-available/$SERVERNAME.conf"
sudo rm "/etc/apache2/sites-enabled/$SERVERNAME.conf"
# Restart Apache
sudo systemctl restart apache2
echo "Virtual host deleted successfully."
}
# Function to search for files recursively
search_files() {
echo "You opted to search for files recursively."
# Prompt the user to enter the directory to search in
read -e -p "Please enter the directory to search in: " DIRECTORY
# Prompt the user to enter the expression to search for
read -e -p "Please enter the expression to search for: " EXPRESSION
# Use the find command to search for files recursively
echo "Searching for files with expression '$EXPRESSION' in directory '$DIRECTORY'..."
find "$DIRECTORY" -type f -iname "*$EXPRESSION*" -print
# Call countdown function
countdown 10 "Clearing the screen in"
}
get_weather(){
curl -s "wttr.in/"
}
check_ip_or_domain() {
echo "You opted to check IP address or domain."
read -e -p "Please enter the IP address or domain: " IP_OR_DOMAIN
# Ping the IP address or domain
echo "Pinging $IP_OR_DOMAIN ..."
ping -c 5 $IP_OR_DOMAIN
# Get the public IP address of the domain
echo "Retrieving public IP address of $IP_OR_DOMAIN ..."
PUBLIC_IP=$(dig +short $IP_OR_DOMAIN)
echo "Public IP address: $PUBLIC_IP"
# Check for open ports
echo "Checking for open ports on $IP_OR_DOMAIN ..."
nmap -p 80,443,3306,5432 $IP_OR_DOMAIN
# Get the location of the IP address
echo "Getting location of $IP_OR_DOMAIN ..."
LOCATION=$(curl -s ipinfo.io/$PUBLIC_IP)
echo "Location: $LOCATION"
# Zone Transfer
echo "Checking for zone transfer on $IP_OR_DOMAIN ..."
dig axfr @nameserver $IP_OR_DOMAIN
# Replace nameserver with the actual authoritative nameserver for the domain
# SPF Record
echo "Verifying SPF record for $IP_OR_DOMAIN ..."
dig +short TXT $IP_OR_DOMAIN | grep -i "v=spf1"
# Modify the SPF record check as per your specific SPF configuration
# MX Record
echo "Checking MX record for $IP_OR_DOMAIN ..."
dig +short MX $IP_OR_DOMAIN
# TXT Records
echo "Checking TXT records for $IP_OR_DOMAIN ..."
dig +short TXT $IP_OR_DOMAIN
# Call countdown function
countdown 10 "Clearing the screen in"
}
# Function to start Wireguard
start_wireguard() {
echo "Starting Wireguard..."
sudo wg-quick up wg0
echo "Wireguard started successfully."
}
# Function to stop Wireguard
stop_wireguard() {
echo "Stopping Wireguard..."
sudo wg-quick down wg0
echo "Wireguard stopped successfully."
}
# Function to install Wireguard
install_wireguard() {
echo "Installing Wireguard..."
sudo apt-get update && sudo apt-get upgrade
sudo apt install wireguard resolvconf wireguard-tools
echo "Wireguard installed successfully."
}
# Function to check the configuration of Wireguard
config_wireguard() {
echo "Configuring Wireguard..."
sudo nano /etc/wireguard/wg0.conf
echo "Wireguard configured successfully."
}
# Function to install and start openssh-server
start_ssh_server() {
echo "Installing and starting openssh-server..."
sudo apt install openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh
sudo ufw allow ssh
echo "SSH server installed and started."
}
# Function to stop and remove openssh-server
stop_ssh_server() {
echo "Stopping and removing openssh-server..."
sudo systemctl stop ssh
sudo systemctl disable ssh
sudo apt-get remove openssh-server
sudo ufw delete allow ssh
echo "SSH server stopped and removed."
}
# Function to manage SSH server
manage_ssh_server() {
echo "You have these options:"
echo "1 - Start SSH server"
echo "2 - Stop SSH server"
echo "3 - Restart SSH server"
echo "4 - SSH server status"
echo "5 - Go back"
read -p "Enter your choice: " choice
case $choice in
1) sudo systemctl start ssh ;;
2) sudo systemctl stop ssh ;;
3) sudo systemctl restart ssh ;;
4) sudo systemctl status ssh ;;
5) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
}
# SSH submenu
submenu_ssh_tools() {
while true; do
echo "SSH Tools:"
echo "---------------------------------------------------------"
echo "[1] Install and enable SSH server."
echo "[2] Stop and remove SSH server."
echo "[3] Manage SSH server."
echo "[4] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read TOOLS_OPTION
case $TOOLS_OPTION in
1) start_ssh_server ;;
2) stop_ssh_server ;;
3) manage_ssh_server ;;
4) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
done
}
# Function to manage Wireguard submenu
submenu_wireguard_tools() {
while true; do
echo "Wireguard Tools:"
echo "---------------------------------------------------------"
echo "[1] Start Wireguard."
echo "[2] Stop Wireguard."
echo "[3] Install Wireguard."
echo "[4] Wireguard configuration."
echo "[5] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read TOOLS_OPTION
echo "---------------------------------------------------------"
case $TOOLS_OPTION in
1) start_wireguard ;;
2) stop_wireguard ;;
3) install_wireguard ;;
4) config_wireguard ;;
5) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
done
}
submenu_linux_tools() {
while true; do
echo "Linux Tools:"
echo "---------------------------------------------------------"
echo "[1] Clear Linux cache."
echo "[2] Check disk partitions."
echo "[3] Retrieve IP addresses."
echo "[4] Display Top 20 commands"
echo "[5] Search for a specific file in directory."
echo "[6] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read TOOLS_OPTION
echo "---------------------------------------------------------"
case $TOOLS_OPTION in
1) clear_linux_cache ;;
2) check_disk_partitions ;;
3) retrieve_ip_address ;;
4) display_top_commands ;;
5) search_files ;;
6) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
done
}
submenu_general_tools() {
while true; do
echo "General Tools:"
echo "---------------------------------------------------------"
echo "[1] Weather."
echo "[2] Check IP address or domain."
echo "[3] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read TOOLS_OPTION
echo "---------------------------------------------------------"
case $TOOLS_OPTION in
1) get_weather ;;
2) check_ip_or_domain ;;
6) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
done
}
submenu_website_tools() {
while true; do
echo "Website Tools:"
echo "---------------------------------------------------------"
echo "[1] Download website."
echo "[2] Start a HTTP server on a specific directory."
echo "[3] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read TOOLS_OPTION
echo "---------------------------------------------------------"
case $TOOLS_OPTION in
1) download_website ;;
2) start_http_server ;;
3) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
done
}
submenu_php_tools() {
while true; do
echo "PHP Tools:"
echo "---------------------------------------------------------"
echo "[1] Switch PHP version."
echo "[2] Directory beautifier."
echo "[3] Manage Apache Virtual Hosts."
echo "[4] Go back."
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read TOOLS_OPTION
echo "---------------------------------------------------------"
case $TOOLS_OPTION in
1) switch_php_version ;;
2) directory_beautifier ;;
3) manage_apache_vhosts ;;
4) clear; break ;;
*) echo "Invalid option. Please try again with a valid choice." ;;
esac
# Call countdown function
countdown 10 "Clearing the screen in"
done
}
#Clear the screen
clear
check_and_download_updates
while true; do
echo "---------------------------------------------------------"
echo "Welcome! What would you like to do today?"
echo "---------------------------------------------------------"
echo "[1] Linux Tools"
echo "[2] Wireguard Tools"
echo "[3] Website Tools"
echo "[4] PHP Tools"
echo "[5] SSH Tools"
echo "[6] General Tools"
echo "[7] Logout current user."
echo "[8] Exit"
echo "---------------------------------------------------------"
echo -n "Enter your choice and press Enter: "
read OPTION
echo "---------------------------------------------------------"
case $OPTION in
1) submenu_linux_tools ;;
2) submenu_wireguard_tools;;
3) submenu_website_tools ;;
4) submenu_php_tools ;;
5) submenu_ssh_tools;;
6) submenu_general_tools ;;
7) logout_current_user ;;
8) clear; break ;;
*) echo "Invalid option. Please try again with valid choice." ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment