Created
July 24, 2010 00:04
-
-
Save unixmonkey/488231 to your computer and use it in GitHub Desktop.
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
| #!/bin/bash | |
| # ssh-eternal is a poor-man's clone of ssh-forever | |
| # (http://github.com/mattwynne/ssh-forever) | |
| # | |
| # It's purpose is to run this the first time you connect to a | |
| # remote computer with ssh. This script will then copy your public | |
| # key to the remote computers ~/.ssh/authorized_keys file to | |
| # allow subsequent logins to be done without having to remember | |
| # the remote machine's password every time. | |
| # | |
| # This derivative work was rewritten to work entirely in bash and | |
| # similar shells on machines without ruby or rubygems. | |
| # | |
| # Because the logic for this is wrapped in a single function, | |
| # it can be easily added to one's .profile or .aliases file | |
| # without ever needing to be installed | |
| # | |
| # However, to install, chmod +x the file, and place somewhere | |
| # in your executable path, like /usr/local/bin | |
| # | |
| # Licensed under the MIT license: | |
| # http://www.opensource.org/licenses/mit-license.php | |
| function ssh-eternal() { | |
| HELP='Usage: ssh-eternal username@yourserver.com [port] [identity_file]' | |
| LOGIN=$1 | |
| PORT=$2 | |
| ALTKEY=$3 | |
| KEYFILE=~/.ssh/id_rsa.pub | |
| # print help in absence of any arguments | |
| if [ $# == 0 ]; then | |
| echo $HELP | |
| exit 1 | |
| fi | |
| # check for port | |
| if [ $PORT ]; then | |
| echo "Connecting to ssh on port $PORT" | |
| ARGS=" -p $PORT" | |
| else | |
| ARGS="" | |
| fi | |
| # check for public key | |
| if [ $ALTKEY ]; then | |
| echo "Using public key file at $ALTKEY" | |
| KEY=`cat $ALTKEY` | |
| elif [ -e $KEYFILE ]; then | |
| echo "Found public key at $KEYFILE" | |
| KEY=`cat $KEYFILE` | |
| else | |
| echo "You do not appear to have a public key." | |
| echo "I expected to find one at '$KEYFILE'" | |
| echo -n "Would you like me to generate one [Y/n]" | |
| read | |
| if [[ "$REPLY" == "" || "$REPLY" == "y" || "$REPLY" == "Y" ]]; then | |
| ssh-keygen -t rsa | |
| else | |
| echo "Fair enough, I'll be off then." | |
| echo "You can generate your own by hand by running 'ssh-keygen -t rsa'" | |
| exit 2 | |
| fi | |
| fi | |
| # build key copy command | |
| echo "Copying your public key to the remote server. Prepare to enter your password for the last time." | |
| C1="mkdir -p ~/.ssh" | |
| C2="chmod 700 ~/.ssh" | |
| C3="touch ~/.ssh/authorized_keys" | |
| C4="chmod 700 ~/.ssh/authorized_keys" | |
| C5="echo $KEY >> ~/.ssh/authorized_keys" | |
| REMOTE_COMMAND="$C1 && $C2 && $C3 && $C4 && $C5" | |
| # ssh public key to remote server | |
| `ssh $LOGIN$ARGS "$REMOTE_COMMAND"` | |
| if [ $? == 0 ]; then | |
| echo "Success. From now on you can use just plain old 'ssh $LOGIN'. Logging you in.." | |
| ssh $LOGIN$ARGS | |
| exit 0 | |
| else | |
| echo "Unable to log in." | |
| exit 1 | |
| fi | |
| } | |
| ssh-eternal $1 $2 $3 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much shorter version. No help, error checking etc.
function ssh-copykey() {
cat ~/.ssh/id_rsa.pub | ssh $1 "cat - >> ~/.ssh/authorized_keys"
}