ssh-copy-id revision 225825
155682Smarkm#!/bin/sh
2102644Snectar
355682Smarkm# Shell script to install your public key on a remote machine
455682Smarkm# Takes the remote machine name as an argument.
555682Smarkm# Obviously, the remote machine must accept password authentication,
655682Smarkm# or one of the other keys in your ssh-agent, for this to work.
755682Smarkm
855682SmarkmID_FILE="${HOME}/.ssh/id_rsa.pub"
955682Smarkm
1055682Smarkmif [ "-i" = "$1" ]; then
1155682Smarkm  shift
1255682Smarkm  # check if we have 2 parameters left, if so the first is the new ID file
1355682Smarkm  if [ -n "$2" ]; then
1455682Smarkm    if expr "$1" : ".*\.pub" > /dev/null ; then
1555682Smarkm      ID_FILE="$1"
1655682Smarkm    else
1755682Smarkm      ID_FILE="$1.pub"
1855682Smarkm    fi
1955682Smarkm    shift         # and this should leave $1 as the target name
2055682Smarkm  fi
2155682Smarkmelse
2255682Smarkm  if [ x$SSH_AUTH_SOCK != x ] && ssh-add -L >/dev/null 2>&1; then
2355682Smarkm    GET_ID="$GET_ID ssh-add -L"
2455682Smarkm  fi
2555682Smarkmfi
2655682Smarkm
2755682Smarkmif [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
2855682Smarkm  GET_ID="cat \"${ID_FILE}\""
2955682Smarkmfi
3055682Smarkm
3155682Smarkmif [ -z "`eval $GET_ID`" ]; then
3255682Smarkm  echo "$0: ERROR: No identities found" >&2
3355682Smarkm  exit 1
3455682Smarkmfi
3555682Smarkm
3655682Smarkmif [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
3755682Smarkm  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
3855682Smarkm  exit 1
3955682Smarkmfi
4055682Smarkm
4155682Smarkm# strip any trailing colon
4255682Smarkmhost=`echo $1 | sed 's/:$//'`
4355682Smarkm
4455682Smarkm{ eval "$GET_ID" ; } | ssh $host "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys" || exit 1
4555682Smarkm
4690926Snectarcat <<EOF
4755682SmarkmNow try logging into the machine, with "ssh '$host'", and check in:
48107207Snectar
4990926Snectar  ~/.ssh/authorized_keys
50103423Snectar
51103423Snectarto make sure we haven't added extra keys that you weren't expecting.
5255682Smarkm
5355682SmarkmEOF
5455682Smarkm
5555682Smarkm