Created
January 3, 2025 19:38
-
-
Save Toomoch/c14acf6c6bdda95d9e137fe13f77a11b to your computer and use it in GitHub Desktop.
OpenWrt flash script that only preserves ssh key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
usage() { | |
echo "Usage: $0 -f <file> <hostname>" | |
exit 1 | |
} | |
file="" | |
while getopts "f:h" opt; do | |
case ${opt} in | |
f) | |
file=${OPTARG} | |
;; | |
h) | |
usage | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Get the hostname from the remaining arguments | |
if [ $# -lt 1 ]; then | |
echo "Error: Hostname argument is required." | |
usage | |
fi | |
hostname=$1 | |
if [ -z "$file" ]; then | |
echo "Error: -f flag requires a file argument." | |
usage | |
fi | |
filebase=$(basename "${file}") | |
script=$(cat <<ENDSSH | |
#!/usr/bin/env sh | |
TMPFILE="\$(mktemp -u)" | |
echo -e "\e[31mBacking up SSH keys...\$TMPFILE\e[0m" | |
tar cvzf "\$TMPFILE" /etc/dropbear/* | |
echo -e "\e[31mBacked up to \$TMPFILE\e[0m" | |
SHASUM="\$(sha256sum /tmp/${filebase} | cut -d " " -f 1 )" | |
echo -e "\n\e[31msha256sum for ${filebase} is \$SHASUM. \e[0m" | |
read -rp "Do you want to proceed? (y/n): " yn | |
if [[ "\$yn" != "y" ]]; then | |
echo "Operation aborted." | |
exit 1 | |
fi | |
sysupgrade -f "\$TMPFILE" /tmp/${filebase} | |
ENDSSH | |
) | |
scp -O "$file" root@"${hostname}":/tmp | |
echo "$script" | ssh root@"$hostname" "cat > /tmp/upgrscript.sh && chmod +x /tmp/upgrscript.sh" | |
ssh -tt root@"${hostname}" "/tmp/upgrscript.sh" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment