#! /bin/bash # If a new node is later added to the cluster, issue this script as root to sync from within the new node to sync the node against the frontend. ## 1 sync users cat > /share/tmp/temp.sh << EOF #! /bin/bash cat /etc/passwd | awk -F":" '{print \$1, \$3}' | awk '{if(\$2>=1000 && \$2<4000) {print}}' EOF hostname=$(echo $HOSTNAME) ssh -X -Y -oStrictHostKeyChecking=no 192.168.1.10 "sh /share/tmp/temp.sh > /share/tmp/username.dat" username=$(cat /share/tmp/username.dat | awk '{print $1}') echo 'username=' $username declare -a username=($username) uid=$(cat /share/tmp/username.dat | awk '{print $2}') declare -a uid=($uid) alength=${#username[@]} echo 'alength=' $alength for (( i=1; i<${alength}+1; i++ )); do echo 999 ${username[$i-1]} ${uid[$i-1]} mockpasswd > /share/apps/configrepo/users_data/newuser.dat passwd=$(cat $(ls /share/apps/configrepo/users_data/userpass* | sort | awk 'END {print}') | grep ${username[$i-1]} | awk '{print $4}') echo 'i=' $i ${username[$i-1]} ${uid[$i-1]} $passwd useradd -u ${uid[$i-1]} -g users -s /bin/bash -p $(echo "$passwd" | openssl passwd -1 -stdin) ${username[$i-1]} sh /share/apps/local/bin/gen_bashrc.txt echo user ${username[$i-1]} with uid ${uid[$i-1]} has been added to /etc/passwd in $hostname ## coc-sync-pwlssh if [[ -e /home/${username[$i-1]}/.ssh/id_rsa.pub ]] ; then echo 1. Will not generate id_rsa.pub; else ssh-keyscan 192.168.1.10 >> /home/${username[$i-1]}/.ssh/known_hosts; yes '' | coc-gen_sshkeygen ; echo 1. Will generate id_rsa.pub; fi echo 2. Copy the rsa key of the ${username[$i-1]} in the current node $hostname to the frontend. Password is $passwd sshpass -p $passwd ssh-copy-id -i /home/${username[$i-1]}/.ssh/id_rsa.pub -o StrictHostKeyChecking=no ${username[$i-1]}@192.168.1.10 echo ' ' echo 3. Remotely ssh-copy-id from the frontend to the current node $hostname sshpass -p $passwd ssh -oStrictHostKeyChecking=no 192.168.1.10 "sshpass -p $passwd ssh-copy-id -i /home/${username[$i-1]}/.ssh/id_rsa.pub -oStrictHostKeyChecking=no ${username[$i-1]}@$hostname" echo ' ' ip='192.168.1.10' ssh-keygen -R $ip ssh-keygen -R $hostname ssh-keyscan -H $ip >> /home/${username[$i-1]}/.ssh/known_hosts ssh-keyscan -H $hostname >> /home/${username[$i-1]}/.ssh/known_hosts done