Skip to content

Instantly share code, notes, and snippets.

@sergioccrr
Created December 5, 2014 18:03
Show Gist options
  • Save sergioccrr/235d41786377c274c27e to your computer and use it in GitHub Desktop.
Save sergioccrr/235d41786377c274c27e to your computer and use it in GitHub Desktop.
Script for create and append SSH key in a remote server
#!/bin/sh
FKEY="$HOME/.ssh/id_rsa"
FPUB="$FKEY.pub"
if [ ! -f "$FPUB" ]; then
echo 'Type your email, followed by [ENTER]:'
read COMMENT
ssh-keygen -t rsa -N '' -C "$COMMENT" -f "$FKEY"
fi
TPARENT="/tmp/$(basename $0)-$$"
TDIR="$TPARENT/.ssh"
mkdir -p "$TDIR"
if [ $? -ne 0 ]; then
echo 'fatal';
exit
fi
TFILE="$TDIR/authorized_keys"
echo 'Type user of server, followed by [ENTER]:'
read SUSER
echo 'Type host of server, followed by [ENTER]:'
read SHOST
scp "$SUSER@$SHOST:~/.ssh/authorized_keys" "$TFILE"
if [ $? -ne 0 ]; then
touch "$TFILE"
fi
cat "$FPUB" >> "$TFILE"
scp -r "$TPARENT/." "$SUSER@$SHOST:~/"
rm "$TFILE"
rmdir "$TDIR"
rmdir "$TPARENT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment