Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright 2018-present Open Networking Foundation |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 4 | # |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 6 | # use this file except in compliance with the License. You may obtain a copy |
| 7 | # of the License at: |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations under |
| 15 | # the License. |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 16 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 17 | # copy-ssh-keys.sh - Adds ssh keys to nodes given as parameters to the script, |
| 18 | # after removing them from the ~/.ssh/known_hosts file on the local system. |
| 19 | # |
| 20 | # This script should be run interactively as it will prompt for input, and only |
| 21 | # invoked once, so as not to add multiple copies of the SSH key to the remote |
| 22 | # system. |
Luca Prete | c97ad88 | 2018-05-04 11:22:14 -0700 | [diff] [blame] | 23 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 24 | set -e -u -o pipefail |
| 25 | |
| 26 | REMOTE_SSH_USER="${REMOTE_SSH_USER:-cord}" |
| 27 | SSH_PUBKEY_PATH="${SSH_PUBKEY_PATH:-${HOME}/.ssh/id_rsa.pub}" |
| 28 | |
| 29 | SSH_PUBKEY=$(cat "${SSH_PUBKEY_PATH}") |
| 30 | |
| 31 | for NODE in "$@"; |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 32 | do |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 33 | # remove key for this node from local ~/.ssh/known_hosts file |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 34 | ssh-keygen -R "${NODE}" |
Zack Williams | 11b2e5c | 2018-05-18 09:50:54 -0700 | [diff] [blame] | 35 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 36 | # copy the ssh key to the remote system ~/.ssh/authorized_keys file |
| 37 | # shellcheck disable=SC2029 |
| 38 | ssh "${REMOTE_SSH_USER}@${NODE}" "umask 0077 && mkdir -p ~/.ssh && echo \"${SSH_PUBKEY}\" >> ~/.ssh/authorized_keys" |
| 39 | done |