Created
June 29, 2017 11:37
-
-
Save HalisCz/118c2aab824a2dc84260051fd765cc1d to your computer and use it in GitHub Desktop.
Shell function to ssh into server and authenticate with old ssh key, and then add new ssh key into authorized_keys
This file contains 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
ssh-copy-id-exchange() { | |
OLDKEY="/home/halis/.ssh/id_rsa-old" | |
OLDKEY_PUB="/home/halis/.ssh/id_rsa-old.pub" | |
NEWKEY="/home/halis/.ssh/id_rsa" | |
NEWKEY_PUB="/home/halis/.ssh/id_rsa.pub" | |
echo "Trying login with new key" | |
ssh -i $NEWKEY -o "PreferredAuthentications=publickey" $1 "true" | |
#exit if it successed | |
if [ $? -eq "0" ]; then | |
echo "New key is on the server, nothing to do" | |
#try old key if it did not work | |
else | |
echo "New key is not on the server" | |
#try to login with old key | |
ssh -i $OLDKEY -o "PreferredAuthentications=publickey" $1 "true" | |
#if it succeed, grep new key in authorized_keys | |
if [ $? -eq "0" ]; then | |
echo "Old key is on the server, proceeding" | |
ssh -i $OLDKEY -o "PreferredAuthentications=publickey" $1 "grep '$(cat $NEWKEY_PUB)' ~/.ssh/authorized_keys" | |
if [ $? -eq "0" ]; then | |
echo "Weird, new key is in the authorized_keys" | |
#if not found, add it | |
else | |
cat $NEWKEY_PUB | ssh -i $OLDKEY -o "PreferredAuthentications=publickey" $1 "cat >> .ssh/authorized_keys" && \ | |
echo "New key added to authorized_keys" && \ | |
#try login with new key | |
ssh -i $NEWKEY -o "PreferredAuthentications=publickey" $1 "true" && \ | |
echo "Succesfully loged in with new key. My job here is done." | |
fi | |
#If it did not succeed, end. | |
else | |
echo "Old key is not on the server" | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment