#! /bin/bash # Launch this script as $user in the current node to set up passwordless ssh to-and-fro the current node and all other nodes in the cluster for the $user ## Generate two files, id_rsa and id_rsa.pub, in $HOME/.ssh in the current compute node echo 1. Generate two files, id_rsa and id_rsa.pub, in $HOME/.ssh in the current compute node $(echo $(hostname)) if [[ -e $HOME/.ssh/id_rsa.pub ]]; then echo pub rsa exists. Will not create any.; else echo see me 1; ssh-keyscan 192.168.1.10 >> $HOME/.ssh/known_hosts; yes '' | ssh-keygen -t rsa; fi echo ' ' /share/apps/local/bin/coc-sshreachable hosts=$(cat /share/tmp/ssh-reachable.dat) TMP_PASS=$(cat $HOME/passwd.dat) echo '$USER=' $USER 'TMP_PASS=' $TMP_PASS for rn in $hosts do #sshpass -p $TMP_PASS ssh -X -Y $rn "hostname" # worked echo 2. Copy the rsa key of the $USER in the current node $(hostname) to the remote node $rn sshpass -p $TMP_PASS ssh-copy-id -i $HOME/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $USER@$rn ### worked echo ' ' echo 3. Remotely generates the rsa key of the user $USER in the remote node $rn by ssh-ing from the current node $(hostname) sshpass -p $TMP_PASS ssh -oStrictHostKeyChecking=no $rn "if [[ -e $HOME/.ssh/id_rsa.pub ]] ; then echo Will not generate id_rsa.pub; else ssh-keyscan 192.168.1.10 >> $HOME/.ssh/known_hosts; yes '' | ssh-keygen -t rsa; echo 'see me 2' ; fi" echo ' ' echo 4. Remotely ssh-copy-id from the remote node $rn to the frontend sshpass -p $TMP_PASS ssh -oStrictHostKeyChecking=no $rn "sshpass -p $TMP_PASS ssh-copy-id -i $HOME/.ssh/id_rsa.pub -oStrictHostKeyChecking=no $USER@192.168.1.10" if [ "$whomai" = 'root' ] then echo i am root else if [ "$(whoami)" = 'root' ] then echo 5. I am root else echo 5. I am $(whoami). sshpass -p $TMP_PASS ssh -oStrictHostKeyChecking=no $rn "/share/apps/local/bin/gen_bashrc.txt" echo Have done gen_bashrc in $(echo $(hostname)) fi fi echo ' ' ip='192.168.1.10'; ssh-keygen -R $ip; ssh-keygen -R $(echo $(hostname)); ssh-keyscan -H $ip >> $HOME/.ssh/known_hosts; ssh-keyscan -H $(echo $(hostname)) >> $HOME/.ssh/known_hosts done