Skip to content

Instantly share code, notes, and snippets.

@fliphess
Last active February 11, 2017 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fliphess/1807427c7f7115041be7 to your computer and use it in GitHub Desktop.
Save fliphess/1807427c7f7115041be7 to your computer and use it in GitHub Desktop.
Script to put your ssh key on the hypernode vagrant
#!/bin/bash
TMPFILE="$( mktemp )"
## Test if there are keys loaded in the SSH agent
if (ssh-add -l | grep -q "The SSH agent has no identities" ) ; then
echo "Please add some keys to your ssh agent first!"
exit 1
fi
## Get all keys from SSH agent
echo "Getting all keys from SSH agent"
for KEY in $( ssh-add -l | awk '{print $(NF-1) }' ); do
PUBKEY="${KEY}.pub"
if [ ! -f "$PUBKEY" ] ; then
echo "Public key for $KEY not found! Skipping..."
continue
fi
cat $PUBKEY >> $TMPFILE && echo >> $TMPFILE
done
## Echo keys to ~app/.ssh/authorized_keys
echo "Putting public keys on vagrant node"
cat $TMPFILE | vagrant ssh -- "cat - > /tmp/key ; sudo mkdir -p /data/web/.ssh ; sudo mv /tmp/key /data/web/.ssh/authorized_keys ; sudo chmod 600 /data/web/.ssh/authorized_keys ; sudo chown app:app /data/web/.ssh/authorized_keys"
## Validate output and retrieve ip
if [ "$?" != 0 ]; then
echo "Something went wrong placing your public key on your hypernode vagrant"
else
IP="$( vagrant ssh -- "ifconfig eth1 | grep inet" | cut -d: -f2 | awk '{print $1}')"
echo "You can now log in with ssh as user app@$IP"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment