Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
Last active December 7, 2015 01:01
Show Gist options
  • Select an option

  • Save hashbrowncipher/e84baf6afc0487809b71 to your computer and use it in GitHub Desktop.

Select an option

Save hashbrowncipher/e84baf6afc0487809b71 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
set -o nounset
set -o pipefail
client=
server=
make_ssh_key() {
local keyfile="$(mktemp -u $TMPDIR/sshkey.XXXXXXXXXX)"
local keyfile_b="$(basename $keyfile)"
local keyfile_rand=$(echo "$keyfile_b" | cut -f2 -d'.')
ssh-keygen -t ed25519 -N '' -f "${keyfile}" -C "$(hostname) temp:${keyfile_rand}" > /dev/null
echo "$keyfile"
}
serverauthkeys_entry() {
echo -n "no-pty,no-port-forwarding,no-agent-forwarding,no-x11-forwarding,no-user-rc,command=\"/nail/sys/bin/rrsync $1\" "
cat "$2"
}
add_authkey() {
local client="$1"
local server="$2"
local dir="$3"
local keyfile="$4"
local keyfile_b="$(basename $keyfile)"
local keyfile_rand=$(echo "$keyfile_b" | cut -f2 -d'.')
local public_keyfile="${keyfile}.pub"
serverauthkeys_entry "$dir" "$public_keyfile" | ssh "$server" -- "/nail/sys/bin/add_temp_authorized_key '${keyfile_rand}'"
cat "$public_keyfile" | ssh "$client" -- "/nail/sys/bin/add_temp_authorized_key '${keyfile_rand}'"
}
if [[ "${E:-}" == "5" ]]; then
ssh-add "$1" 2> /dev/null
shift
ssh "$client" -- rsync "$@" "${server}:"
else
keyfile="$(make_ssh_key)"
add_authkey "$client" "$server" "/tmp" "${keyfile}"
E=5 ssh-agent "$0" "${keyfile}" "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment