routeros-config-export/x

87 lines
2.1 KiB
Text
Raw Normal View History

#!/bin/bash
# setze Variable
2023-07-21 18:57:50 +02:00
# Format <FQDN>,<SSH_KEY_PATH to ssh-private-key(passwordless)>
2023-07-21 18:58:43 +02:00
DEVICES="rb5009.grote.lan,/home/mg/oxidized-selfmade/neu
hex.grote.lan,/ssh/keys/hex"
2023-07-21 18:56:29 +02:00
GIT_REPO_PATH=/home/mg/oxidized-selfmade # wo soll das Repo angelegt werden
GIT_REPO_BRANCH=master
2023-07-21 18:56:29 +02:00
GIT_USERNAME=oxidized-selfmade
GIT_USER_MAIL=michael.grote@posteo.de
2023-07-21 18:56:29 +02:00
GIT_REPO_REMOTE_NAME=origin
2023-07-21 19:02:05 +02:00
# Checks
2023-07-21 19:05:23 +02:00
if [ -z "$GIT_REPO_PATH" ] ; then
2023-07-21 19:02:05 +02:00
echo "GIT_REPO_PATH is not set."
exit 3
fi
2023-07-21 19:05:23 +02:00
if [ -z "$GIT_REPO_BRANCH" ] ; then
2023-07-21 19:02:05 +02:00
echo "GIT_REPO_BRANCH is not set."
exit 3
fi
2023-07-21 19:05:23 +02:00
if [ -z "$GIT_USERNAME" ] ; then
2023-07-21 19:02:05 +02:00
echo "GIT_USERNAME is not set."
exit 3
fi
2023-07-21 19:05:23 +02:00
if [ -z "$GIT_USER_MAIL" ] ; then
2023-07-21 19:02:05 +02:00
echo "GIT_USER_MAIL is not set."
exit 3
fi
2023-07-21 19:05:23 +02:00
if [ -z "$GIT_REPO_REMOTE_NAME" ] ; then
2023-07-21 19:02:05 +02:00
echo "GIT_REPO_REMOTE_NAME is not set."
exit 3
fi
2023-07-21 19:05:23 +02:00
if [ -z "$DEVICES" ] ; then
2023-07-21 19:02:05 +02:00
echo "DEVICES is not set."
exit 3
fi
2023-07-21 18:56:29 +02:00
git config --global user.email "$GIT_USER_MAIL"
git config --global user.name "$GIT_USERNAME"
2023-07-21 19:04:24 +02:00
if [ -d "$GIT_REPO_PATH" ] ; then
2023-07-21 18:56:29 +02:00
cd "$GIT_REPO_PATH"
2023-07-21 19:04:24 +02:00
if [ -d ".git" ] ; then
2023-07-21 18:56:29 +02:00
git pull "$GIT_REPO_REMOTE_NAME" "$GIT_REPO_BRANCH"
else
2023-07-21 19:19:59 +02:00
echo "Error: The directory exists but is not a Git repository"
2023-07-21 18:56:29 +02:00
exit 1
fi
else
git clone "$GIT_REPO_URL" "$GIT_REPO_PATH"
fi
# Save the current value of IFS to restore later
OLD_IFS=$IFS
# Set the IFS to a comma to split the values
IFS=','
2023-07-21 18:57:50 +02:00
while read -r FQDN SSH_KEY_PATH; do
2023-07-21 18:56:29 +02:00
# bereinige FQDN
2023-07-21 18:57:50 +02:00
FQDN=$(echo "$FQDN" | tr -d "[:space:]")
2023-07-21 18:56:29 +02:00
# prüfe ob Key existiert
2023-07-21 19:04:24 +02:00
if [ ! -e "$SSH_KEY_PATH" ] ; then
2023-07-21 19:19:59 +02:00
echo "Error: File $SSH_KEY_PATH does not exist"
echo "Error: can not export $FQDN config"
2023-07-21 18:56:29 +02:00
exit 2
fi
2023-07-21 20:26:53 +02:00
echo "export $FQDN config"
2023-07-21 18:57:50 +02:00
ssh -n -i "$SSH_KEY_PATH" "$FQDN" "/export show-sensitive" > "config_${FQDN}.rsc"
2023-07-21 19:19:59 +02:00
# entferne Datumszeile
2023-07-21 20:26:53 +02:00
sed -i -r '/^# [0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}\:[0-9]{2}\:[0-9]{2}\sby\sRouterOS\s[0-9\s\.]+/d' "config_${FQDN}.rsc"
2023-07-21 20:27:22 +02:00
git add "config_${FQDN}.rsc" --quiet
git commit -m "update config_${FQDN}" --quiet
2023-07-21 18:58:43 +02:00
done <<< "$DEVICES"
git push
# Restore the original IFS value
IFS=$OLD_IFS
2023-07-21 18:56:29 +02:00
# test mitvolume
# ohne volume wg git clone