patch proposal for ssh-copy-id script

Sebastiano Di Paola sebastiano.dipaola at gmail.com
Wed Oct 3 00:01:16 EST 2012


-- BEGIN PATCH CUT AFTER THIS LINE --

diff -rupN d/openssh-6.1p1/contrib/ssh-copy-id
c/openssh-6.1p1/contrib/ssh-copy-id
--- d/openssh-6.1p1/contrib/ssh-copy-id	2011-08-17 04:05:49.000000000 +0200
+++ c/openssh-6.1p1/contrib/ssh-copy-id	2012-10-02 15:41:44.000000000 +0200
@@ -7,21 +7,39 @@

 ID_FILE="${HOME}/.ssh/id_rsa.pub"

-if [ "-i" = "$1" ]; then
-  shift
-  # check if we have 2 parameters left, if so the first is the new ID file
-  if [ -n "$2" ]; then
-    if expr "$1" : ".*\.pub" > /dev/null ; then
-      ID_FILE="$1"
-    else
-      ID_FILE="$1.pub"
+# help function
+usage() {
+       echo "Usage: $0 [-i [identity_file]] [-p [port]] -h [user@]machine" >&2;
+}
+
+while getopts "i:p:h:" option; do
+  case "$option" in
+    i)  ID_FILE="$OPTARG"
+        ;;
+    p)  PORT="$OPTARG"
+        ;;
+    h)  HOST="$OPTARG"
+        ;;
+    ?)  usage
+        exit 1
+        ;;
+    esac
+done
+
+if [ -z "$HOST" ]; then
+    echo "$0: ERROR: No destination host specified" >&2
+    usage
+    exit 1
+fi
+
+if [ -n "$ID_FILE" ]; then
+    if ! expr "$ID_FILE" : ".*\.pub" > /dev/null ; then
+        ID_FILE="$ID_FILE.pub"
     fi
-    shift         # and this should leave $1 as the target name
-  fi
 else
-  if [ x$SSH_AUTH_SOCK != x ] && ssh-add -L >/dev/null 2>&1; then
-    GET_ID="$GET_ID ssh-add -L"
-  fi
+    if [ x$SSH_AUTH_SOCK != x ] && ssh-add -L >/dev/null 2>&1; then
+        GET_ID="$GET_ID ssh-add -L"
+    fi
 fi

 if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
@@ -33,15 +51,16 @@ if [ -z "`eval $GET_ID`" ]; then
   exit 1
 fi

-if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
-  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
-  exit 1
+# set the ssh server port to use it also on port != 22 if not set $PORT_STRING
+# will be unset so no problem to refer to it in the ssh command
+if [ -n "$PORT" ]; then
+    PORT_STRING="-p $PORT"
 fi

 # strip any trailing colon
-host=`echo $1 | sed 's/:$//'`
+HOST=`echo $HOST | sed 's/:$//'`

-{ eval "$GET_ID" ; } | ssh $host "umask 077; test -d ~/.ssh || mkdir
~/.ssh ; cat >> ~/.ssh/authorized_keys" || exit 1
+{ eval "$GET_ID" ; } | ssh $PORT_STRING $HOST "umask 077; test -d
~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys" || exit 1

 cat <<EOF
 Now try logging into the machine, with "ssh '$host'", and check in:
diff -rupN d/openssh-6.1p1/contrib/ssh-copy-id.1
c/openssh-6.1p1/contrib/ssh-copy-id.1
--- d/openssh-6.1p1/contrib/ssh-copy-id.1	2010-07-19 13:24:13.000000000 +0200
+++ c/openssh-6.1p1/contrib/ssh-copy-id.1	2012-10-02 15:38:42.000000000 +0200
@@ -20,8 +20,7 @@ the original English.
 .SH NAME
 ssh-copy-id \- install your public key in a remote machine's authorized_keys
 .SH SYNOPSIS
-.B ssh-copy-id [-i [identity_file]]
-.I "[user@]machine"
+.B ssh-copy-id [-i [identity_file]] [-p [port]] -h "[user@]machine"
 .br
 .SH DESCRIPTION
 .BR ssh-copy-id
@@ -51,6 +50,14 @@ file.  Once it has one or more fingerpri
 uses ssh to append them to
 .B ~/.ssh/authorized_keys
 on the remote machine (creating the file, and directory, if necessary.)
+.PP
+If the
+.B -p
+option is given then the ssh connection is made on specified port and not
+on standard port 22
+.PP
+.B Return value
+0 on success and 1 on error

 .SH NOTES
 This program does not modify the permissions of any

-- END PATCH CUT BEFORE THIS LINE --

Sebastiano Di Paola


More information about the openssh-unix-dev mailing list