Tuesday, July 4, 2023

Retrieving System Information with a Shell Script - OS, php,MySQL, Apache

 This shell script will allows you to retrieve important system information such as the operating system version, PHP version, installed PHP extensions, MySQL/MariaDB version, and Apache version. The script is designed to be compatible with CentOS, RHEL, and Ubuntu distributions of all versions.

 

 
#!/bin/bash

# Function to get OS version for CentOS/RHEL
get_centos_os_version() {
  os_version=$(cat /etc/redhat-release)
}

# Function to get OS version for Ubuntu
get_ubuntu_os_version() {
  os_version=$(lsb_release -ds)
}

# Function to get PHP version
get_php_version() {
  php_version=$(php -v | awk '/^PHP/ {print $2}')
}

# Function to get installed PHP extensions
get_php_extensions() {
  php_extensions=$(php -m)
}

# Function to get MySQL/MariaDB version
get_mysql_version() {
  mysql_version=$(mysql -V | awk '{print $5}')
}

# Function to get Apache version for CentOS/RHEL
get_centos_apache_version() {
  apache_version=$(httpd -v | awk '/^Server version/ {print $3}')
}

# Function to get Apache version for Ubuntu
get_ubuntu_apache_version() {
  apache_version=$(apache2 -v | awk '/^Server version/ {print $3}')
}

# Detect the OS type and call appropriate functions
if [ -f "/etc/redhat-release" ]; then
  get_centos_os_version
  get_php_version
  get_php_extensions
  get_mysql_version
  get_centos_apache_version
elif [ -f "/etc/lsb-release" ]; then
  get_ubuntu_os_version
  get_php_version
  get_php_extensions
  get_mysql_version
  get_ubuntu_apache_version
else
  echo "Unsupported OS. Exiting..."
  exit 1
fi

# Print the gathered information
echo "OS Version: $os_version"
echo "PHP Version: $php_version"
echo "Installed PHP Extensions: $php_extensions"
echo "MySQL/MariaDB Version: $mysql_version"
echo "Apache Version: $apache_version"

No comments:

Post a Comment