ShadowArch/Shared/ssh-authorize-host
2018-02-13 16:52:55 -06:00

42 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
#--------------------------------------------------------------------#
#File: ssh-authorize-host
#
#Description: Add a public key to a remote SSH-capable host.
#
#Package: AniNIX::MiscScripts
#Copyright: WTFPL
#
#Author: DarkFeather
#Changelog by editor:
#--------------------------------------------------------------------#
# Show the usage for this script.
function usage {
echo "Usage: $0 host [ pubkeyfile ]"
}
# Create the initial SSH command.
function initialSSHCommand() {
echo 'mkdir -p ${HOME}/.ssh'
echo 'touch ${HOME}/.ssh/authorized_keys'
echo 'chmod 0700 ${HOME} ${HOME}/.ssh'
echo 'chmod 0600 ${HOME}/.ssh/authorized_keys'
echo 'chown -R `whoami`":" ${HOME}'
echo 'cat >> ${HOME}/.ssh/authorized_keys'
}
# Parse the CLI arguments.
host="$1"
if [ -z "$host" ]; then usage; exit 1; fi
pubkeyfile="$2"
if [ -z "$pubkeyfile" ] || [ ! -f "$pubkeyfile" ]; then
pubkeyfile="${HOME}/.ssh/id_rsa.pub"
fi
# Execute on the remote host.
(initialSSHCommand; cat "$pubkeyfile") | ssh "$host" /bin/bash