This shows you the differences between two versions of the page.
add_ssh_key_to_multiple_host [2010/09/22 02:20] k2patel |
add_ssh_key_to_multiple_host [2020/08/10 02:35] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== SSH Key adding ====== | ||
- | - Assuming you already have key setup for your machine. | ||
- | <code bash> | ||
- | |||
- | #!/bin/bash | ||
- | |||
- | hostlist=( 'alpha.lan' 'beta.lan' 'gamma.lan' 'delta.lan' 'epsilon.lan' ) | ||
- | # get number of elements in the array | ||
- | chost=${#hostlist[@]} | ||
- | |||
- | |||
- | for (( i=0;i<$chost;i++)); do | ||
- | echo ${hostlist[${i}]} | ||
- | file=`cat /dev/urandom|od -N4 -An -i` | ||
- | scp ${hostlist[${i}]}:.ssh/authorized_keys /tmp/$file | ||
- | if cat /tmp/$file | grep "`cat mike.pub`" | ||
- | then | ||
- | echo "Key already exists" | ||
- | rm -rf /tmp/$file | ||
- | else | ||
- | cat mike.pub >> /tmp/$file | ||
- | if cat /tmp/$file | grep "`cat mike.pub`" | ||
- | then | ||
- | echo "Key Successfully Adde" | ||
- | scp /tmp/$file ${hostlist[${i}]}:.ssh/authorized_keys | ||
- | rm -rf /tmp/$file | ||
- | else | ||
- | echo "Failed to add key for host \"${hostlist[${i}]}\" Check manually.\n" | ||
- | echo "Local File \"/tmp/$file\" " | ||
- | fi | ||
- | fi | ||
- | done | ||
- | |||
- | </code> |