ssh-copy-id -p port option

Petr Lautrbach plautrba at redhat.com
Wed Dec 21 01:31:41 EST 2011


Hi.

I would like to add an option [-p port] to ssh-copy-id.

If this option is given then ssh-copy-id calls ssh with -p port to connect to
non-standard port.

The patch [1] adds this option to ssh-copy-id and documents it in ssh-copy-id(1) man page

[1] http://plautrba.fedorapeople.org/openssh/718674/ssh-copy-id-p-port.patch


Thanks,

Petr



diff --git a/contrib/ssh-copy-id b/contrib/ssh-copy-id
index 9451ace..a824382 100644
--- a/contrib/ssh-copy-id
+++ b/contrib/ssh-copy-id
@@ -6,22 +6,49 @@
  # or one of the other keys in your ssh-agent, for this to work.

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

-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"
-    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
+while [ $# -gt 0 ]; do
+  case "$1" in
+    -i)
+      shift
+      # remains last argument or next argument is another option
+      if [ -z "$2" ] || expr "$1" : "^--\?[a-z]*\$" > /dev/null ; then
+        OPT_I="$ID_FILE"
+        continue;
+      fi
+      OPT_I="$1"
+      if expr "$1" : ".*\.pub" > /dev/null ; then
+        ID_FILE="$1"
+      else
+        ID_FILE="$1.pub"
+      fi
+      shift
+      continue
+      ;;
+    -p)
+      shift
+      COMMAND="$COMMAND -p $1"
+      shift
+      continue
+      ;;
+    -h|--help)
+      shift $#
+      break
+      ;;
+    *)
+      break
+      ;;
+  esac
+done
+
+if [ "$#" -ne 1 ]; then
+  echo "Usage: $0 [-i [identity_file]] [-p port] [user@]machine" >&2
+  exit 1
+fi
+
+if [ -z "$OPT_I" ] && [ x$SSH_AUTH_SOCK != x ] && ssh-add -L >/dev/null 2>&1; then
+  GET_ID="$GET_ID ssh-add -L"
  fi

  if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
@@ -33,18 +60,13 @@ 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
-fi
-
  # strip any trailing colon
  host=`echo $1 | sed 's/:$//'`

-{ eval "$GET_ID" ; } | ssh $host "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys" || exit 1
+{ eval "$GET_ID" ; } | $COMMAND $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:
+Now try logging into the machine, with "$COMMAND '$host'", and check in:

    ~/.ssh/authorized_keys

diff --git a/contrib/ssh-copy-id.1 b/contrib/ssh-copy-id.1
index cb15ab2..ed81a92 100644
--- a/contrib/ssh-copy-id.1
+++ b/contrib/ssh-copy-id.1
@@ -20,7 +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]]
+.B ssh-copy-id [-i [identity_file]] [-p port]
  .I "[user@]machine"
  .br
  .SH DESCRIPTION
@@ -51,6 +51,11 @@ file.  Once it has one or more fingerprints (by whatever means) it
  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 port is used to log in into a remote machine with
+sshd listening on non-standard port.

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

-- 
Petr Lautrbach, Red Hat, Inc.
http://cz.redhat.com


More information about the openssh-unix-dev mailing list