Skip to content

Instantly share code, notes, and snippets.

@AddoSolutions
Created November 4, 2013 16:43
Show Gist options
  • Save AddoSolutions/7305429 to your computer and use it in GitHub Desktop.
Save AddoSolutions/7305429 to your computer and use it in GitHub Desktop.
This little bash script will check if you are already set up with a ssh configuration for a given domain, if not, it will create a private key for you, and if possible, upload it to the remote server for future use, it will also update your local ssh_config file with the correct key and username.
#!/bin/bash
DOMAIN=$1
KEYPATH="~/.ssh/servers/"
# Check for already set-up configuration, if so, jsut connect
echo ~/.ssh/servers/$DOMAIN
if [ -f ~/.ssh/servers/$DOMAIN ];
then
echo "Connecting you to $DOMAIN"
ssh $DOMAIN
exit
fi
echo "Creating you a new keypair to use"
echo "What is the remote username?"
read USER
echo "Cranking out a new key"
ssh-keygen -t rsa -b 4096 -f ~/.ssh/servers/$DOMAIN -q
PUBKEY=`cat cat ~/.ssh/servers/$DOMAIN.pub`
echo "If you would like me to upload the key for you, please give me the passphrase to log-into the remote server"
ssh $USER@$DOMAIN << EOF
echo '$PUBKEY' >> ~/.ssh/authorized_keys
echo '$PUBKEY' >> ~/private/.ssh/authorized_keys
EOF
echo "" >> ~/.ssh/config
echo "Host $DOMAIN" >> ~/.ssh/config
echo "IdentityFile ~/.ssh/servers/$DOMAIN" >> ~/.ssh/config
echo "User $USER" >> ~/.ssh/config
## Save entry into the local ssh config
## Save to remote authorized keys
echo "";
echo "All Done...";
echo "";
qssh $DOMAIN
@AddoSolutions
Copy link
Author

Usage:

qssh mydomain.com

I am using a MacBook Pro on Mavericks/Lion. It is a little dirty, but very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment