Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Create self-contained SSH key scripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create self-contained SSH key scripts
Authored by: randalla on Sep 12, '07 08:24:59AM
Feel free to use, hack, dismiss, whatever the following script that automates the creation of your private keys, transmitting them to the remote machine, and setting up the custom host in your ~/.ssh/config file:

~/bin/installSSHKey:
---- COPY BELOW THIS LINE ----
#!/bin/sh

USER="${1}"
HOST="${2}"
ALIAS="${3}"

if [ ! "${USER}" ] && [ ! "${HOST}" ] && [ ! "${ALIAS}" ]; then
echo
echo "Usage: installSSHKey username hostname alias"
echo
echo " username = Your username on the remote system (not necessarily your current username: `whoami`)"
echo " hostname = The hostname (domain name or IP address) of the remote server"
echo " alias = The ssh alias for this host to be created"
echo
fi

if [ ! "${USER}" ]; then
echo "No ssh username specified (EG: `whoami`)"
exit 1
fi

if [ ! "${HOST}" ]; then
echo "No hostname specified (EG: www.example.com)"
exit 1
fi

if [ ! "${ALIAS}" ]; then
echo "No ssh alias specified (EG: example)"
exit 1
fi

cd ~

rm -rf .ssh/known_hosts

if [ ! -r .ssh ]; then
echo -n "Creating hidden .ssh folder in home directory..."
mkdir -p .ssh
chmod 700 .ssh
echo "Done"
fi

if [ ! -r .ssh/config ]; then
echo -n "Creating host configuration file..."
chmod 600 .ssh/config
echo "Done"
fi

if ! grep -q "Host ${ALIAS}" .ssh/config
then
echo -n "Adding host to config file..."
echo "
Host ${ALIAS}
HostKeyAlias ${ALIAS}
Hostname "${HOST}"
User ${USER}
Compression yes" >> .ssh/config
echo "Done"
fi

if [ ! -r .ssh/id_rsa.pub ]; then
echo -n "Creating RSA private and public keys..."
ssh-keygen -q -t rsa -f .ssh/id_rsa -N "" -P ""
chmod 600 .ssh/id_rsa*
echo "Done"
fi

if [ ! -r .ssh/id_dsa.pub ]; then
echo -n "Creating DSA private and public keys..."
ssh-keygen -q -t dsa -f .ssh/id_dsa -N "" -P ""
chmod 600 .ssh/id_dsa*
echo "Done"
fi

echo "Installing SSH public keys onto server: ${HOST}..."
echo "You will be prompted for your remote password."
ssh "${ALIAS}" \
mkdir -p .ssh\; \
echo "`cat .ssh/id_rsa.pub`" \>\> .ssh/authorized_keys\; \
echo "`cat .ssh/id_dsa.pub`" \>\> .ssh/authorized_keys2\; \
chmod 700 .ssh\; \
chmod 600 .ssh/*\;

if [ ! $? = 0 ]; then
echo "An error occurred, please overview your output"
else
echo "Done"
echo
echo "You can now access the server ${HOST} by typing:"
echo
echo " ssh ${ALIAS}"
echo
echo "Commands can be performed without logging in by doing:"
echo
echo " ssh ${ALIAS} commandname"
echo
echo "Examples:"
echo " ssh ${ALIAS} whoami"
echo " ssh ${ALIAS} ps ax"
echo " ssh ${ALIAS} tail -f /var/log/system.log"
echo
fi

chmod 600 .ssh/*
---- COPY ABOVE THIS LINE ----

the above script, I named installSSHKey would be executed as such:

installSSHKey username hostaddress alias
or
installSSHKey myusername ssh.example.com example

When run, it'll kill off the .ssh/known_hosts file (it's lazy), ask you to authorize the host, ask you to enter the user's password on the remote machine, and then do it's magic. Note, the script is smart enough not to pollute your ~/.ssh/config file with duplicate custom host entries. However, it's not smart enough to check if you've changed anything in the host entry, like the host address. If you do that, you need to first remove the entry from .ssh/config and then run this script. Or you could make it better yourself :)

You could then use a .command wrapper to this to set up specific hosts directly:

foo.command:
---- COPY BELOW THIS LINE ----
#!/bin/sh

installSSHKey username hostaddress alias
---- COPY ABOVE THIS LINE ----

---
Xaren Development
http://www.xaren.net

[ Reply to This | # ]

Create self-contained SSH key scripts
Authored by: archdata on Sep 12, '07 08:44:36AM
The above script works like a charm.

Just a small note - the ALIAS (for those that don't know - like me for about 20 minutes) is the Name you want to give the computer you are connecting to.

Therefore, to connect to your home computer called "FooBar" you could use the command

installSSHkey USERNAME (Your name) HOST (www.example.com) ALIAS (FooBar)

To use this alias you can now type "ssh FooBar" on the command line and you are connected to your home computer.

The alias can be whatever you want it to be as long as it makes sense to you.

[ Reply to This | # ]