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 process of applying a JSON configuration file to a FreeRADIUS server. The script performs the following tasks:
Checks if the script is being run with root privileges.
Checks if the jq command is installed and installs it if not.
Locates the FreeRADIUS configuration files.
Prompts the user for the path to the JSON configuration file or uses a default one if not provided.
Reads the JSON configuration file using the jq command.
Applies the client configurations to the 'clients.conf' file.
Restarts the FreeRADIUS service.
Tests the FreeRADIUS configuration for success.
If the test is successful, it outputs the changes made in the configuration files, backs up the original configuration files, and exits.
If the test fails, it restores the original configuration files, outputs an error message, and exits with a non-zero status.
The script uses jq to read the JSON configuration file and apply the changes to the FreeRADIUS configuration files. It also uses 'diff' to compare the original and modified configuration files, highlighting the changes made.