change to python

This commit is contained in:
Michael Grote 2023-07-21 18:12:10 +02:00
parent 41d82dc21d
commit f7f1d6e4d1
3 changed files with 71 additions and 63 deletions

62
app.sh
View File

@ -1,62 +0,0 @@
#!/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

69
app.spy Normal file
View File

@ -0,0 +1,69 @@
import os
import subprocess
from git import Repo
import paramiko
# Function to export RouterOS configuration
def export_routeros_config(device_config):
device = device_config["host"]
username = device_config["username"]
ssh_key_path = device_config["ssh_key_path"]
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(device, username=username, key_filename=ssh_key_path)
# Modify the export command based on the RouterOS command for configuration export
stdin, stdout, stderr = ssh.exec_command("/export")
with open(f"{device}_config_export.txt", "w") as f:
f.write(stdout.read().decode())
ssh.close()
# Function to check if the Git repository exists and pull or clone accordingly
def check_and_pull_git_repo():
git_repo_path = os.environ.get("GIT_REPO_PATH")
if os.path.exists(git_repo_path):
repo = Repo(git_repo_path)
if not repo.bare:
origin = repo.remote(name="origin")
origin.pull()
else:
print("Error: The directory exists but is not a Git repository.")
exit(1)
else:
git_repo_url = os.environ.get("GIT_REPO_URL")
Repo.clone_from(git_repo_url, git_repo_path)
# Function to commit and push the configuration to the Git repository
def commit_and_push_to_git():
git_repo_path = os.environ.get("GIT_REPO_PATH")
repo = Repo(git_repo_path)
index = repo.index
# Add the RouterOS config files to the index
config_files = [f"{device['host']}_config_export.txt" for device in DEVICE_CONFIGS]
index.add(config_files)
# Commit and push the changes
index.commit("Update configuration")
origin = repo.remote(name="origin")
origin.push()
if __name__ == "__main__":
# RouterOS devices SSH connection settings
DEVICE_CONFIGS = os.environ.get("DEVICE_CONFIGS").split()
DEVICE_CONFIGS = [
{"host": device.split(',')[0], "username": device.split(',')[1], "ssh_key_path": device.split(',')[2]}
for device in DEVICE_CONFIGS
]
# Export RouterOS configurations
for device_config in DEVICE_CONFIGS:
export_routeros_config(device_config)
# Check and pull the Git repository
check_and_pull_git_repo()
# Commit and push the configurations to the Git repository
commit_and_push_to_git()

View File

@ -5,6 +5,7 @@ services:
image: registry.mgrote.net/oxidized-selfmade:latest
environment:
- DEVICE_CONFIGS=rb5009.grote.lan,test,/keys/rb5009
- GIT_REPO_PATH=/repo
- GIT_REPO_URL=ssh://gitea@git.mgrote.net:2222/mg/network-configs.git
- GIT_USERNAME=gitea # nur für comitter name
- GIT_SSH_KEY=/keys/git
@ -13,7 +14,7 @@ services:
- ./keys/rb5009:/keys/rb5009:ro
# git
- ./keys/git:/keys/git:ro
-
-
# Add any other volumes or configurations needed