routeros-config-export/app.sh
2023-07-19 20:46:20 +02:00

52 lines
1.6 KiB
Bash

#!/bin/bash
# RouterOS devices SSH connection settings
DEVICE_CONFIGS="$DEVICE_CONFIGS" # Comma-separated list of device configurations in the format: "device,username,/path/to/ssh/key"
# Git repository settings
GIT_REPO_URL="$GIT_REPO_URL" # Git repository SSH URL
GIT_USERNAME="$GIT_USERNAME" # Git username for SSH authentication
GIT_SSH_KEY="$GIT_SSH_KEY" # Git SSH private key path (mounted in Docker container)
GIT_REPO_PATH="/app/config_repo" # Directory where the Git repository is cloned in the Docker container
# Function to export RouterOS configuration
export_routeros_config() {
local device="$1"
local username="$2"
local ssh_key="$3"
ssh -i "$ssh_key" "$username"@"$device" /export > "/app/${device}_config_export.txt"
}
# Function to commit and push the configuration to the Git repository
commit_and_push_to_git() {
cd "$GIT_REPO_PATH"
git config --global user.email "$GIT_USERNAME"
git config --global user.name "$GIT_USERNAME"
git add .
git commit -m "Update configuration $(date +%Y-%m-%d)"
git push origin master
}
# Main script
IFS=',' read -r -a devices <<< "$DEVICE_CONFIGS"
for device_config in "${devices[@]}"; do
IFS=' ' read -r -a config <<< "$device_config"
device="${config[0]}"
username="${config[1]}"
ssh_key="${config[2]}"
export_routeros_config "$device" "$username" "$ssh_key"
done
cd "$GIT_REPO_PATH"
if [ -d "$GIT_REPO_PATH" ]; then
git pull origin master
else
git clone "$GIT_REPO_URL" "$GIT_REPO_PATH"
fi
commit_and_push_to_git