routeros-config-export/app.sh
2023-07-19 21:41:48 +02:00

63 lines
1.9 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
mkdir -p /app
# 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 > "/${GIT_REPO_PATH}/${device}.txt"
}
# Function to check if the Git repository exists and pull or clone accordingly
check_and_pull_git_repo() {
if [ -d "$GIT_REPO_PATH" ]; then
cd "$GIT_REPO_PATH"
if [ -d ".git" ]; then
git pull origin master
else
echo "Error: The directory exists but is not a Git repository."
exit 1
fi
else
git clone "$GIT_REPO_URL" "$GIT_REPO_PATH"
fi
}
# 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
check_and_pull_git_repo
commit_and_push_to_git