diff options
| author | Anton Bobov <anton@bobov.name> | 2025-08-01 09:47:23 +0500 |
|---|---|---|
| committer | Anton Bobov <anton@bobov.name> | 2025-08-01 09:47:23 +0500 |
| commit | 90b3810aa0fc741a7ce7e95219b00be4c748998c (patch) | |
| tree | 2b1f9dc50d12e931a93b9f1814f8d33ab367b776 | |
| parent | 1630da7e1a1e48a1706d8657bd3958ffaa753d1c (diff) | |
| -rwxr-xr-x | ssh-key-upgrade | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ssh-key-upgrade b/ssh-key-upgrade new file mode 100755 index 0000000..b15ce1e --- /dev/null +++ b/ssh-key-upgrade @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# ssh key migration script, allow to replace old ssh key to new seamlessly +# +# Usage example as ssh command replacement: +# +# ssh() { +# if [ $# -eq 1 ]; then +# ssh-key-upgrade "$1" +# fi +# command ssh "$@" +# } + +set -euo pipefail + +SSH_KEY_UPGRADE_OLD_KEY="${SSH_KEY_UPGRADE_OLD_KEY:-$HOME/.ssh/id_rsa.pub}" +SSH_KEY_UPGRADE_NEW_KEY="${SSH_KEY_UPGRADE_ONEW_KEY:-$HOME/.ssh/id_ed25519.pub}" + +main() { + local host="$1" + local keys='.ssh/authorized_keys' + local oldkey + oldkey=$(cat "${SSH_KEY_UPGRADE_OLD_KEY}") + if ssh "$host" "grep -Fq '$oldkey' '$keys'"; then + ssh-copy-id -i "$SSH_KEY_UPGRADE_NEW_KEY" "$host" + ssh "$host" "sed -i -e '\#$oldkey#d' $keys" + fi +} + +main "$@" |
