1#!/bin/sh 2#- 3# Copyright (c) 2012 Eitan Adler 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer 11# in this position and unchanged. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28# $FreeBSD$ 29 30usage() { 31 echo "usage: ssh-copy-id [-l] [-i keyfile] [-o option] [-p port] [user@]hostname" >&2 32 exit 1 33} 34 35sendkey() { 36 local h="$1" 37 shift 1 38 local k="$@" 39 echo "$k" | ssh $port -S none $options "$user$h" /bin/sh -c \'' 40 set -e; 41 umask 077; 42 keyfile=$HOME/.ssh/authorized_keys ; 43 mkdir -p $HOME/.ssh/ ; 44 while read alg key comment ; do 45 if ! grep -sqwF "$key" "$keyfile"; then 46 echo "$alg $key $comment" | 47 tee -a "$keyfile" >/dev/null ; 48 fi ; 49 done 50 '\' 51} 52 53agentKeys() { 54 keys="$(ssh-add -L | grep -v 'The agent has no identities.')$nl$keys" 55} 56 57keys="" 58host="" 59hasarg="" 60user="" 61port="" 62nl=" 63" 64options="" 65 66while getopts 'i:lo:p:' arg; do 67 case $arg in 68 i) 69 hasarg="x" 70 if [ -f "$OPTARG" ]; then 71 keys="$(cat $OPTARG)$nl$keys" 72 fi 73 ;; 74 l) 75 hasarg="x" 76 agentKeys 77 ;; 78 p) 79 port="-p $OPTARG" 80 ;; 81 o) 82 options="$options -o '$OPTARG'" 83 ;; 84 *) 85 usage 86 ;; 87 esac 88done >&2 89 90shift $((OPTIND-1)) 91 92if [ -z "$hasarg" ]; then 93 agentKeys 94fi 95if [ -z "$keys" -o "$keys" = "$nl" ]; then 96 echo "no keys found" >&2 97 exit 1 98fi 99if [ -z "$@" ]; then 100 usage 101fi 102 103for host in "$@"; do 104 sendkey "$host" "$keys" 105done 106