if [ -z "$clients_conf" ] || [ -z "$eap_conf" ]; then
echo "Unable to locate FreeRADIUS configuration files."
exit 1
fi
}
Function to prompt for JSON file location
prompt_for_json_file() {
read -p "Enter the path to your configuration JSON file or press Enter to use the default (./config.json): " json_file
json_file="${json_file:-./config.json}"
code
if [ ! -f "$json_file" ]; then
echo "Configuration file not found at $json_file."
exit 1
fi
}
Read JSON using jq
read_json() {
jq -r "1""json_file"
}
Function to apply client configurations
apply_client_configurations() {
total_clients=(readjson′.clitotal_clients"; i++ ))
do
name=i].name")
ip=i].ip")
sharedKey=i].sharedKey")
test_freeradius_config() {
systemctl restart freeradius.service
if systemctl is-active --quiet freeradius.service; then
echo "FreeRADIUS configuration test passed."
else
echo "FreeRADIUS configuration test failed."
return 1
fi
}
Function to compare configuration files with their backups
output_config_changes() {
echo "Outputting changes made in the configuration files:"
code
# Paths to configuration files and their backups
local eap_config="/etc/freeradius/3.0/mods-available/eap"
local clients_config="/etc/freeradius/3.0/clients.conf"
local eap_config_backup="${eap_config}.backup"
local clients_config_backup="${clients_config}.backup"
# Check and output changes for EAP configuration
if [ -f "$eap_config_backup" ]; then
echo "Changes in EAP configuration:"
diff "$eap_config_backup" "$eap_config"
else
echo "No backup found for EAP configuration."
fi
# Check and output changes for clients configuration
if [ -f "$clients_config_backup" ]; then
echo "Changes in Clients configuration:"
diff "$clients_config_backup" "$clients_config"
else
echo "No backup found for Clients configuration."
fi
if ! test_freeradius_config; then
echo "Restoring backup due to failed configuration."
cp "clientsconf.backup""clients_conf"
cp "eap_conf"
exit 1
else
echo "FreeRADIUS configuration updated successfully."
output_config_changes
fi
Assistant
This is a Bash script that automates the configuration of FreeRADIUS using a JSON file. Here's how the script works:
The check_root function checks if the script is running with root privileges. If not, it displays an error message and exits.
The check_jq_installed function checks if the jq command-line tool is installed. If not, it installs it using the apt-get command.
The search_config_files function searches for the FreeRADIUS configuration files (clients.conf and eap) in the /etc/freeradius directory. If any of the files are not found, it displays an error message and exits.
The prompt_for_json_file function prompts the user to enter the path to the configuration JSON file. If no path is provided, it uses the default path ./config.json. If the file is not found, it displays an error message and exits.
The read_json function uses the jq command to read values from the JSON file. It takes an argument that specifies the jq query to execute.
The apply_client_configurations function reads the client configurations from the JSON file and adds them to the clients.conf file.
The test_freeradius_config function restarts the FreeRADIUS service and checks if it is active. If the service is active, it displays a success message. Otherwise, it displays an error message and returns a non-zero exit code.
The output_config_changes function compares the modified configuration files with their backups and outputs the differences using the diff command.
The main script execution calls the functions in the following order:
check_root
check_jq_installed
search_config_files
prompt_for_json_file
apply_client_configurations
test_freeradius_config
output_config_changes
If the FreeRADIUS configuration test fails, it restores the backups of the configuration files. Otherwise, it displays a success message and outputs the changes made to the configuration files.
Note: This script assumes that the FreeRADIUS service is managed by systemctl and that the configuration files are located in the specified paths.