Skip to content

Instantly share code, notes, and snippets.

@nikuyoshi
Created July 15, 2016 06:21
Show Gist options
  • Save nikuyoshi/08d20662993f53156be79f05d9d5515d to your computer and use it in GitHub Desktop.
Save nikuyoshi/08d20662993f53156be79f05d9d5515d to your computer and use it in GitHub Desktop.
#!/bin/sh
#######################################################
#
# Precondition: Complete executing the following command
# ssh-keygen -t rsa
#
# How to use:
# sh ssh-auto-auth.sh {user name}@{IP address}
#
# ex. sh ssh-auto-auth.sh root@192.168.1.1
#
# FYI:
# http://www.linuxproblem.org/art_9.html
#
#######################################################
if [ $# -lt 1 -o $# -gt 1 ]; then
echo -e "How to use: sh ssh-auto-auth.sh {user name}@{IP address}"
exit 1
fi
# Use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine).
ssh "$1" mkdir -p .ssh
if [ $? -ne 0 ]; then
echo -e "Failed to create .ssh directory."
exit 1
fi
# Append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time.
cat ~/.ssh/id_rsa.pub | ssh "$1" 'cat >> .ssh/authorized_keys'
if [ $? -ne 0 ]; then
echo -e "Failed to append new public key to authorized_keys"
exit 1
fi
echo "Complete!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment