#!/bin/bash accounts=( \ somehost.example.com \ someotherhost.example.com \ someuser@example.com ) # e.g. prefix='from="myhost" ' prefix='' auth_key_file=$HOME/.ssh/new_authorized_keys_file echo "# Auto generated authorized_keys file" > $auth_key_file echo "# $(date -R)" >> $auth_key_file # list of keys that i authorize echo "$prefix$(cat $HOME/.ssh/id_rsa.pub)" >> $auth_key_file command=" cat - > ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys [[ -f ~/.ssh/authorized_hosts ]] && rm ~/.ssh/authorized_hosts [[ -f ~/.ssh/authorized_keys2 ]] && rm ~/.ssh/authorized_keys2 " for account in ${accounts[@]}; do echo Changing keys for $account ssh \ $account \ "$command" \ < $auth_key_file done rm $auth_key_file